我正在开发一款游戏,玩家在其中进行随机类型的基本算术问题。当游戏结束时显示结果时,它将显示一条消息:“您以%d次尝试总共回答了%d个问题,您的得分为%.1f %%”。但是,当用户输入的值不是其期望的问题数量的整数时,每次尝试的次数和问题的数量都会不同。问题的数量和尝试的数量始终是相同的,因此它将始终输出100%的分数。相反,当用户第一次输入整数时,系统会提示他们输入要回答的问题数,如果他们回答错误甚至一次,则游戏结束时百分比分数的输出将为“ 0.0%”。
我尝试使用try块内的try块捕获NumberFormatException,因为只有在它们输入的值不是整数时,才会发生这种情况。
public static void randomArithmeticGame()
{
//RNG
int randNum;
boolean isValidInput = false;
boolean isInRange = false;
JOptionPane.showMessageDialog(null, "Welcome to the random arithmetic test", "Welcome", JOptionPane.INFORMATION_MESSAGE);
Addition a;
Subtraction s;
Multiplication m;
int ans;
while(!isValidInput)
{
try
{
int numberOfQuestions = Integer.parseInt(JOptionPane.showInputDialog(null, "How many questions would you like? (-1 to return to main menu)", "How many questions", JOptionPane.INFORMATION_MESSAGE));
while(!isInRange)
{
if(numberOfQuestions == -1)
{
//get out of loop if user enters -1 and go to bottom of block
break;
}
//do this while the number is not in range
if(numberOfQuestions > 10 || numberOfQuestions < 1)
{
//tell user that the number entered is out of range
JOptionPane.showMessageDialog(null, "Number of questions is not between 1 and 10, Please try again.", "ERROR", JOptionPane.INFORMATION_MESSAGE);
try
{
numberOfQuestions = Integer.parseInt(JOptionPane.showInputDialog(null, "How many questions would you like? (-1 to return to main menu)"));
}
catch(NumberFormatException exception)
{
//catch exception
JOptionPane.showMessageDialog(null, "Number entered was not an integer. Please try again.", "ERROR", JOptionPane.INFORMATION_MESSAGE);
}
}
else
{
//if number is in range then break out of loop
isInRange = true;
isValidInput = true;
}
}
for(int i = 0; i < numberOfQuestions; i++)
{
randNum = (int)(Math.random()*3 + 1);
if(randNum == 1)
{
//generate addition if rng is 1
a = new Addition();
ans = a.getAnswer();
a.checkAnswer(ans);
}
if(randNum == 2)
{
//generate subtraction if rng is 2
s = new Subtraction();
ans = s.getAnswer();
s.checkAnswer(ans);
}
if(randNum == 3)
{
//generate multiplication if rng is 3
m = new Multiplication();
ans = m.getAnswer();
m.checkAnswer(ans);
}
}
}
catch(NumberFormatException exception)
{
//catch exception
JOptionPane.showMessageDialog(null, "Number entered was not an integer. Please try again.", "ERROR", JOptionPane.INFORMATION_MESSAGE);
}
}
int finalScoreQuestionNum = Addition.getNumberOfAdditionQuestions() + Subtraction.getNumberOfSubtractionQuestions() + Multiplication.getNumberOfMultiplicationQuestions();
int finalScoreResponseNum = Addition.getNumberOfResponses() + Subtraction.getNumberOfResponses() + Multiplication.getNumberOfResponses();
double finalScorePercentage = (finalScoreQuestionNum/finalScoreResponseNum)*100.0;
String finalScoreMessage = String.format("You answered %d questions with %d total attempts, your score is %.1f%%", finalScoreQuestionNum, finalScoreResponseNum, finalScorePercentage);
//show user how mantt questions they did, number of attempts they made, as well as their final score and percentage
JOptionPane.showMessageDialog(null, finalScoreMessage, "Results", JOptionPane.INFORMATION_MESSAGE);
}
输出应显示用户提出的问题数量,他们尝试的次数及其百分比得分。但是,只有当用户在游戏开始时输入的问题数没有输入整数时,尝试的百分比和尝试次数才相同。
答案 0 :(得分:-1)
问题是您将int
除以finalScoreQuestionNum/finalScoreResponseNum
。
如果某个值0