微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

由于“无法从 void 转换为 int”错误,Java 代码无法工作

如何解决由于“无法从 void 转换为 int”错误,Java 代码无法工作

我对在 uni 学习 Java 基础知识非常陌生,我正在开展一个项目,为每个玩家返回 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);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。