日期:2014-05-19 浏览次数:20820 次
public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int[] input = new int[100]; for (int i = 0; i < 100; i++) { int num = 0; boolean stop = false; while (!stop) { try { System.out.print("Input: "); String s = in.readLine(); if ("s".equals(s.toLowerCase())) { return; } num = Integer.parseInt(s); stop = true; input[i] = num; } catch (NumberFormatException nfe) { System.out.println("Input is invalid.Pls input again!"); } } } }
------解决方案--------------------
while (!stop) {
try {
System.out.print("Input: ");
String str = in.readLine());
if ("s".equals(str.toLowerCase()) ){
break ;
}
num = Integer.parseInt(str);
input[i] = num;
} catch (NumberFormatException nfe) {
System.out.println("Input is invalid.");
}