要求:这个循环要求输入最多100个数字。如果一直输入数字,一直循环。如果输入了特定字母s,这个循环停止。如果到了100次,也停止。如果输入的是非数字和非字母s,重新要求用户输入。import java.io.*;public class 123 { 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: ");
num = Integer.parseInt(in.readLine());
stop = true;
input[i] = num;
} catch (NumberFormatException nfe) {
System.out.println("Input is invalid.");
}
}
}
}这是我写的,但是不能在用户输入字母s的时候停止,请问怎么修改?
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: ");
num = Integer.parseInt(in.readLine());
stop = true;
input[i] = num;
} catch (NumberFormatException nfe) {
System.out.println("Input is invalid.");
}
}
}
}这是我写的,但是不能在用户输入字母s的时候停止,请问怎么修改?
解决方案 »
- 请教大神关于java.lang.NoClassDefFoundError问题,java环境变量没错,因为其他的运用能过
- hibernate设置lazy="false"后能不能动态修改
- Struts2 在服务器上报错
- 求解语法的含义
- 通过hibernate的多表链接查询出List页面怎么循环写列表
- 生成XML文件!!!!!!!! 急
- java做网站一般用什么框架
- 救急啊,struts的问题,太蹊跷了,都来看看吧.
- 如何替换JVM中的ClassLoader
- 急!!!!stuts 页面提交后 转到另一个页面 页面bean没有重新载入,无法显示正确结果
- 输入输出流问题
- 求助 linux resin 编码问题
while (i<100) {
in = read();
if ( in == s ) break;
if ( in is not a num ) print(error message);
i++;
}
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!");
}
}
}
}
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.");
}