Results 1 to 4 of 4
- 10-23-2012, 08:20 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 4
- Rep Power
- 0
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.");
}
}
- 10-23-2012, 09:17 PM #2
Member
- Join Date
- Jan 2012
- Posts
- 49
- Rep Power
- 0
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.
- 10-23-2012, 11:28 PM #3
Member
- Join Date
- Oct 2012
- Posts
- 36
- Rep Power
- 0
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!"); }
- 10-24-2012, 02:05 AM #4
Member
- Join Date
- Jan 2012
- Posts
- 49
- Rep Power
- 0
Similar Threads
-
for loop not working?
By Newbieprogrammer in forum New To JavaReplies: 3Last Post: 07-24-2012, 03:55 PM -
for loop stops working
By name in forum New To JavaReplies: 5Last Post: 04-17-2012, 05:53 PM -
Loop not working
By swilliams236 in forum New To JavaReplies: 2Last Post: 11-07-2011, 11:36 PM -
Why isn't this while loop code working
By GreenTea in forum New To JavaReplies: 16Last Post: 11-10-2010, 04:14 AM -
while loop not working
By RBNSN83 in forum New To JavaReplies: 6Last Post: 06-21-2010, 08:29 AM
Bookmarks