6 Replies - 306 Views - Last Post: 14 February 2013 - 01:47 AM Rate Topic: -----

#1 girldown92gtgn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 12-February 13

EvenOdd Program - Loop Problem

Posted 13 February 2013 - 12:09 AM

So I need to write a program that prompts the user to enter a number and then it will in turn tell you whether it was odd or even. My problem first problem is that once it displays the number, the output is not what it should look like. It has the directory coming right after the output and also prompts me to run the program again. I know it has to do with a loop, but I just don't know what to do. My second problem is that the program also must exit the loop when a 0 is entered while also displaying "thank you, goodbye."

This is a sample output that I get
Enter a number:
1
OddMy-MacBook-Pro:Desktop my$ java OddEven
Enter a number:
2
EvenMy-MacBook-Pro:Desktop my$ java OddEven
Enter a number:
10
EvenMy-MacBook-Pro:Desktop my$ java OddEven

Below is the code I have written so I was wondering where I have gone wrong and what I need to add/or change. Thank you!

import java.util.Scanner;

public class OddEven
{
   public static void main(String[] args)
   {

      int x;
      System.out.println("Enter a number:");

      Scanner s = new Scanner(System.in);
      x = s.nextInt();

      if(x % 2 == 0)
          System.out.print("Even");
      else
          System.out.print("Odd");

   }

}



Is This A Good Question/Topic? 0
  • +

Replies To: EvenOdd Program - Loop Problem

#2 Limecore  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 86
  • Joined: 28-November 12

Re: EvenOdd Program - Loop Problem

Posted 13 February 2013 - 01:26 AM

I don't see any loop there, so I don't know how you assume there is a problem with a loop.
Are you sure this is the code that you are running?

I tried running it and it worked fine

Maybe there is something wrong with your java installation
Was This Post Helpful? 0
  • +
  • -

#3 TheKirk  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 58
  • Joined: 10-February 13

Re: EvenOdd Program - Loop Problem

Posted 13 February 2013 - 04:09 AM

Just start a never ending loop and only exit if 0 was entered:

import java.util.Scanner;

public class OddEven
{
	public static void main(String[] args)
	{
		int x;
		
		System.out.println("Enter a number:");
		Scanner s = new Scanner(System.in);
		x = s.nextInt();
		
		// Never ending loop
		while(1)
		{
			// If x is 0
			if(x == 0)
			{
				System.out.println("Goodbye!"); // print message
				System.exit(1); // exit program
			}
			
			if(x % 2 == 0)
			{
				System.out.println("Even");
			}
			else
			{
				System.out.println("Odd"); // Use println to print on a new line
			}
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#4 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: EvenOdd Program - Loop Problem

Posted 13 February 2013 - 04:21 AM

Well, I feel that will result in an infinite loop which never exits...

Well, consider a value 4.

When the control enters the while loop, it will go to line 23 and print even. Now, it has no way of exiting the loop, and so once again, the control goes back to while and sees while(1) and the above process is repeated for ever.

Instead, you could take the while loop above the input like this.
 
 while(1)
	        {
                System.out.println("Enter a number:");
	        Scanner s = new Scanner(System.in);//Not required in while loop, can be declared outside
	        x = s.nextInt();
	         
	       //rest of code till line 33


In that way, the user will be prompted to enter the number each time and the possibility of the user entering 0 is very likely later on.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#5 TheKirk  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 58
  • Joined: 10-February 13

Re: EvenOdd Program - Loop Problem

Posted 13 February 2013 - 06:44 AM

Whoops my bad, the question 'enter a number' should be inside the loop yes. However OP is wants the program to quit when 0 is given.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: EvenOdd Program - Loop Problem

Posted 13 February 2013 - 10:07 AM

View PostTheKirk, on 13 February 2013 - 06:09 AM, said:

Just start a never ending loop and only exit if 0 was entered:

	
		// Never ending loop
		while(1)
		{


This is true in C/C++ but not in Java
Java does not interpret 1 as true so cannot convert from int to boolean
If you want a never ending loop you need:
   while(true) {


Was This Post Helpful? 0
  • +
  • -

#7 TheKirk  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 58
  • Joined: 10-February 13

Re: EvenOdd Program - Loop Problem

Posted 14 February 2013 - 01:47 AM

View Postpbl, on 13 February 2013 - 10:07 AM, said:

View PostTheKirk, on 13 February 2013 - 06:09 AM, said:

Just start a never ending loop and only exit if 0 was entered:

	
		// Never ending loop
		while(1)
		{


This is true in C/C++ but not in Java
Java does not interpret 1 as true so cannot convert from int to boolean
If you want a never ending loop you need:
   while(true) {



Of course you're right :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1