你不必阅读我所有的代码,不用担心。我的主要问题在此消息的正文中突出显示。
我对编程很陌生。我应该创建一个Java程序来读取字符串输入,将该字符串分成映射到数组的字符(该数组是用于回答多项选择问题的备忘录)。然后从用户读取另一个字符串输入,将该输入也分成数组中的字符,然后比较两个数组索引。基本上,它应该查看有多少数组索引是相同的,然后生成正确答案的分数(这是一个多项选择答案程序,因此字符串中的字符将是 AB 或 C)。
我必须存储学生(用户)的姓名并保留他的分数。但是程序会询问您有多少学生,然后运行一个循环询问姓名,并回答(与您输入的学生数量一样多次。最大为 10),然后将答案与备忘录进行比较,生成一个分数,并使用所有学生的答案计算一个平均分数。到目前为止,我已经设法创建了一个可以为一个用户执行此操作的程序。我遇到的问题是循环存储多个用户名和分数,以计算平均分数。
这是我的代码。如果您将其复制并粘贴到您的 IDE 中,您会发现它运行良好(除了一些错误处理和JOptionPane
对话框按钮内容遗漏)。但它只有在您输入用户数为 1 时才会起作用。其他任何东西,它都不会正常运行。那么如何让它为多个用户循环呢?
package multiquestions;
import javax.swing.JOptionPane;
public class MultiQuestions {
public static void main(String[] args) {
String aMemo = getInput0();
int aStudentNumber = getInput1();
String aStudentName = getInput2();
String aStudentAnswer = getInput3();
int aScore = CalcScore(aStudentAnswer, aMemo);
if (aStudentNumber == 1) {
StudentInfo(aStudentName, aScore);
}
System.exit(0);
}
public static String getInput0() {
String Memo = JOptionPane
.showInputDialog("Please enter the memo answers to the 10 multiple choice questions\nno spaces and all in Capital letters e.g.ABCDEABCDE");
return Memo;
}
public static int getInput1() {
int StudentNumber = Integer.parseInt(JOptionPane
.showInputDialog("Please enter the number of students in the class"));
return StudentNumber;
}
public static String getInput2() {
String StudentName = JOptionPane.showInputDialog("Please enter the student's name");
return StudentName;
}
public static String getInput3() {
String StudentAnswer = JOptionPane
.showInputDialog("Please enter the student's answers to the 10 multiple choice questions\nno spaces and all in Capital letters e.g.ABCDEABCDE");
return StudentAnswer;
}
public static int CalcScore(String bStudentAnswer, String bMemo) {
int One;
int Two;
int Three;
int Four;
int Five;
int Six;
int Seven;
int Eight;
int Nine;
int Ten;
char Reference[] = new char[10];
for (char Ref : Reference) {
Reference[0] = bMemo.charAt(0);
Reference[1] = bMemo.charAt(1);
Reference[2] = bMemo.charAt(2);
Reference[3] = bMemo.charAt(3);
Reference[4] = bMemo.charAt(4);
Reference[5] = bMemo.charAt(5);
Reference[6] = bMemo.charAt(6);
Reference[7] = bMemo.charAt(7);
Reference[8] = bMemo.charAt(8);
Reference[9] = bMemo.charAt(9);
}
char Answer[] = new char[10];
for (char Ans : Answer) {
Answer[0] = bStudentAnswer.charAt(0);
Answer[1] = bStudentAnswer.charAt(1);
Answer[2] = bStudentAnswer.charAt(2);
Answer[3] = bStudentAnswer.charAt(3);
Answer[4] = bStudentAnswer.charAt(4);
Answer[5] = bStudentAnswer.charAt(5);
Answer[6] = bStudentAnswer.charAt(6);
Answer[7] = bStudentAnswer.charAt(7);
Answer[8] = bStudentAnswer.charAt(8);
Answer[9] = bStudentAnswer.charAt(9);
}
/*
* Below is the list of if statements which add one to each variable (Variables One to Ten
* declared at top of this method) if the array characters match each other at their indeces
*/
if (Reference[0] == Answer[0]) {
One = 1;
} else {
One = 0;
}
if (Reference[1] == Answer[1]) {
Two = 1;
} else {
Two = 0;
}
if (Reference[2] == Answer[2]) {
Three = 1;
} else {
Three = 0;
}
if (Reference[3] == Answer[3]) {
Four = 1;
} else {
Four = 0;
}
if (Reference[4] == Answer[4]) {
Five = 1;
} else {
Five = 0;
}
if (Reference[5] == Answer[5]) {
Six = 1;
} else {
Six = 0;
}
if (Reference[6] == Answer[6]) {
Seven = 1;
} else {
Seven = 0;
}
if (Reference[7] == Answer[7]) {
Eight = 1;
} else {
Eight = 0;
}
if (Reference[8] == Answer[8]) {
Nine = 1;
} else {
Nine = 0;
}
if (Reference[9] == Answer[9]) {
Ten = 1;
} else {
Ten = 0;
}
int Score = One + Two + Three + Four + Five + Six + Seven + Eight + Nine + Ten;
return Score;
}
public static void StudentInfo(String bStudentName, int bScore) {
switch (bScore) {
/*
* Below is case stament for values of bScore(score returned from CalcScore() For each value of
* bScore it must do something specific
*/
case 10:
JOptionPane.showMessageDialog(null, bStudentName + " " + bScore + "/10 A Marvelous");
break;
case 9:
JOptionPane.showMessageDialog(null, bStudentName + " " + bScore + "/10 A Marvelous");
break;
case 8:
JOptionPane.showMessageDialog(null, bStudentName + " " + bScore + "/10 B Outstanding");
break;
case 7:
JOptionPane.showMessageDialog(null, bStudentName + " " + bScore + "/10 C Significant");
break;
case 6:
JOptionPane.showMessageDialog(null, bStudentName + " " + bScore
+ "/10 D Above Average");
break;
case 5:
JOptionPane.showMessageDialog(null, bStudentName + " " + bScore
+ "/10 E Barely Adequate");
break;
case 4:
JOptionPane.showMessageDialog(null, bStudentName + " " + bScore + "/10 F Fail");
break;
case 3:
JOptionPane.showMessageDialog(null, bStudentName + " " + bScore + "/10 F Fail");
break;
case 2:
JOptionPane.showMessageDialog(null, bStudentName + " " + bScore + "/10 F Fail");
break;
case 1:
JOptionPane.showMessageDialog(null, bStudentName + " " + bScore + "/10 F Fail");
break;
case 0:
JOptionPane.showMessageDialog(null, bStudentName + " " + bScore + "/10 F Fail");
}
}
public static void Exit() {
int exit = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit?");
if (exit == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
public static void CalcAverage() {}
}