我有一个读取=
的程序我认为我的大部分内容都是正确的,但我遇到的主要问题是不了解如何使用Wrongs增加totalWrong全局变量!循环内部。在获取汇总方法以利用全局变量时也遇到问题。
package practicejoptionpanetwo;
import java.util.Random;
import javax.swing.JOptionPane;
public class PracticeJOptionPaneTwo
{
public static int loopCount = 0;
public static int totalWrong = 0;
public static void main(String[] args)
{
addition();
summary();
}
public static void addition()
{
int userAnswer, ranNum1, ranNum2, correctAnswer;
String strUserAnswer;
Random myRan = new Random();
int loopCount;
String strLoopCount;
strLoopCount = JOptionPane.showInputDialog("How many times do you wish to practice addition?");
loopCount = Integer.parseInt(strLoopCount);
for (int i = 0; i < loopCount; i++)
{
ranNum1 = myRan.nextInt(10)+1;
ranNum2 = myRan.nextInt(10)+1;
correctAnswer = ranNum1 + ranNum2;
JOptionPane.showMessageDialog(null, "Problem # " + (i+1) );
strUserAnswer = JOptionPane.showInputDialog("What is the sum of " + ranNum1 + " + " + ranNum2 + " =?");
userAnswer = Integer.parseInt(strUserAnswer);
if (userAnswer == correctAnswer)
{
JOptionPane.showMessageDialog (null,"Correct!");
}
else
{
JOptionPane.showMessageDialog (null, "Wrong.");
}
}
}
public static void summary()
{
JOptionPane.showMessageDialog(null, "There were " + loopCount + " number of problems generated.");
JOptionPane.showMessageDialog(null, "The total number of problems answered wrong were " + totalWrong);
JOptionPane.showMessageDialog(null, "The percentage wrong is " + ((double)totalWrong/(double)loopCount)+ 100 + " %");
}
}