场景如下,如何在Java中设置参数?
装饰者需要输入房间的高度(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米)输入不在打印错误的参数内。
是否真的需要创建这些很多'Scanner's的。 – Satya
你不应该多次包装一个流。这导致不可预知的结果。 –
使用'if'条件来检查用户输入。 –