骰子声明错误

时间:2013-11-19 18:21:55

标签: java class enums dice

Dice2 myDice2;
myDice2 = new Dice();

public class 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:错误:预期的类,接口或枚举     Dice2 myDice2;     ^ RecordDice.java:10:错误:期望的类,接口或枚举     myDice2 = new Dice();     ^ 2个错误

  

退出代码:1

当我尝试使用上述两个错误编译程序时。对不起,长篇文章和业余代码我只是一个初学者。任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:1)

在java中,你必须在一个类中声明里面字段,你不能在外面对它们进行delcare

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

看起来好像你的继承是向后的。在没有真正看到DiceDice2的类声明的情况下,我无法确定,但命名约定表明Dice2继承自Dice

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

答案 1 :(得分:1)

Dice2更改为Dice。我假设您确实某个Dice班级而Dice extends Dice2班级。否则请发布DiceDice2课程的完整代码。我所说的是改变

Dice2 myDice2;
myDice2 = new Dice();

Dice myDice2;
myDice2 = new Dice();

更好

Dice myDice2 = new Dice();

答案 2 :(得分:0)

如果Dice没有extends Dice2,则编译时错误。

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