从用户那里获取一个数字并验证它在7到11之间的程序?

时间:2014-11-02 05:53:48

标签: java

我并不熟悉Java ...我有一项任务,我需要制作一个程序,从用户那里获取一个数字并验证它是否在7到11之间。我试过这样做,但我卡住了。这就是iv到目前为止所做的事情。

import java.util.Scanner;

public class assignment8 {


    Scanner in = new Scanner(System.in);

    public static void main(String[] args){ 

       int value1 = 7;
       int value2 = 11;

        if(value1 == value2)
            System.out.println("value1 == value2");
        if(value1 != value2)
            System.out.println("value1 != value2");
        if(value1 > value2)
            System.out.println("value1 > value2");
        if(value1 < value2)
            System.out.println("value1 < value2");
        if(value1 <= value2)
            System.out.println("value1 <= value2");

        System.out.print("Please enter a number ");

    }
}

4 个答案:

答案 0 :(得分:2)

public class Demo {
    public static void main(String[] args) {
        System.out.println("Enter number here : ");
        int num;

        Scanner scanIn = new Scanner(System.in);

        try {

            num = scanIn.nextInt();

            if (num >= 7 && num <= 11) {
                System.out.println("Number is between 7 and 11 inclusive");
            } else {
                System.out.println("Number is not between 7 and 11");
            }
        } catch (InputMismatchException e) {
            System.out.println("Please enter a valid integer");
        } finally {
            scanIn.close();
        }`enter code here`

    }

答案 1 :(得分:1)

您缺少从用户那里获取输入。

Scanner scan = new Scanner(System.in);
System.out.print("Please enter a number");
int num = scan.nextInt(); // take the input from the user.

if(num >= 7 && num <=11){ //case to make sure that the number lies between 7 and 11 inclusively
    System.out.print("The number "+num+" lies between 7 and 11");
}
else{
    System.out.println("The number "+num+" does not lie between 7 and 11");
}

答案 2 :(得分:0)

你必须首先接受输入并使用条件验证输入,请参阅我的示例代码:) 了解basics from here

public class SeventoElevenInclusive {

    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        System.out.println("Enter a number");
        int number=input.nextInt();

        if(number>=7 && number<=11){
            System.out.println(number+" is accepted");
        }
        else if(number<7){
                System.out.println(number+" is less than 7");
            }
        else{
            System.out.println(number+" is greater than 11");
        }

    }
}

答案 3 :(得分:0)

我没有看到你在某个数字中读到的任何内容。您的Scanner字段不是static,因此main()无法使用它。我赞成存储你的最小和最大常数,但我个人并不关心他们当前的名字(value1value2)。相反,我认为你想要像 -

private static final int MIN = 7;
private static final int MAX = 11;

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.print("Please enter a number ");
    if (in.hasNextInt()) {
        int num = in.nextInt();
        if (num < MIN) {
            System.err.printf("%d is less than the minimum of %d%n", num,
                    MIN);
        } else if (num > MAX) {
            System.err.printf("%d is greater than the maximum of %d%n",
                    num, MAX);
        } else {
            System.out.printf("%d is between %d and %d%n", num, MIN, MAX);
        }
    }
}