Results 1 to 4 of 4
  1. #1
    esined93 is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default My while loop is not working, why??

    This is what I'm trying to do: Write a program that reads in two integers (by user) and prints out which of the numbers is greater. If the numbers are equal, the text "The numbers are equal" should be printed. Extend the program so that it is repeated until the first number is zero.
    When I type in i.e 5, then 10 the program responds with: "Write another number". Insted I want it to respond with: "10 is bigger than 5"
    " Write a number: "

    Here's the code:

    import java.util.Scanner;

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

    Scanner reader = new Scanner(System.in);
    int b;
    System.out.println("Write a number: ");
    int a = reader.nextInt();
    while (a > 0)
    {
    System.out.println("Write another number: ");
    b = reader.nextInt();

    if (a > b)
    System.out.println(a + " is bigger than " + b);
    else if (a < b)
    System.out.println(b + " is bigger than " + a);
    else
    System.out.println("The numbers are equal");

    System.out.println("Write a number: ");
    a = reader.nextInt();
    }

    if (a == 0)
    System.out.println("Thank you.");

    }
    }

  2. #2
    Reeling is offline Member
    Join Date
    Jan 2012
    Posts
    49
    Rep Power
    0

    Default Re: My while loop is not working, why??

    edit your post and use code tags so the indentation is correct, please read the forum rules.

  3. #3
    JBelg is offline Member
    Join Date
    Oct 2012
    Posts
    36
    Rep Power
    0

    Default Re: My while loop is not working, why??

    Java Code:
    public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		//Initialize the a variable so you enter the loop for the first time
    		int a = -1, b;
    
    		//personally I'd use a do while instead of a regular while but I'll stick to a regular while.
    		// while a is not 0 keep looping
    		while(a != 0){
    			//ask the use to enter the first name (a)
    			System.out.println("Enter the first number: ");
    			a = input.nextInt();
    			//if the user entered 0 just skip everything and it'll exit the loop.
    			if (a != 0) {
    			 //if the user didn't enter 0 the first time ask for the second number.
    			 System.out.println("Enter the second number: ");
    			 b = input.nextInt();
    				if (a > b)
    					System.out.printf("%d is bigger than %d\n", a, b);
    				else if (a < b)
    					System.out.printf("%d is bigger than %d\n", b, a);
    				else
    					System.out.println("The numbers are equal.\n");
    			}
    		} 
    		
    		//no need to check if a is 0, you only get here if you come out of the loop.
    		System.out.println("Done!");
    	}
    This is a solution, do with it what you want.

  4. #4
    Reeling is offline Member
    Join Date
    Jan 2012
    Posts
    49
    Rep Power
    0

    Default Re: My while loop is not working, why??

    JBelg, perhaps you should read the forum rules as well. Don't give ready made solutions to people.

    Quote Originally Posted by JBelg View Post
    Java Code:
    public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		//Initialize the a variable so you enter the loop for the first time
    		int a = -1, b;
    
    		//personally I'd use a do while instead of a regular while but I'll stick to a regular while.
    		// while a is not 0 keep looping
    		while(a != 0){
    			//ask the use to enter the first name (a)
    			System.out.println("Enter the first number: ");
    			a = input.nextInt();
    			//if the user entered 0 just skip everything and it'll exit the loop.
    			if (a != 0) {
    			 //if the user didn't enter 0 the first time ask for the second number.
    			 System.out.println("Enter the second number: ");
    			 b = input.nextInt();
    				if (a > b)
    					System.out.printf("%d is bigger than %d\n", a, b);
    				else if (a < b)
    					System.out.printf("%d is bigger than %d\n", b, a);
    				else
    					System.out.println("The numbers are equal.\n");
    			}
    		} 
    		
    		//no need to check if a is 0, you only get here if you come out of the loop.
    		System.out.println("Done!");
    	}
    This is a solution, do with it what you want.
    quoted for reference.

Similar Threads

  1. for loop not working?
    By Newbieprogrammer in forum New To Java
    Replies: 3
    Last Post: 07-24-2012, 03:55 PM
  2. for loop stops working
    By name in forum New To Java
    Replies: 5
    Last Post: 04-17-2012, 05:53 PM
  3. Loop not working
    By swilliams236 in forum New To Java
    Replies: 2
    Last Post: 11-07-2011, 11:36 PM
  4. Why isn't this while loop code working
    By GreenTea in forum New To Java
    Replies: 16
    Last Post: 11-10-2010, 04:14 AM
  5. while loop not working
    By RBNSN83 in forum New To Java
    Replies: 6
    Last Post: 06-21-2010, 08:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •