编写简单的成绩管理系统,其中有成绩录入模块。成绩录入过程中,难免出现录入数据的类型出现错误,或录入的成绩不在合理的范围内。在成绩录入程序框架下,若出现上述错误,程序应该如何处理。程序框架如下,根据注释内容将代码补充完整。import java.io.*; import java.util.ArrayList; public class Exp1 { public static void main(String[] args) { ArrayList<Integer> scores = new ArrayList<Integer>(); inputScores(scores); outputScores(scores); } static void inputScores(ArrayList<Integer> scores) { System.out.println("Input q to quit."); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // fill here br.close(); } static void outputScores(ArrayList<Integer> scores) { for (int i = 0; i < scores.size(); i++) { System.out.println((i + 1) + ": " + scores.get(i)); } } }
时间: 2024-03-10 18:47:00 浏览: 26
好的,根据你的需求,我给出如下代码:
```
import java.io.*;
import java.util.ArrayList;
public class Exp1 {
public static void main(String[] args) {
ArrayList<Integer> scores = new ArrayList<Integer>();
inputScores(scores);
outputScores(scores);
}
static void inputScores(ArrayList<Integer> scores) {
System.out.println("Input q to quit.");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = "";
while(true){
try{
input = br.readLine(); // 获取控制台输入
if(input.equals("q")){ // 如果输入 "q",则退出输入循环
break;
}
int score = Integer.parseInt(input); // 将输入的字符串转换为整数
if(score < 0 || score > 100){ // 成绩不在合理范围内
System.out.println("Invalid score! Please input again.");
continue; // 继续输入
}
scores.add(score); // 将输入的成绩添加到列表中
} catch(NumberFormatException e){ // 输入的不是整数
System.out.println("Invalid input! Please input again.");
} catch(IOException e){ // 输入异常
e.printStackTrace();
}
}
try{
br.close(); // 关闭输入流
} catch(IOException e){
e.printStackTrace();
}
}
static void outputScores(ArrayList<Integer> scores) {
for (int i = 0; i < scores.size(); i++) {
System.out.println((i + 1) + ": " + scores.get(i));
}
}
}
```
在 inputScores() 方法中,我们使用 while 循环不断读取控制台输入,如果输入的是 "q",则退出输入循环。对于输入的成绩,我们使用 Integer.parseInt() 方法将输入的字符串转换为整数,如果输入的字符串不是整数,则会抛出 NumberFormatException 异常,我们在 catch 块中处理这种情况。如果输入的成绩不在合理范围内,则会提示用户重新输入。如果一切正常,则将输入的成绩添加到列表中。在最后,我们关闭输入流。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)