我有代码要求用户输入5个测试分数,然后计算5个测试分数的平均值。代码编译并运行得很好,但我的问题是:我的代码输出的对话框多于必要的。我想尝试找到一种方法来一次显示输出或使用较少的对话框。我是Java的新手,所以我还在学习。如果您运行代码,您将更好地了解我想要获得的内容。
更新:有没有人知道为什么0.0级会出现在我的结果窗口中?什么是摆脱它的最好方法?
import javax.swing.JOptionPane; //Needed for GUI
import java.text.DecimalFormat; //needed to format the Output
public class DTTestAverageAndGrade
{//Begin class
public static void main(String[] args)
{//Begin main method
String inputString; //For reader's input
double Score1, //Define score1
Score2, //Define score2
Score3, //Define score3
Score4, //Define score4
Score5, //Define score5
Average ;//Define Average
DecimalFormat formatter = new DecimalFormat("#,##0.0"); //format the scores
// Get the five scores
inputString=
JOptionPane.showInputDialog("Enter the First Test Score: "); //ask user to enter the first test score
Score1 = Double.parseDouble(inputString);
inputString=
JOptionPane.showInputDialog("Enter the Second Test Score: ");//ask user to enter the second test score
Score2 = Double.parseDouble(inputString);
inputString=
JOptionPane.showInputDialog("Enter the third test score: ");//ask user to enter the third test score
Score3 = Double.parseDouble(inputString);
inputString=
JOptionPane.showInputDialog("Enter the fourth test score: ");//ask user to enter the fourth test score
Score4 = Double.parseDouble(inputString);
inputString=
JOptionPane.showInputDialog("Enter the fifth test score: ");//ask user to enter the fifth test score
Score5 = Double.parseDouble(inputString);
// Call to method calcAverage and output the 5 test average
Average = calcAverage(Score1, Score2, Score3, Score4, Score5);
// Display Average test Score and Determine the letter grade for each test and call to determineGrade
JOptionPane.showMessageDialog(null, "\t\nYour Score 1 is : " +formatter.format(Score1) +"\t Grade: " + determineGrade(Score1)
+ "\t\nYour Score 2 is : " +formatter.format(Score2) +"\t Grade: " + determineGrade(Score2)
+ "\t\nYour Score 3 is : " +formatter.format(Score3) +"\t Grade: " + determineGrade(Score3)
+ "\t\nYour Score 4 is : " +formatter.format(Score4) +"\t Grade: " + determineGrade(Score4)
+ "\t\nYour Score 5 is : " +formatter.format(Score5) +"\t Grade: " + determineGrade(Score5)
+ "\t\nYour Average is : " +formatter.format(Average) +"\t Grade: " + determineGrade(Average),
"\tYour Test Results",JOptionPane.INFORMATION_MESSAGE);
}//end Main method
// Calculate the average of the five test scores
public static double calcAverage(double Score1, double Score2, double Score3, double Score4, double Score5)
{
double Average = ((Score1 + Score2 + Score3 + Score4 + Score5) / 5);
return Average;
}
// Determine the letter grade for the average and 5 test scores
public static double determineGrade(double Average)
{
char grade; // Define grade
// Determine letter grade
if (Average>=90)
{
grade = 'A';
JOptionPane.showMessageDialog(null, grade + "\n");
}
else if (Average>=80)
{
grade = 'B';
JOptionPane.showMessageDialog(null, grade + "\n");
}
else if (Average>=70)
{
grade = 'C';
JOptionPane.showMessageDialog(null, grade + "\n");
}
else if (Average>=60)
{
grade = 'D';
JOptionPane.showMessageDialog(null, grade + "\n");
}
else if (Average<60)
{
grade = 'F';
JOptionPane.showMessageDialog(null, grade + "\n");
}
else
{
JOptionPane.showMessageDialog(null, "error\n");
}
return 0;
}//end determinegrade method
}//end class
答案 0 :(得分:1)
您可以输入一个字符串并拆分。 例如:
String inputString=
JOptionPane.showInputDialog("Enter 5 test Scores with comma: "); //ask user to enter the 5 test scores at once
String[] scores = inputString.split(",");
score1 = scores[0];
score2 = scores[1];
score3 = scores[2];
score4 = scores[3];
score5 = scores[4];
答案 1 :(得分:1)
您几乎可以在Object
showInputDialog
参数中添加任何内容。很可能你会想要添加一个面板,其中包含你需要的任何位,例如:
JTextField score1 = new JTextField(10);
JTextField score2 = new JTextField(10);
JTextField score3 = new JTextField(10);
JTextField score4 = new JTextField(10);
JTextField score5 = new JTextField(10);
JPanel scorePanel = new JPanel();
scorePanel.add(new JLabel("First Test Score:"));
scorePanel.add(score1);
scorePanel.add(Box.createHorizontalStrut(15)); // a spacer
scorePanel.add(new JLabel("Second Test Score:"));
scorePanel.add(score2);
scorePanel.add(Box.createHorizontalStrut(15));
scorePanel.add(new JLabel("Third Test Score:"));
scorePanel.add(score3);
scorePanel.add(Box.createHorizontalStrut(15));
scorePanel.add(new JLabel("Fourth Test Score:"));
scorePanel.add(score4);
scorePanel.add(Box.createHorizontalStrut(15));
scorePanel.add(new JLabel("Fifth Test Score:"));
scorePanel.add(score5);
int result = JOptionPane.showInputDialog(null, scorePanel,
"Please enter all 5 test scores.");
double Score1 = Double.parseDouble(score1.getText());
double Score2 = Double.parseDouble(score2.getText());
... etc.
然后像你一样计算和显示结果。