我对 Java 不太熟悉...我有一个作业,需要编写一个程序,从用户那里获取一个数字,并验证它是否在 7 到 11 之间(包括 7 和 11)。我尝试过这样做,但我卡住了。这就是 iv'e 迄今为止尝试做的事情。
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 ");
}
}
最佳答案
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`
}
关于java - 程序从用户那里获取一个数字并验证它是否在 7 到 11 之间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26696482/