【问题标题】:Dice declaration error骰子声明错误
【发布时间】:2013-12-03 10:50:08
【问题描述】:
Dice2 myDice2;
myDice2 = new Dice();

公共类 RecordDice {

public static void main  (String [] args) {
//Declaring the variables of sides and choice
int i;
int choice;
//Declaring the dice2 

Dice2 myDice2;
myDice2 = new Dice();   
//Asking the user for input
System.out.println("How many sides do you want your dice to have?");
Scanner sc = new Scanner(System.in);
i = sc.nextInt();

if (i!= 4 )//||  6|| 8|| 12|| 20|| 100)
{
    System.out.println("You have entered an incorrect number");
}
else 
{
    //myDice2= new Dice(i);
}

//Starting the do statement
    do {
    System.out.print("1- Reroll the dice");
    System.out.print("2- Get the value");
    System.out.print("4- Get the minimum");
    System.out.print("Please make a choice");


    //Gathering the choice for the switch statement
    Scanner s= new Scanner (System.in);
    choice = s.nextInt();

//Starting the switch statement with varying cases dependent on entry   
switch(choice){
    case '1':
        myDice2.reroll();
    System.out.print("1- Reroll the dice");
    System.out.print("2- Get the value");
    System.out.print("4- Get the minimum");
    System.out.print("Please make a choice");

    break;

    case '2':
        myDice2.getValue();
    System.out.print("1- Reroll the dice");
    System.out.print("2- Get the value");
    System.out.print("4- Get the minimum");
    System.out.print("Please make a choice");
    break;

    /*case '3':
        myDice.getMaxValue();
    System.out.print("1- Reroll the dice");
    System.out.print("2- Get the value");
    System.out.print("4- Get the minimum");
    System.out.print("Please make a choice");
    break;

    case '4':
        myDice.getMinValue();
    System.out.print("1- Reroll the dice");
    System.out.print("2- Get the value");
    System.out.print("4- Get the minimum");
    System.out.print("Please make a choice");
    */
    default:
        System.out.println("Invalid choice entered");
}

//If the choice entered isn't the right value it exits the program
}while( choice < 0);

        System.exit(0);
}   }   

RecordDice.java:9:错误:需要类、接口或枚举 骰子2我的骰子2; ^ RecordDice.java:10:错误:需要类、接口或枚举 myDice2 = 新骰子(); ^ 2 个错误

退出代码:1

当我尝试编译程序时出现上述两个错误。对不起,很长的帖子和业余代码,我只是一个初学者。任何帮助将不胜感激。

【问题讨论】:

  • myDice2 = new Dice(); 也许应该写成new Dice2()

标签: java class enums dice


【解决方案1】:

在java中,你必须在一个类内部声明字段,你不能在外部声明它们

public class RecordDice {
    public Dice2 myDice2 = new Dice();

你的继承似乎是倒退的。在没有实际看到 DiceDice2 的类声明的情况下,我无法确定,但命名约定表明 Dice2 继承自 Dice

public class RecordDice {
    public Dice myDice2 = new Dice2();

【讨论】:

    【解决方案2】:

    Dice2 更改为Dice。我假设您在某处确实有 Dice 课程,而Dice 没有 extends Dice2 课程。否则请发布DiceDice2 类的完整代码。我说的是改变

    Dice2 myDice2;
    myDice2 = new Dice();
    

    Dice myDice2;
    myDice2 = new Dice();
    

    更好

    Dice myDice2 = new Dice();
    

    【讨论】:

    • 我可以粘贴你的代码吗,因为我无法解决发布 dice2 类代码的问题?对不起。
    • @bringmethejava 如果我的回答真的能帮助你得到正确的答案,请采纳给未来的读者。
    【解决方案3】:

    如果Dice 没有extends Dice2 则会出现编译时错误。

     Dice2 myDice2 = new Dice2();// This is right.
     Dice2 myDice2 = new Dice();// This is wrong
    

    【讨论】:

      猜你喜欢
      • 2017-02-06
      • 2015-02-20
      • 2014-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多