Java 字符串数组和BufferedReader
我正在做一个课堂作业,一开始我就被困在那里。我不知道如何进行用户输入,在我告诉您任务是什么之后,我会详细说明 第一个输入是10个T或F答案的测验的答案键。然后用户输入学生的名字和姓氏,ID#,然后回答T/F的“测验”,用户输入任意数量的学生,然后“ZZZZ”终止。所有用户输入都在一个条目中输入,这就是我遇到的问题 程序的输入示例:Java 字符串数组和BufferedReader,java,arrays,string,split,newline,Java,Arrays,String,Split,Newline,我正在做一个课堂作业,一开始我就被困在那里。我不知道如何进行用户输入,在我告诉您任务是什么之后,我会详细说明 第一个输入是10个T或F答案的测验的答案键。然后用户输入学生的名字和姓氏,ID#,然后回答T/F的“测验”,用户输入任意数量的学生,然后“ZZZZ”终止。所有用户输入都在一个条目中输入,这就是我遇到的问题 程序的输入示例: 1 T T T T T F T T F F Bobb, Bill 123456789 T T T T T F T T F F Lou, Mary 97438764
1 T T T T T F T T F F
Bobb, Bill 123456789 T T T T T F T T F F
Lou, Mary 974387643 F T T T T F T T F F
Bobb, Sam 213458679 F T F T f t 6 T F f
Bobb, Joe 315274986 t t t t t f t t f f
ZZZZ
将产生以下输出:
小测验1的结果:
123-45-6789 Bill Bobb 10
974-38-7643 Mary Lou 9
213-45-8679 Sam Bobb 5
315-27-4986 Joe Bobb 10
The average score is 8.5
我们必须使用BufferedReader,所有的输入一次全部输入。我遇到的问题是我不知道如何进行输入。起初我想我会用换行符分割输入,并创建一个数组,其中每个索引都是换行符,但我现在只打印“ZZZZ”,我不知道为什么?我也不知道如何将第一个索引(答案键)与所有学生的答案进行比较。一旦我用换行符分割了输入,我可以用空格分割数组中的每个索引吗?非常感谢您的帮助!请记住,我对Java非常陌生。
到目前为止我所拥有的(我知道不多,但我只是被困在了前面)
}我刚刚在这里输入了这段代码,因此可能有输入错误,您应该验证它。但这是一种方法 如果使用ArrayList存储学生详细信息,则不需要知道学生人数。ArrayList的大小是动态的
import java.util.ArrayList;
公共类CST200_Lab4{
公共静态void main(字符串[]args)引发IOException{
字符串inputValue=“”;
ArrayList学生=新建ArrayList()
字符串[]应答器;
BufferedReader BR=新的BufferedReader(新的InputStreamReader(System.in));
//第一行永远是答案
字符串answers=BR.readLine();
answerArr=答案。拆分(“”);
而(!(inputValue.equalsIgnoreCase(“ZZZZ”)){
inputValue=BR.readLine();
//添加额外的数组元素,以便我们以后可以使用它来存储分数
inputValue=inputValue+“分数”;
字符串[]inputArr=inputValue.split(“”);
int studentTotal=0;
对于(int i=3,i
在main()中使用此代码
String inputValue=“”;
字符串inputArr[]=新字符串[13];
字符串answerKey=null;
字符串numStudents[];
InputStreamReader ISR=新的InputStreamReader(System.in);
BufferedReader BR=新的BufferedReader(ISR);
试一试{
inputValue=BR.readLine();
字符串answers[]=inputValue.split(“”);
整数计数=0;
System.out.println();
而((inputValue=BR.readLine())!=null){
if(inputValue.equalsIgnoreCase(“ZZZZ”))
打破
inputArr=inputValue.split(\\s+);
系统输出打印(inputArr[2]+“”);
系统输出打印(inputArr[1]+“”);
System.out.print(inputArr[0]。拆分(“,”[0]+”);
计数=0;
对于(int i=0;答案是第一个输入,这很有意义。我还没有学习ArrayList,但我会检查它。你知道为什么我没有用换行符拆分成单独的元素吗?你能澄清一下你的意思吗?如果你是指string.split,请看一下本教程-我的意思是拆分成单独的arra索引因此,用户一次输入三个学生,每一个学生在一个新行中,然后zzzz结束它。数组现在有三个元素,每个元素都是所有学生的信息。我想我上面的方法是这样做的,但是当我试图打印第一个元素以查看它是否工作时,它打印的所有元素都是zzzz。另外,我想我理解了还有你的代码,除了你的数组列表如何区分每个学生的输入,这样我可以在打印输出时引用它……这有意义吗?我最初也有一个大小为13的数组,因为如果有意义的话,每行中每个学生有13个元素。我希望我没有问那些愚蠢的问题,因为这会给你一个索引还有,如果我们在while循环中使用Break,我的教授会逐字标出分数,这不是开玩笑。这很好!!来吧,伙计,你可以修改if逻辑来消除Break。oyu得到的是哪一行异常?使用与你在问题中发布的输入完全相同的输入来运行上面的代码。它为我运行方式。我想你一定是在以某种方式改变输入的格式,这是导致异常的原因。另外,我想说最好是将输入包含在一个文件中,然后从中读取,而不是从控制台读取。我想可能是计数初始化后的打印行,我没有按enter键,我输入了更多的输入。我明天早上再试一次谢谢你们的帮助,伙计们
public class CST200_Lab4 {
public static void main(String[] args) throws IOException {
String inputValue = " ";
String inputArr[] = new String[13];
String answerKey = null;
String numStudents[];
InputStreamReader ISR = new InputStreamReader(System.in);
BufferedReader BR = new BufferedReader(ISR);
while(!(inputValue.equalsIgnoreCase("ZZZZ"))) {
inputValue = BR.readLine();
inputArr = inputValue.split("\\r+");
answerKey = inputArr[0];
}
System.out.println(answerKey);
}
import java.util.ArrayList;
public class CST200_Lab4 {
public static void main(String[] args) throws IOException {
String inputValue = "";
ArrayList<String[]> students = new ArrayList<String[]>()
String[] answerdarr;
BufferedReader BR = new BufferedReader(new InputStreamReader(System.in));
//first line is always the answers
String answers = BR.readLine();
answerArr = answers.split(" ");
while(!(inputValue.equalsIgnoreCase("ZZZZ"))) {
inputValue = BR.readLine();
//add extra array element so we can later use it to store score
inputValue = inputValue + " Score";
String[] inputArr = inputValue.split(" ");
int studentTotal = 0;
for(int i = 3, i < inputArr.length; i++) {
if(inputArr[i].equals["T"]) {
studentTotal++;
}
}
//previous value of this is "Score" as we set earlier
inputArr[13] = studentTotal;
students.add(inputArr);
}
//Now do your printing here...
}
}
String inputValue = " ";
String inputArr[] = new String[13];
String answerKey = null;
String numStudents[];
InputStreamReader ISR = new InputStreamReader(System.in);
BufferedReader BR = new BufferedReader(ISR);
try {
inputValue = BR.readLine();
String answers[] = inputValue.split(" ");
int count = 0;
System.out.println();
while((inputValue = BR.readLine()) != null) {
if (inputValue.equalsIgnoreCase("ZZZZ"))
break;
inputArr = inputValue.split("\\s+");
System.out.print(inputArr[2] + " ");
System.out.print(inputArr[1] + " ");
System.out.print(inputArr[0].split(",")[0] + " ");
count = 0;
for(int i = 0; i <10; i++){
if(inputArr[i+3].equalsIgnoreCase(answers[i+1]))
count ++;
}
System.out.print(count);
System.out.println();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}