我最後一次獲得幫助的嘗試很少在我的部分中解釋過,因此我認爲我會再試一次並更清楚。從用戶輸入字符串中提取數據到數組中
我正在編寫一個程序,該程序應該讓用戶在一個班級中輸入考試次數,作業和測驗次數,然後在每個學生的所有任務上輸入分數。例如:
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進入數組。但是會自動發生嗎?
你怎麼看'的(布爾做= FALSE;完成=真;){ '是嗎?爲什麼? – 2014-11-22 04:18:23
當用戶在輸入數據後輸入「done」後,我將設置爲true並返回到循環的開頭。 – sam 2014-11-22 04:20:01
無論用戶輸入什麼內容,當測試循環條件'done'將被設置爲'true'並且副作用將被評估爲'true'。 'done = true'是賦值。 'done == true'或者'done'將會是一個更典型的'boolean'表達式。 – 2014-11-22 04:32:49