While Loop Help (beginner)
Hello I have an assignment for my beginners java course this semester and I'm having some problems with the program I am trying to create. The purpose of the program is to get a starting number, ending number, and an increment from the user to count upwards from the starting number to the ending number. Also it should say if the number printed is negative or positive which I believe I done correctly in my code using the modulus expression. For example the user chooses 3 for start number and 12 for ending number and 3 for the increment the program should print out:
The number 3 is negative
The number 6 is positive
The number 9 is negative
The number 12 is positive
I have some code posted below and It is all kinds of terrible. I was hoping to get a little bit of help.
Code Java:
import java.util.*;
public class numbers
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
int endNumber;
int startNumber;
int increment;
int count;
System.out.print("Your starting number? ");
startNumber = console.nextInt();
System.out.print("Your ending number? ");
endNumber = console.nextInt();
System.out.print("Your increment number? ");
increment = console.nextInt();
System.out.println();
while(count <= endNumber)
{
if(0 == count%2)
{
System.out.println("The number " + count + " is positive");
}
else
{
System.out.println("The number " + count + " is negative");
}
}
System.out.println("Thanks for playing");
}
}
Re: While Loop Help (beginner)
Quote:
I was hoping to get a little bit of help.
What sort of help? Does the code compile? Does it behave correctly?
Re: While Loop Help (beginner)
I cant seem to get it compiled because I don't have count initialized. The problem I seem to be having most is how to actually take the increment the user inputs and get it to count by that number.
Re: While Loop Help (beginner)
I would presume you want to initialize count to the initial number, then increment by increment
Code java:
count = startNumber;
....
count += increment;
....
Re: While Loop Help (beginner)
yes that is exactly what I was wanting. Thank you very much for the help.