你用 args,也就是命令行参数的话,是在启动程序的时候的参数,不是后来的交互输入 你用 System.in.read的话,需要判断返回值,每次读入一个字符 或者使用重载版本,指定缓冲区 int read() Reads the next byte of data from the input stream. int read(byte[] b) Reads some number of bytes from the input stream and stores them into the buffer array b. int read(byte[] b, int off, int len)
yayv(yayv) 下面的几个函数就是交互式输入啊
import java.io.*;public class mainExercise { public static void main(String args[]) throws Exception { String s; int a; int b = 0;
System.out.println("enter 3 number,0 to quit"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); while (true) { s = br.readLine(); a = Integer.parseInt(s); b +=a; if(a==0) break; } System.out.println(b/3); } }
public static void main(String args[]){
System.out.println("请输入四个参数:");
try{System.in.read();}
catch(IOException ioe){
System.out.println("error input");
}
double A=Double.parseDouble(args[0]);
double B=Double.parseDouble(args[1]);
double C=Double.parseDouble(args[2]);
double v,t;
v=A+B+C
t=v/3;
System.out.println("和:"+v);
System.out.println("平均值"+t);
}
}运行的时候,我输入10 20 30
但是提示错误:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at computertime.ComputerTime.main(ComputerTime.java:20)谢谢!!
你用 System.in.read的话,需要判断返回值,每次读入一个字符
或者使用重载版本,指定缓冲区
int read()
Reads the next byte of data from the input stream.
int read(byte[] b)
Reads some number of bytes from the input stream and stores them into the buffer array b.
int read(byte[] b, int off, int len)
public static void main(String args[]) throws Exception {
String s;
int a;
int b = 0;
System.out.println("enter 3 number,0 to quit");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (true) {
s = br.readLine();
a = Integer.parseInt(s);
b +=a;
if(a==0) break;
}
System.out.println(b/3);
}
}