hello
i trying to learn how to type in for exampel 5 and its going to type out
5 4 3 2 1 0
import java.util.Scanner;
class indexerad_variable
{
public static void main( String args[] )
{
int i= 0;
while (i<= 100){
System.out.println("your number: ");
System.out.println((100-i));
i++;
}
}
}
yes i know its only typing your number then counting down to 0.
The logic for a simple version of what you are talking would look like this:
Scanner sc=new Scanner(System.in);
System.out.println("Enter Number");
int num= sc.nextInt();
for(int i=num;i>=0;i--) {
System.out.print(i+" ");
}
DavidKroukamp
Practically a Master Poster
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
If the user input 5 you print the 5, then subtract 1, then print the result (4), then subtract another 1 and print 3 etc... until you get down to 0
You;ll have to write the code yourself - but here's an outline of what you need:
ask the user for a number
input the number from the user
while the number is greater than or equal to zero
print the number
subtract one from the number
or you could just cheat and copy the code that someone posted without any attempt to explain or teach.
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
thx for the help. yes i can copy it but i want to learn to. its no point in only copying a code and dont understand it
As you can see i did not put it in the format you wanted as this then would be very easy and non-educational. And i didnt do an explanation because i found it self explanatory:
Scanner sc=new Scanner(System.in);//initiate scanner to get console input
System.out.println("Enter Number");//ask the user to input a number
int num= sc.nextInt();//read the number into a int variable
for(int i=num;i>=0;i--) {//let i equal the number they want, while i lessthen or equal to zero print i and minus 1
System.out.print(i+" ");//prints i
}
Hope that helps, its up to you to put it in a while statement thats where JamesCherrill's answer comes in
DavidKroukamp
Practically a Master Poster
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
well, if you write the code for the pseudocode JamesCherrill provided, you should be almost finished.
stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433