Java 问答节目问题
我对Java相当陌生,我有一个任务,我必须创建5个问题和答案,并让用户回答。然后,程序应输出五个结果中的正确结果,以及百分比和反馈消息。我的代码如下Java 问答节目问题,java,Java,我对Java相当陌生,我有一个任务,我必须创建5个问题和答案,并让用户回答。然后,程序应输出五个结果中的正确结果,以及百分比和反馈消息。我的代码如下 package quiz; import javax.swing.*; public class Quiz { public static void main(String[] args) { int count = 0; final int totalQuestions = 5; J
package quiz;
import javax.swing.*;
public class Quiz {
public static void main(String[] args) {
int count = 0;
final int totalQuestions = 5;
JOptionPane.showMessageDialog(null, "Welcome to the Trivia Program!");
JOptionPane.showMessageDialog(null, "A series of questions will be asked."
+ "\nPlease enter the letter of the answer"
+ "\nyou think is the correct choice.");
String[][] question = {
{"What letter is used to represent the number 100 in Roman Numerals?"
+ "\nA. M"
+ "\nB. D"
+ "\nC. C"
+ "\nD. X", "c"
},
{"Triton is the moon to which planet?"
+ "\nA. Jupiter"
+ "\nB. Neptune"
+ "\nC. Saturn"
+ "\nD. Uranus", "b"
},
{"What are Lanthanides?"
+ "\nA. Elements of the periodic table"
+ "\nB. Mountains on Earth"
+ "\nC. Mountains on the moon"
+ "\nD. Bacterium", "a"
},
{"What does a nidoligist study?"
+ "\nA. waves"
+ "\nB. clouds"
+ "\nC. bird nests"
+ "\nD. caves", "c"
},
{"Hypermetropic people are what?"
+ "\nA. obese"
+ "\nB. underfed"
+ "\nC. moody"
+ "\nD. far sighted", "d"
}
};
String[] answers = new String[totalQuestions];
for (int x = 0; x < totalQuestions; x++) {
answers[x] = JOptionPane.showInputDialog("\t\t" + (x + 1) + ". " + question[x][0] + " ");
answers[x] = answers[x].toLowerCase();
if (question[x][1].equals(answers[x])) {
JOptionPane.showMessageDialog(null, "Correct!");
count++;
} else {
JOptionPane.showMessageDialog(null, "Incorrect");
}
}
int avg = (count / totalQuestions) * 100;
switch (count) {
case 3:
JOptionPane.showMessageDialog(null, "You got " + count + "/" + totalQuestions + " correct"
+ "(" + avg + "%)"
+ "\nAverage");
break;
case 4:
JOptionPane.showMessageDialog(null, "You got " + count + "/" + totalQuestions + " correct"
+ "(" + avg + "%)"
+ "\nVery Good");
break;
case 5:
JOptionPane.showMessageDialog(null, "You got " + count + "/" + totalQuestions + " correct"
+ "(" + avg + "%)"
+ "\nExcellent!");
break;
default:
JOptionPane.showMessageDialog(null, "You got " + count + "/" + totalQuestions + " correct "
+ "(" + avg + "%)"
+ "\nPoor");
break;
}
}
}
package测验;
导入javax.swing.*;
公开课测验{
公共静态void main(字符串[]args){
整数计数=0;
最终问题=5;
showMessageDialog(null,“欢迎使用琐事程序!”);
showMessageDialog(null)“将询问一系列问题。”
+“\n请输入答案的字母”
+“\n您认为这是正确的选择。”);
字符串[][]问题={
{“罗马数字中用什么字母表示数字100?”
+“\nA.M”
+“\nB.D”
+“\nC.C”
+“\nD.X”,“c”
},
{“Triton®声波风廓线仪是哪颗行星的卫星?”
+“\nA.朱庇特”
+“\nB.海王星”
+“\n.Saturn”
+“\n天王星”,“b”
},
{“什么是镧系元素?”
+“\n.元素周期表的元素”
+“\nB.地球上的山脉”
+“\n.月球上的山脉”
+“\n.细菌”,“a”
},
{“鸟巢学家研究什么?”
+“\n.waves”
+“\nB.云”
+“\n.鸟巢”
+“\n.洞穴”,“c”
},
{“远视的人是什么?”
+“\n.肥胖”
+“\nB.食物不足”
+“\n.moody”
+“\n.远视”,“d”
}
};
字符串[]答案=新字符串[totalQuestions];
对于(int x=0;x
我的问题是,当所有答案都正确(5/5)时,它只输出正确的平均值%。其他的,百分比是0。谁能帮我解释一下吗 您需要更改以下代码行:
int avg = (count / totalQuestions) * 100;
到
编辑:要获得类似XX.XX%的格式,您需要使用字符串格式化程序:
String.format("%,.2f", avg);
在Java中除以两个整数时,结果是一个整数。为了得到一个百分比,您可以像这样将整数转换成浮点数/双精度数
int avg = (int)((((double)count) / ((double)totalQuestions)) * 100.0)
将count&avg更改为float当我将count更改为float时,switch语句说我不能使用value float。当我将totalQuestions更改为float时,使用totalQuestions的新字符串对象表示无法从float转换为int。好的,我已经解决了!我在平均方程中的count和totalQuestion之前添加了(float)。好的,我这样做了,它工作了,但是当我得到3/5的时候,它的输出为60.000004%。我该如何解决这个问题?不知道如何在JOptionPane中执行此操作
int avg = (int)((((double)count) / ((double)totalQuestions)) * 100.0)