-2

I need help with my code. Errors keep coming up and it seems I doing nothing right. I'm supposed to make a Java program that prompts the user to enter a number 1 to 10. If you enter a number that's not in the range, it's supposed to display an error message. My errors come from my last 2 lines and I just don't know what to do.

Here's my code so far and I'm just getting frustrated.

public static void main(String[] args) {

    int number;

    System.out.print("Enter a number in the " + "range of 1 - 10: ");
    value = input("'Please Enter a Number between 1 and 10 (1-10)'");
    while ( value < 1 || value > 10)
    }
Sotirios Delimanolis
  • 252,278
  • 54
  • 635
  • 683

2 Answers2

2

Here's what I see:

public static void main(String[] args) {

So far, so good - you're declaring your main method in your class (I assume you've wrapped this in a class definition?)

    int number;
    System.out.print("Enter a number in the " + "range of 1 - 10: ");

You're declaring an int named number. It's not assigned to anything yet, so it will initialize to 0. Then you're outputting something to the console. Nothing wrong so far.

    value = input("'Please Enter a Number between 1 and 10 (1-10)'");

Whoa, wait a second - what's value? This variable isn't defined anywhere so far (is it a class variable you haven't pasted into your question? Or is this your first error? You need to define what value is. Also, you're using a function named input - where is this defined?

    while ( value < 1 || value > 10)

Here, you've stared a while loop on value, but there is no loop contents. You'll want to do some reading on how a while loop works.

    }

Whew, at least your main function has a closing brace. I recommend aligning this brace with the beginning of the method declaration (in this case, the p in public).

I hope this helps somewhat. It sounds like this may lead to a lot more questions though, so you'll want to clarify with your course instructor some of the basics of java.

Krease
  • 14,501
  • 8
  • 47
  • 82
1
int number;

You name the variable number here, but later you call it value. You'll want to pick one or the other.

value = input("'Please Enter a Number between 1 and 10 (1-10)'");

You should look up how to display a prompt and ask for user input. You've got the right idea, but this input() function you're trying to call doesn't exist. As a human being I understand what you want, but the computer has no idea.

I recommend googling for "input number in java" or similar. You should be able to find some helpful links (for example, this Stack Overflow question).

while ( value < 1 || value > 10)

This is good loop logic. So what happens while the value is out of range? What do you want to happen inside the loop? You've got the start of the loop but no loop body. Presumably you'll want to give the user another chance to enter the number.

while (value < 1 || value > 10) {
    // loop body: ask the user to try again
}
Community
  • 1
  • 1
John Kugelman
  • 307,513
  • 65
  • 473
  • 519
  • What the program is suppose to do pick a number 1 though 10 and display it in Chinese (I already have that code completed since I finished it last night.) If someone displays it out of range, it's supposed to display an error. – user3281951 Feb 07 '14 at 01:10