我正在接受4個點並返回形狀類型的項目。這些要求非常具體。掃描儀在一條線上僅接受2個整數
用戶需要在一行上輸入點x和y(例如:1 2)。額外的輸入(例如:1 5 3)3應該被忽略,無效的輸入(像char)會導致用戶被重新提示,直到有效的輸入。我也需要使用這些值來創建一個點。我完全失去了如何實現這一目標。
Integer
我正在接受4個點並返回形狀類型的項目。這些要求非常具體。掃描儀在一條線上僅接受2個整數
用戶需要在一行上輸入點x和y(例如:1 2)。額外的輸入(例如:1 5 3)3應該被忽略,無效的輸入(像char)會導致用戶被重新提示,直到有效的輸入。我也需要使用這些值來創建一個點。我完全失去了如何實現這一目標。
Integer
在第二種方法validateInputY,你又初始化掃描儀,而不是做如下:
public static void main(String[] args) {
Integer x1 = null, y1 = null, x2 = null, y2 = null, x3 = null, y3 = null, x4 = null, y4 = null;
System.out.println("Point 1 x y: ");
Scanner input = new Scanner(System.in);
x1 = validInputX(x1, input);
y1 = validInputY(y1, input);
}
public static int validInputX(Integer x, Scanner input) {
while (x == null) {
try {
x = input.nextInt();
} catch (InputMismatchException ex) {
input = new Scanner(System.in);
System.out.println("please provide valid input");
}
}
return x;
}
public static int validInputY(Integer y, Scanner input) {
while (y == null) {
try {
y = input.nextInt();
} catch (InputMismatchException ex) {
input = new Scanner(System.in);
System.out.println("please provide valid input");
}
}
return y;
}
如果您瞭解算法,這很容易。 我想你可以自己輸入的代碼,因此,這是一種方法可以實現:從掃描儀
我們怎樣才能幫助? – shmosel
對不起,我現在編輯它 – Levy