Java 骰子游戏模拟中类内的组织方法

Java 骰子游戏模拟中类内的组织方法,java,syntax,brackets,Java,Syntax,Brackets,我有人帮我编写了这个模拟骰子游戏的程序,但由于语法/括号的原因,我似乎无法让它运行。我想我在课堂上组织我的方法有困难。有什么建议吗 import java.util.Random; public class diceGame { private class DiceRoll { public static Random rng = new Random(); private int a, b, c; //rolls publi

我有人帮我编写了这个模拟骰子游戏的程序,但由于语法/括号的原因,我似乎无法让它运行。我想我在课堂上组织我的方法有困难。有什么建议吗

import java.util.Random;

public class diceGame 
{
    private class DiceRoll
    {
        public static Random rng = new Random();
        private int a, b, c; //rolls

        public DiceRoll()
        {
            this.a = rng.nextInt(6);
            this.b = rng.nextInt(6);
            this.c = rng.nextInt(6);
        }

        public int getScore()
        {
            if (a ==6 && b == 6 && c ==6)
            {
                return 500;
                System.out.println("Congratulations! You rolled three 6s! You just earned 500 points!!");
            }
            else if ((a == 6 && b == 6 && b != c) || (b == 6 && c == 6 && c !=a))
            {
                return 100;
                System.out.println("Congratulations! You rolled two 6s! You just earned 100 points!!");
            }
            else if ((a == b && b != c) || (b == c && c !=a))
            {
                return 50;
                System.out.println("Congratulations! You just earned 50 points!!");
            }
            else 
            {
                System.out.println("Nice Try!");
            }
        }

        public String toString()
        {
            return String.format("Rolled a %d, %d, and %d", a, b, c);
        }
    }

    public static void main (String args[])
    {
        DiceRoll d; 
        while (true)
        {
            d = new DiceRoll();
            d.getScore();        // gives you how much to change the player score
        }
        // finish client
    }
}
}

您的代码中有多个问题

  • 有些方法没有返回语句
  • 返回后几乎没有Sysout(这使得代码无法访问)
  • 父类内部类的概念是错误的。在内部类中,说到static,您只能有静态和final的常量
  • 括号是错的 下面的代码将起作用,并且只有在掷完所有骰子后才会终止

    import java.util.Random;
    
    public class diceGame {
        private class DiceRoll {
            public Random rng = new Random();
            private int a, b, c; // rolls
    
            public DiceRoll() {
                this.a = rng.nextInt(6);
                this.b = rng.nextInt(6);
                this.c = rng.nextInt(6);
            }
    
            public int getScore() {
                if (a == 6 && b == 6 && c == 6) {
                    System.out
                            .println("Congratulations! You rolled three 6s! You just earned 500 points!!");
                    return 500;
    
                } else if ((a == 6 && b == 6 && b != c)
                        || (b == 6 && c == 6 && c != a)) {
                    System.out
                            .println("Congratulations! You rolled two 6s! You just earned 100 points!!");
                    return 100;
    
                } else if ((a == b && b != c) || (b == c && c != a)) {
                    System.out
                            .println("Congratulations! You just earned 50 points!!");
                    return 50;
    
                } else {
                    System.out.println("Nice Try!");
                    return 0;
                }
            }
    
            public String toString() {
                return String.format("Rolled a %d, %d, and %d", a, b, c);
            }
        }
    
        public static void main(String args[]) {
            DiceRoll d;
            while (true) {
                d = new diceGame().new DiceRoll();
                if (d.getScore() == 500) { // gives you how much to change the player score
    
                    break;
                }
    
            }
            // finish client
        }
    }
    
    您需要解决所有这些问题,才能掷骰子。

    一看:

    if (a ==6 && b == 6 && c ==6){
      return 500;
      System.out.println("Congratulations! You rolled three 6s! You just earned 500 points!!"); //This is the error!
    }
    
    一旦从方法返回,任何其他代码都不会运行,并且会导致编译错误。我想这就是你想要的:

    if (a ==6 && b == 6 && c ==6) {
      System.out.println("Congratulations! You rolled three 6s! You just earned 500 points!!");
      return 500;
    }
    
    另外,看看这个:

    public static void main(String args[]) {
      DiceRoll d;
      while (true) {
        d = new DiceRoll();
        d.getScore(); // gives you how much to change the player score
      } // finish client
    }
    }
    } //Extra
    
    取下标有“额外”的卷曲大括号

    另外,Random
    rng
    是静态的,但它是在非静态类中声明的

    将其更改为
    私有静态类

    您的
    getScore()
    方法已损坏;它不返回任何内容

        public int getScore() {
            if (a == 6 && b == 6 && c == 6) {
                return 500;
                System.out.println("Congratulations! You rolled three 6s! You just earned 500 points!!");
            } else if ((a == 6 && b == 6 && b != c) || (b == 6 && c == 6 && c != a)) {
                return 100;
                System.out.println("Congratulations! You rolled two 6s! You just earned 100 points!!");
            } else if ((a == b && b != c) || (b == c && c != a)) {
                return 50;
                System.out.println("Congratulations! You just earned 50 points!!");
            } else {
                System.out.println("Nice Try!");
                //Wut no return?
            }
        }
    
    您可能需要:

      public int getScore() {
        if (a == 6 && b == 6 && c == 6) {
          System.out.println("Congratulations! You rolled three 6s! You just earned 500 points!!");
          return 500;
        } else if ((a == 6 && b == 6 && b != c) || (b == 6 && c == 6 && c != a)) {
          System.out.println("Congratulations! You rolled two 6s! You just earned 100 points!!");
          return 100;
        } else if ((a == b && b != c) || (b == c && c != a)) {
          System.out.println("Congratulations! You just earned 50 points!!");
          return 50;
        } else {
          System.out.println("Nice Try!");
          return -1;
        }
      }
    

    调用您的
    diceGame
    diceGame

    是个好主意,您的代码几乎没有问题。第一个是关于使用内部类的想法,这样内部类就应该退出。所有方法都可以转到外部类。将掷骰放在构造器中会使游戏变得无聊,a、b和c将在实例的生命周期内保持不变,或者强制您为每个掷骰创建新实例。非常糟糕的设计决策。我会将其移动到
    getScore
    。 最后,
    while(true)
    将永远持续下去,您必须杀死程序才能停止它。这是您的代码和检查评分逻辑,我不知道规则:

    import java.util.Random;
    
    public class diceGame {
      public static Random rng = new Random();
      private int a, b, c; //rolls
      public int getScore() {
        this.a = 1 + rng.nextInt(5);
        this.b = 1 + rng.nextInt(5);
        this.c = 1 + rng.nextInt(5);
        if (a == 6 && b == 6 && c == 6) {
          System.out.println("Congratulations! You rolled three 6s! You just earned 500 points!!");
          return 500;
        } else if ((a == 6 && b == 6 && b != c) || (b == 6 && c == 6 && c != a)) {
          System.out.println("Congratulations! You rolled two 6s! You just earned 100 points!!");
          return 100;
        } else if ((a == b && b != c) || (b == c && c != a) || (a == c && b != a)) {
          System.out.println("Congratulations! You just earned 50 points!!");
          return 50;
    
        } else {
          System.out.println("Nice Try!");
          return 0;
        }
      }
      public String toString() {
        return String.format("Rolled a %d, %d, and %d", a, b, c);
      }
      public static void main(String args[]) {
        diceGame d = new diceGame();
        for (int i = 0; i < 3; i++) {
          System.out.println(d.getScore());
          System.out.println(d.toString());
        }
      }
    }
    
    import java.util.Random;
    公营骰子游戏{
    公共静态随机rng=新随机();
    私有int a,b,c;//卷
    公共整数getScore(){
    此.a=1+rng.nextInt(5);
    此.b=1+rng.nextInt(5);
    此.c=1+rng.nextInt(5);
    如果(a==6&&b==6&&c==6){
    System.out.println(“祝贺你!你得了三个6分!你刚刚得了500分!!”;
    返回500;
    }else如果((a==6&&b==6&&b!=c)| |(b==6&&c==6&&c!=a)){
    System.out.println(“祝贺你!你得了两个6分!你刚刚得了100分!!”;
    返回100;
    }else如果((a==b&&b!=c)| |(b==c&&c!=a)| |(a==c&&b!=a)){
    System.out.println(“祝贺你!你刚刚得了50分!!”;
    返回50;
    }否则{
    System.out.println(“很好的尝试!”);
    返回0;
    }
    }
    公共字符串toString(){
    返回String.format(“a%d,%d和%d”,a,b,c);
    }
    公共静态void main(字符串参数[]){
    diceGame d=新的diceGame();
    对于(int i=0;i<3;i++){
    System.out.println(d.getScore());
    System.out.println(d.toString());
    }
    }
    }
    
    /me叹息。。。你能格式化你的代码吗?请?最后还有一个额外的
    }
    。您应该使用像eclipse这样的IDE来帮助您修复错误。仅供将来参考,在发布到此处之前,通过调试器运行您的代码。您可以从netbeans或其他源代码编辑工具打开您的.java文件并执行source->Format。它将安排大括号并格式化代码,使您更易于阅读。:)或者只需将您的java源代码发布到并按下按钮“美化”:)并非所有方法都需要返回!如果您不返回,它将运行所有代码,直到方法结束。仅此修复不会使骰子运行。