user1323393
user1323393

Reputation: 1

A program that will loop under the conditions of an operator

I'm writing a java program that will count up to a number that the user inputs. The user is only allowed to input a number that is between 1-10.

For instance:

if the user entered 6 the output will be:

1 2 3 4 5 6

How do I do this with only using operators and while and if statements?

Here's my code. I've been painfully trying to figure out why my code won't work. Thanks in advance!

import java.util.Scanner;

public class loop_lab {

    public static void main(String[] args) {
        System.out.println("Hi user, input any number that is between 1-10");{
            Scanner input = new Scanner(System.in);
            int num1 = input.nextInt();
            int num2 = 0;

            if (1<=num1 && num1>=10);
            num2=0;

            while (num2 < num1)
            System.out.println(""+(num2 + 1));
                num2++;

            }
            }
        }

Upvotes: 0

Views: 186

Answers (5)

Joost
Joost

Reputation: 3209

I think the problems lies with the code-blocks (the stuff between {}). Especially look at how the while-loop behaves. What is supposed to be in the loop and what not? Also, your if-statement is empty. The ; closes the code-block that is handled by the if.

An IDE can help you detect these errors by applying syntax-formatting. The comments in your code looked like they were coming from Eclipse. Try ctrl-shift-f (or look it up in the menu). This automatically formats and indents your code, this makes it easier to detect errors in the structure.

Upvotes: 1

GoSmash
GoSmash

Reputation: 1108

Try this one

import java.util.Scanner;

public class loop_lab {
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.out.println("Hi user, input any number that is between 1-10");
        Scanner input = new Scanner(System.in);
        int num1 = input.nextInt();
        int num2 = 1;

        if (1<=num1 && num1>=10){
            num2=1;

            while (num2 <= num1)
            {
                System.out.println("" + num2);
                num2++;
            }
        }
    }
}

Upvotes: 0

Zaz Gmy
Zaz Gmy

Reputation: 4356

in while loop just change

 while (num2 < num1){
        if(num2==0)
         System.out.println((num2 + 1));
        else{
           num2++;
           System.out.println(num2);

        }


            }

Upvotes: 0

Marcelo
Marcelo

Reputation: 4608

First, your conditional check should use an or and braces; and assign 0 to num1, as to prevent the loop from running if the user inputs anything outside the 1-10 range:

if (num1 < 1 || num1 > 10){
    num1=0;
}

And you can also improve your loop:

while (num2 < num1) {
    System.out.println( ""+ num2++ );
}

Also, as said by user689893, check your {} blocks.

Upvotes: 0

Andrew Thompson
Andrew Thompson

Reputation: 168835

The if has a stray trailing ; As a result, the next line is always run.

I make it a point to include even single line statements involved with conditional statements and loops inside {/}. That helps make the start & end of the code block clear. My earlier comment about the code indentation is also a factor in identifying where code block begin & end.

Upvotes: 0

Related Questions