我在這裏有一個簡單的do while
循環。我現在唯一的問題是這個循環現在只接受數字。我需要它接受除空白輸入之外的所有內容。如何在不輸入任何內容時提示用戶
import java.util.Scanner;
public class Assignment6 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
boolean notValid = true;
int numberAsInt = 0;
do {
try {
System.out.print("Enter a number to Convert > ");
String number = scan.nextLine();
numberAsInt = Integer.parseInt(number);
notValid = false;
}
catch (Exception e) {
}
} while (notValid);
}
}
非常感謝這是最有幫助的 – 2014-10-29 08:15:59
我很高興它有幫助。那麼請接受它作爲迴應。 – Iootu 2014-10-29 08:21:47
嘿,還有一件事。我正在使用基地,我需要提供一個錯誤消息。我可以在這裏發佈我的代碼嗎? – 2014-10-29 08:37:18