場景如下,如何在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'條件來檢查用戶輸入。 –