方案如下,
装饰者需要输入房间的高度(2至6米之间),然后是所有四面墙的长度(最小1米;最大25米)。
System.out.println("Enter Height of the room");
Scanner hr = new Scanner(System.in);
int height = hr.nextInt();
System.out.println("Enter Length1 of the room");
Scanner l1 = new Scanner(System.in);
int length = l1.nextInt();
System.out.println("Enter Length2 of the room");
Scanner l2 = new Scanner(System.in);
int length2 = l2.nextInt();
System.out.println("Enter Length3 of the room");
Scanner l3 = new Scanner(System.in);
int length3 = l3.nextInt();
System.out.println("Enter Length4 of the room");
Scanner l4 = new Scanner(System.in);
int length4 = l4.nextInt();
我已经编写了扫描仪来接收用户的输入,但我不知道如何为扫描仪设置参数。我希望程序做的是接收用户的输入,如果(例如,房间的高度是9米),输入不在打印错误的参数范围内。
答案 0 :(得分:1)
如果我理解正确,你必须创建你所谓的参数。 无法满足您的需求。
所以,再次,如果我理解正确,你应该创建if条件来检查用户是否给你正确的输入。
而且你只需要 ONE Scanner
个实例。所以:
Scanner
如果您需要获得其他输入,请使用相同的扫描仪Scanner scannerToUsAll = new Scanner(System.in);
System.out.println("Enter Height of the room");
int height = scannerToUsAll.nextInt();
//here you check
if ( height < 2 && height > 6 ){
System.out.println("The Height is not within the parameters (2 and 6)");
}
您需要控制应用程序的流程以退出或返回相同的问题。我在这里提示:int length = scannerToUsAll.nextInt();
答案 1 :(得分:0)
System.out.println("Enter Height of the room");
Scanner sc = new Scanner(System.in);
int height = sc.nextInt();
if (height < 2 || height > 6)
{
System.out.println("Error: height is invalid");
}
System.out.println("Enter Length1 of the room");
int length1 = sc.nextInt();
if (length1 < 1 || length1 > 25)
{
System.out.println("Error: length1 is invalid");
}
System.out.println("Enter Length2 of the room");
int length2 = sc.nextInt();
if (length2 < 1 || length2 > 25)
{
System.out.println("Error: length2 is invalid");
}
......等等......