2014-11-21 83 views
0

我們正在編寫一個名爲GradeBook的程序,它允許用戶輸入學生數據,例如名稱和不同成績項目的分數。 GradeBook爲每個學生提供選項計算等級並繪製班級成績分佈圖。用戶可以隨時返回並添加更多學生數據。它支持的學生的最大數量是200,一個類別中的最高成績項目數量是10.例如,它最多隻支持10個測驗,10個考試和10個家庭作業。使用數組的等級計算器

數據進入的一個例子是這樣的:

喬·史密斯W.:E100 E95 E87 Q10 Q10 Q8 H10 H10 H10

邁克爾·布朗:Q10 Q10 H7 H10 H10 H9 E80

要顯示學生成績和統計數據,我們應該使用System.out.printf。

下面是最終輸出應該是什麼的另一個例子。

Name  Exam Exam Exam Quiz Quiz Quiz HWork HWork HWork Grade 

Danny Devito 100.0 80.0 90.0 10.0 10.0 0.0 10.0 5.0 10.0 84.0 

Joe Smith 85.0 90.0 100.0 10.0 10.0 5.0 0.0 10.0 5.0  81.7 

Will Smith 60.0 100.0 90.0 10.0 10.0 8.0 10.0 0.0 10.0 82.0 

這最後一個例子是應該由小數更好地列隊和神韻:這樣看起來整潔乾淨,但我對如何做到這一點不確定。

import java.util.Scanner; 

公共類GradeCalcWithArrays {/ * *洛根韋格納的目的是計算 *進入等級 */ 公共靜態無效的主要(字串[] args){

Scanner s = new Scanner(System.in); 

    boolean done = false; 
    boolean quit = false; 
    int choice = 0; 

    int studentcounter = 0; 

    int[] examstats = new int[3]; /* 
           * Array created to store the information 
           * entered for exams 
           */ 
    int[] quizstats = new int[3]; /* 
           * Array created to store the information 
           * entered for quizzes 
           */ 
    int[] homeworkstats = new int[3]; /* 
            * Array created to store the 
            * information entered for homework 
            */ 

    String[] studentnames = new String[200]; /* 
              * Array created to store the 
              * student name information 
              * entered 
              */ 

    System.out.println("Welcome to GradeBook!"); 
    System.out.println("Please provide grade item details"); 

    System.out.print("Exams (number, points, weight):"); 

    examstats[0] = s.nextInt(); // inputs exam number 
    examstats[1] = s.nextInt(); // inputs exam points 
    examstats[2] = s.nextInt(); // inputs exam weight 

    System.out.print("Quizzes  (number, points, weight):"); 

    quizstats[0] = s.nextInt(); // inputs quiz number 
    quizstats[1] = s.nextInt(); // inputs quiz points 
    quizstats[2] = s.nextInt(); // inputs quiz weight 

    System.out.print("Homework (number, points, weight):"); 

    homeworkstats[0] = s.nextInt(); // inputs homework number 
    homeworkstats[1] = s.nextInt(); // inputs homework points 
    homeworkstats[2] = s.nextInt(); // inputs homework weight 

    System.out.println("--------------------"); 

    do { 
     System.out.println("What would you like to do?"); 
     System.out.println(" 1 Add student data"); 
     System.out.println(" 2 Display student grades & statistics"); 
     System.out.println(" 3 Plot grade distribution"); 
     System.out.println(" 4 Quit"); 
     System.out.print("Your choice:"); 
     choice = s.nextInt(); /* 
          * Choice will determine what the next course of 
          * action will be with the program 
          */ 

     if (choice == 1) { 
      System.out.println("Enter student data:"); 
      for (int i = 0; i <= 200; i++) { 
       studentcounter = studentcounter + 1; 
       System.out.print("Data>"); 
       studentnames[i] = s.nextLine(); 

       if (studentnames[i].equals("done")) { 
        break; 
       } 
      } 
     } 

     if (choice == 2) { 

     } 

     if (choice == 3) { 

     } 

     if (choice == 4) { 
      quit = true; 
      System.out.println("Good bye!"); 
     } 

    } while (quit == false); 

} 

} 

最大我堅持的部分只是能夠輸入數據並將其放入字符串和數組中。我不確定如何使用e100 q100 h100輸入的數據,因爲它們可能會混淆並且不按順序。我真的非常感謝這方面的幫助。先謝謝你們。

回答

0

試着將你的問題與你實際上困惑的事情隔離開來。通常這會導致你找出你需要做的事情。

看起來你真的只是不確定如何將exam,quizhomework字符串分成3個獨立的數組,因爲它們沒有任何順序。那麼,要分開它們,它們必須有分歧。這三種字符串有什麼區別?

所有3種類型的字符串輸入都可以包含0到100之間的某個數字,並附上一個字母:'e','q'或'h'。所以唯一的區別是每個字符串的第一個字母。因此,要將所有不同的字符串分隔成不同的數組,請檢查第一個字母是什麼,然後將其放入相應的數組中。

現在你終於到了需要編碼的地步了:你如何根據字母的第一個字母來編程分隔字符串?

那麼,你有if陳述,我相信你可以弄清楚如何獲得一個字符串的第一個字符。

在口頭上擺開(又名僞代碼):

String s; 
Char c; 

c = //get the first letter of string 's'. Figure this out with a simple google search. 

if (c == 'e') 
{ 
    //Add the string after the first character in string s to the Exam array 
    //NOTE: You have int arrays, so you will need to convert the string to an int 
    //  before adding it to the array. 
} 
else if (c == 'q') 
{ 
    //Same as above with Quiz array 
} 
else if ... 
{ 
    //... you should be able to finish this! 
} 

這應該讓你開始你的項目。嘗試模擬上面詳細介紹的思考過程,以便您可以快速發現在初學者級別遇到的每個問題都已經得到解決;你只需要找出你的問題在編程方面具體是什麼。

+0

謝謝你的幫助。這絕對是我的問題的90%。我想我知道你的意思,而不是做s.firstLetter的事情。你是說我應該使用split來挑選e的,然後我可以將不同的分數輸入到正確的數組中?或者我應該試圖使用子字符串來查找e的,然後在正確的數組中輸入我的成績? – Daniel 2014-11-21 01:53:39

+0

@Daniel無論對你來說最有意義。你可以用很多不同的方式來做到這一點。我不想告訴你如何做到這一點,但如果你閱讀我上面發佈的代碼,你可以分析它,並找出我的思維過程和意圖(主要是通過'if'塊)。 – Matthew 2014-11-21 02:01:00

+0

給出的例子是'Joe Smith:e100 e80 e90 q10 q10 q0 h10 h5 h10'開始J是序列的第一個字符,並且有多個e,q和h。你解釋如何去解決這個問題的方式只需要字符正確?我很困惑,我將如何找到3 e或者其中會有多少,然後將它們的所有值放入一個數組中。 – Daniel 2014-11-21 02:06:43