Java JoptionPane问题
发布网友
发布时间:2022-06-24 18:16
我来回答
共1个回答
热心网友
时间:2023-10-11 22:11
变量的作用域问题,将 output 变量的声明放在 if 外面就行了
package chapter3;
import javax.swing.JOptionPane;
public class AddtionQuizUsingInputDialog {
public static void main(String[] args) {
int num1 = (int)(System.currentTimeMillis()%10);
int num2 = (int)(System.currentTimeMillis()*3%10);
String output;
String answerString = JOptionPane.showInputDialog("What is " + num1 + "+" + num2 + "?");
int answer=Integer.parseInt(answerString);
if(answer == num1+num2) {
output = "You are right";
}
else {
output = "You are wrong";
}
JOptionPane.showMessageDialog(null,output);
}
}