Java 代码因 "Cannot convert from void to int" 错误而无法运行
Java code not working due to "Cannot convert from void to int" error
我刚开始在大学学习 java 的基础知识,并且正在研究一个项目,每个玩家 return 6 个随机骰子,但我在最后一行遇到了这个错误“resultRoll = JOptionPane ...”任何帮助将不胜感激。
public class Dice {
public static void main(String[] args){
}
Dice () {
Component f = new JFrame();
Random rnd = new Random();
String resultRoll;
// values set 1 to 6
int n1 = 1 + rnd.nextInt(7-1);
int n2 = 1 + rnd.nextInt(7-1);
int n3 = 1 + rnd.nextInt(7-1);
int n4 = 1 + rnd.nextInt(7-1);
int n5 = 1 + rnd.nextInt(7-1);
int n6 = 1 + rnd.nextInt(7-1);
resultRoll = JOptionPane.showMessageDialog(f, "Yay! You rolled: " + n1 + n2 + n3 + n4 + n5 + n6);
}
}
您可能想在显示消息之前设置 resultRoll
。
resultRoll = n1 + " " + n2 + " " + n3 + " " + n4 + " " + n5 + " " + n6;
JOptionPane.showMessageDialog(f, "Yay! You rolled: " + resultRoll);
我刚开始在大学学习 java 的基础知识,并且正在研究一个项目,每个玩家 return 6 个随机骰子,但我在最后一行遇到了这个错误“resultRoll = JOptionPane ...”任何帮助将不胜感激。
public class Dice {
public static void main(String[] args){
}
Dice () {
Component f = new JFrame();
Random rnd = new Random();
String resultRoll;
// values set 1 to 6
int n1 = 1 + rnd.nextInt(7-1);
int n2 = 1 + rnd.nextInt(7-1);
int n3 = 1 + rnd.nextInt(7-1);
int n4 = 1 + rnd.nextInt(7-1);
int n5 = 1 + rnd.nextInt(7-1);
int n6 = 1 + rnd.nextInt(7-1);
resultRoll = JOptionPane.showMessageDialog(f, "Yay! You rolled: " + n1 + n2 + n3 + n4 + n5 + n6);
}
}
您可能想在显示消息之前设置 resultRoll
。
resultRoll = n1 + " " + n2 + " " + n3 + " " + n4 + " " + n5 + " " + n6;
JOptionPane.showMessageDialog(f, "Yay! You rolled: " + resultRoll);