Jump to content

Java coding (TMC)

Go to solution Solved by fjdhdhcyfhf,

if you enter the number 3 then num = 3 which means while (3 < 3) results in false. I dont know why it is counting down, I have had too many beers to work it out in my head.

 

If you want to count from one to the number entered then do this:

 

for (int i = 1; i <= num; i++){

        System.out.println(i);

}

import java.util.Scanner;
public class javacode {
    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        System.out.println("till?");
        int num = Integer.parseInt(reader.nextLine());  
        int i = 0;
        while (num < 3) {
            num += 1;
            i++;
            System.out.println(num);
        }
    }
}


 

this code should give me result like this example:

user gives number 3 to program and program should do this

1

2

3

but mine does 

3

2

1

pls help thx xD

Link to comment
https://linustechtips.com/topic/834271-java-coding-tmc/
Share on other sites

Link to post
Share on other sites

if you enter the number 3 then num = 3 which means while (3 < 3) results in false. I dont know why it is counting down, I have had too many beers to work it out in my head.

 

If you want to count from one to the number entered then do this:

 

for (int i = 1; i <= num; i++){

        System.out.println(i);

}

             ☼

ψ ︿_____︿_ψ_   

Link to comment
https://linustechtips.com/topic/834271-java-coding-tmc/#findComment-10427572
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×