2014-11-22 56 views
0

我最後一次獲得幫助的嘗試很少在我的部分中解釋過,因此我認爲我會再試一次並更清楚。從用戶輸入字符串中提取數據到數組中

我正在編寫一個程序,該程序應該讓用戶在一個班級中輸入考試次數,作業和測驗次數,然後在每個學生的所有任務上輸入分數。例如:

Please provide grade item details 

Exams (number, points, weight):3 100 60 

Quizzes (number, points, weight):3 10 20 

Homework (number, points, weight):3 10 20 

What would you like to do? 

1 Add student data 

2 Display student grades & statistics 

3 Plot grade distribution 

4 Quit 

Your choice: 1 

Enter student data: 

data>Bob Jones: e100 e80 e90 q10 q10 q0 h10 h5 h10 

data>Joe Smith: e85 e90 e100 q10 q10 q5 h0 h10 h5 

data>Sam Johnson: q10 h10 h0 h10 e60 e100 e90 q10 q8 

我的主要問題是,我與如何將數據的一條線分成四個不同的陣列中掙扎:一個名稱,一個是考試成績,一爲測驗分數和一個功課成績。

我試圖做了這一點如下所示:

if(input == 1){ 



     for(int i = 0; i <= students;){ 
      for (boolean done = false;done == true;){ 
       System.out.println("Enter student data: ");  
       System.out.print("data>"); 

       String[] readInput = s.next().split(":"); 

       String name = readInput[0];  

       String scoreString = readInput[1]; 

       String [] scores = scoreString.split("\\s+"); 

       char type = scores[0]; 
       String inputscore = scores.substring(1); 
       int score = Integer.valueOf(inputscore); 

       if (type == 'e'){ 

那麼,我試圖做的,拆分輸入行數據到名爲readInput一個數組,索引爲0作爲名稱和索引1是分數列表。然後,我創建了兩個字符串,一個叫做名字,一個叫做score,用來自索引0和1的數據。然後我的想法是在每個輸入分數(例如「e100」)之前讀取字符,以確定它應該進入哪個數組。我覺得我正在以一種毫無意義的方式來做這件事,而且可能有一種更簡單的方法來做到這一點,但是我在這裏試圖弄明白這一點。

我的其他問題是,當用戶未能輸入所有分數(例如,在課程中有3個考試,但用戶只輸入兩個考試分數),那些沒有輸入的分數將作爲0進入數組。但是會自動發生嗎?

+0

你怎麼看'的(布爾做= FALSE;完成=真;){ '是嗎?爲什麼? – 2014-11-22 04:18:23

+0

當用戶在輸入數據後輸入「done」後,我將設置爲true並返回到循環的開頭。 – sam 2014-11-22 04:20:01

+0

無論用戶輸入什麼內容,當測試循環條件'done'將被設置爲'true'並且副作用將被評估爲'true'。 'done = true'是賦值。 'done == true'或者'done'將會是一個更典型的'boolean'表達式。 – 2014-11-22 04:32:49

回答

0

首先,我認爲你需要一個輔助方法來獲得特定類型的得分的計數(S)。某些內容需要輸入String並返回char出現的次數。您可以使用String.toCharArray()和增強for-each loop得到計數一樣,

private static int countChar(String in, char v) { 
    int count = 0; 
    if (in != null) { 
     for (char ch : in.toCharArray()) { 
      if (ch == v) { 
       count++; 
      } 
     } 
    } 
    return count; 
} 

然後你可以使用正則表達式,例如\\D匹配單個非數字和\\d+比賽連續的數字。所以,你可以用它來創建一個Pattern,然後遍歷的grades陣列狀

String str = "Bob Jones: e100 e80 e90 q10 q10 q0 h10 h5 h10"; 
Pattern p = Pattern.compile("(\\D)(\\d+)"); 
String[] input = str.split("\\s*:\\s*"); // optional white space \\s* 
System.out.println("Name: " + input[0]); 
String[] grades = input[1].split("\\s+"); // one (or more) consecutive 
              // white space characters \\s+ 
int[] exams = new int[countChar(input[1], 'e')]; 
int[] quizs = new int[countChar(input[1], 'q')]; 
int[] homework = new int[countChar(input[1], 'h')]; 
int epos = 0, qpos = 0, hpos = 0; 
for (String grade : grades) { 
    Matcher m = p.matcher(grade); 
    if (m.matches()) { 
     String type = m.group(1); 
     if (type.equals("e")) { 
      exams[epos++] = Integer.parseInt(m.group(2)); 
     } else if (type.equals("q")) { 
      quizs[qpos++] = Integer.parseInt(m.group(2)); 
     } else if (type.equals("h")) { 
      homework[hpos++] = Integer.parseInt(m.group(2)); 
     } 
    } 
} 
System.out.println("Homework: " + Arrays.toString(homework)); 
System.out.println("Quizs: " + Arrays.toString(quizs)); 
System.out.println("Exams: " + Arrays.toString(exams)); 

輸出是

Name: Bob Jones 
Homework: [10, 5, 10] 
Quizs: [10, 10, 0] 
Exams: [100, 80, 90] 
1

你走在正確的道路上;想想你需要分手才能獲得所有不同的考試分數。一切似乎都被一個空間隔開了,爲什麼不再分開呢?

String[] scoreArray = scores.split(" "); 
for(String score : scoreArray) { 
    if(score.startsWith("e")) { 
     //add to exam array 
    } else if(score.startsWith("q")) { 
     //add to quiz array 
    } else if(score.startsWith("h")) { 
     //add to homework array 
    } 
} 

至於第二個問題,這些分數不會自動輸入爲0。您需要檢測給定的數組是否已滿(使用「數字」值作爲「成績項目詳細信息」的輸入)。如果沒有填滿,則用0填充餘下的部分。

+0

但是,一旦我用空格分割它,我會留下,例如:e100,e90,h10,h8,q10,q10等。我如何提取沒有前面字符的數字並將其放入數組中? – sam 2014-11-22 04:27:21

+0

假設你有「e100」從位置1到結束位置的子字符串,那麼你可以做Integer.parseInt(在該子字符串),或者Integer.valueOf(subString) – Vihar 2014-11-22 04:33:12

0

您可以使用此方法

if (input == 1){ 

     String name = readInput[0];     
     String scores = readInput[1]; 

     String s[]=scores.split("\\s+"); 


     examArray=0,harray=0; 
     for(int i=0;i<s.length();i++){ 
      if(s.startsWith("e")) { 
       string str=s.subString(1,s.length()); 
       exam[examArray++]=Integer.valueOf(str); 
      }else 
      { 
      // same for different cases 
      } 
    } 
} 
+0

好吧,所以我更新了OP中的代碼。通過使用空格分割scoreString創建分數數組後,現在我被困在如何檢查數組中的每個項目,如e100 e90 q10 q9,並將它們放入正確的數組中,如考試,作業或測驗數組。 – sam 2014-11-22 05:01:11

+0

更新我的回答 – Vihar 2014-11-22 06:03:32

+0

做了什麼工作? – Vihar 2014-11-22 10:09:20