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.

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+" ");
        }

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.

Edited 4 Years Ago by JamesCherrill: n/a

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

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

Edited 4 Years Ago by DavidKroukamp: n/a

This article has been dead for over six months. Start a new discussion instead.