java如何从数据台输入不同类型的数据并储存下来 假设有10个学生,每个数据包括姓名,学号,3门课的成绩,从键盘输入每个学生的数据,处理数据的输入异常,计算每个学生3门课的平均分,并分别计算3门课的所有学生的平均分。我不懂的是如何把数据输入?求提点 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 //数据输入,可以通过类型转换把用户输入的字符串转成相应的类型Scanner sc = new Scanner(System.in); //获取标准输入流扫描器String buf = sc.nextLine(); //获取用户输入的字符串try { int i = Integer.parseInt(buf); //把用户输入的字符串转成int类型 double d = Double.parseDouble(buf);//把用户输入的字符串转成double类型} catch (Exception e) { //异常处理}//for exampleclass Student { String name; String id; double[] scores = new double[3]; public Student() {} public Student(String name, String id) { this.name = name; this.id = id; } public void setName(String name) {this.name=name;} public void setId(String id) {this.id=id;} public void setScore(double[] scores) { for (int i=0; i<Math.min(this.scores.length, scores.length); i++) { this.scores[i] = scores[i]; } } public void setScore(int index, double score) { if (index<0||index>=this.scores.length) return; this.scores[index] = score; } public String getName(){return name;} public String getId() {return id;} public double[] getScores() {return Arryas.copyOf(scores, scores.length);} public double getScore(int index) { if (index<0||index>=this.scores.length) return -1; return this.scores[index]; } public String toString() { return String.format("name=%s, id=%s, score1=%.2f, score2=%.2f, score3=%.2f", name, id, scores[0], scores[1], scores[2]); } public double getSum() { double sum = 0; for (int i=0; i<scores.length; i++) { sum += scores[i]; } return sum; } public double getAvg() { return getSum()/scores.length; }}public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int cnt = 0; Student[] stu = new Student[10]; String name, id; double[] scores = new double[3]; while (cnt < stu.length) { System.out.println("please input student name"); name = sc.nextLine(); System.out.println("please input student id"); id = sc.nextLine(); for (int i=0; i<scores.length;) { try { System.out.printf("please input student score[%d]\n", i+1); scores[i] = Double.parseDouble(sc.nextLine()); i++; } catch (Exception e) { System.out.println("error input, please input again"); } } stu[cnt] = new Student(name, id); stu[cnt].setScores(scores); cnt++; } double sum = 0; for (int i=0; i<stu.length; i++) { System.out.printf("name=%s, avg=%.2f\n", stu[i].getName(), stu[i].getAvg()); sum += stu.getSum(); } System.out.printf("total avg: %.2f\n", sum/stu.length/scores.length); }} public static void main(String[] args) { System.out.println("输入语文成绩"); String core = getInput(); System.out.println("语文成绩是: " + core); }public static String getInput() { InputStreamReader readerResponse = new InputStreamReader(System.in); BufferedReader bufferResponse = new BufferedReader(readerResponse); try { return bufferResponse.readLine(); } catch (IOException e) { return null; } }其他的LZ自己做一下吧 Java JFrame 菜鸟问题 try....catch...finally 如何取字符? string类中的substring函数,如果第2个函数参数已经超出了字符串的范围,或者字符串本身为null,那会如何? double类型比较的问题 【JButton 在 Windows 风格下的默认边框是什么?】 关于JFrame,Canvas边界的问题 如何暂停一个带有内部循环的线程的问题?在线等待 请教如何才能固定jbutton的大小,帮忙了 求教高手rose对java源程序逆向工程的问题! 界面的问题 求递归算法代码详细注释
//数据输入,可以通过类型转换把用户输入的字符串转成相应的类型
Scanner sc = new Scanner(System.in); //获取标准输入流扫描器
String buf = sc.nextLine(); //获取用户输入的字符串
try {
int i = Integer.parseInt(buf); //把用户输入的字符串转成int类型
double d = Double.parseDouble(buf);//把用户输入的字符串转成double类型
} catch (Exception e) {
//异常处理
}
//for example
class Student {
String name;
String id;
double[] scores = new double[3]; public Student() {}
public Student(String name, String id) {
this.name = name;
this.id = id;
} public void setName(String name) {this.name=name;}
public void setId(String id) {this.id=id;}
public void setScore(double[] scores) {
for (int i=0; i<Math.min(this.scores.length, scores.length); i++) {
this.scores[i] = scores[i];
}
}
public void setScore(int index, double score) {
if (index<0||index>=this.scores.length) return;
this.scores[index] = score;
}
public String getName(){return name;}
public String getId() {return id;}
public double[] getScores() {return Arryas.copyOf(scores, scores.length);}
public double getScore(int index) {
if (index<0||index>=this.scores.length) return -1;
return this.scores[index];
}
public String toString() {
return String.format("name=%s, id=%s, score1=%.2f, score2=%.2f, score3=%.2f",
name, id, scores[0], scores[1], scores[2]);
}
public double getSum() {
double sum = 0;
for (int i=0; i<scores.length; i++) {
sum += scores[i];
}
return sum;
}
public double getAvg() {
return getSum()/scores.length;
}
}public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int cnt = 0;
Student[] stu = new Student[10];
String name, id;
double[] scores = new double[3];
while (cnt < stu.length) {
System.out.println("please input student name");
name = sc.nextLine();
System.out.println("please input student id");
id = sc.nextLine();
for (int i=0; i<scores.length;) {
try {
System.out.printf("please input student score[%d]\n", i+1);
scores[i] = Double.parseDouble(sc.nextLine());
i++;
} catch (Exception e) {
System.out.println("error input, please input again");
}
}
stu[cnt] = new Student(name, id);
stu[cnt].setScores(scores);
cnt++;
}
double sum = 0;
for (int i=0; i<stu.length; i++) {
System.out.printf("name=%s, avg=%.2f\n", stu[i].getName(), stu[i].getAvg()); sum += stu.getSum();
}
System.out.printf("total avg: %.2f\n", sum/stu.length/scores.length);
}
}
public static void main(String[] args) {
System.out.println("输入语文成绩");
String core = getInput();
System.out.println("语文成绩是: " + core);
}public static String getInput() {
InputStreamReader readerResponse = new InputStreamReader(System.in);
BufferedReader bufferResponse = new BufferedReader(readerResponse);
try {
return bufferResponse.readLine();
} catch (IOException e) {
return null;
}
}其他的LZ自己做一下吧