2015-10-06 62 views
2

我正在为类的任务工作,并且我一开始就卡住了。我不知道如何去用户输入,我会详细阐述,当我告诉你什么是任务....字符串数组和缓冲读取器

第一个输入将是一个测验答案的十个T或F答案。然后它将用户输入作为学生的姓名和ID#,然后回答T/F的“测验”,用户输入尽可能多的学生,然后输入“ZZZZ”终止。所有用户输入都输入到一个条目中,这就是我遇到的问题。

的程序的一个例子的输入:输入

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和所有的输入的:

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一次全部。我遇到的问题是我不知道如何处理输入。起初,我想我会用新行分割输入,并创建一个数组,其中每个索引是新行,但我现在只打印“ZZZZ”,我不明白为什么?我也不知道如何去比较第一个索引(答案键)和所有学生的答案。一旦我通过换行符分割输入,我可以将空间中的每个索引分割出来吗?任何帮助是极大的赞赏!请记住我对Java很新。

我有什么至今(我知道它的不是很多,但我只是卡住右前面)....

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);  
} 

}

回答

2

使用此代码中的main()

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(); 
    }  

} 

我已经离开了的平均部件为您计算。不要提。

+0

这是一个索引越界异常....此外,如果我们在while循环中使用Break,而不是开玩笑,那么我的教授会逐字地标出分数。 – NoobCoderChick

+0

这工作正常!并且来到男人你可以修改if逻辑来消除中断。什么行oyu获得例外? – 2015-10-06 05:23:03

+0

此行.... \t if(inputArr [i + 3] .equalsIgnoreCase(answers [i + 1])) – NoobCoderChick

1

我只是在这里输入这个代码,所以有可能是拼写错误,你应该验证它。但这是一种做法。

如果使用ArrayList存储学生的详细信息,则不需要知道学生的数量。 ArrayList的大小是动态的。

http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

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... 
    } 
} 
+0

哦,这是有道理的答案是第一个输入。我还没有学习ArrayLists,但我会检查出来。任何想法为什么我没有被新行分裂成单独的元素? – NoobCoderChick

+0

你能澄清你的意思吗? –

+0

如果你的意思是string.split,看看这个教程 - http://examples.javacodegeeks.com/core-java/lang/string/java-string-split-example/ –

相关问题