Java 骰子声明错误

Java 骰子声明错误,java,class,enums,dice,Java,Class,Enums,Dice,公共类记录骰子{ Dice2 myDice2; myDice2 = new Dice(); publicstaticvoidmain(字符串[]args){ //声明边和选择的变量 int i; 智力选择; //宣布骰子2 Dice2 myDice2; myDice2=新骰子(); //请求用户输入 System.out.println(“您希望您的骰子有多少面?”); 扫描仪sc=新的扫描仪(System.in); i=sc.nextInt(); 如果(i!=4)/| 6 | 8 | 12

公共类记录骰子{

Dice2 myDice2;
myDice2 = new Dice();
publicstaticvoidmain(字符串[]args){
//声明边和选择的变量
int i;
智力选择;
//宣布骰子2
Dice2 myDice2;
myDice2=新骰子();
//请求用户输入
System.out.println(“您希望您的骰子有多少面?”);
扫描仪sc=新的扫描仪(System.in);
i=sc.nextInt();
如果(i!=4)/| 6 | 8 | 12 | 20 | 100)
{
System.out.println(“您输入的号码不正确”);
}
其他的
{
//myDice2=新骰子(i);
}
//启动do语句
做{
系统输出打印(“1-重新掷骰子”);
系统输出打印(“2-获取值”);
系统输出打印(“4-获取最小值”);
系统输出打印(“请选择”);
//收集switch语句的选项
扫描仪s=新的扫描仪(System.in);
choice=s.nextInt();
//根据条目的不同情况启动switch语句
开关(选择){
案例“1”:
myDice2.reroll();
系统输出打印(“1-重新掷骰子”);
系统输出打印(“2-获取值”);
系统输出打印(“4-获取最小值”);
系统输出打印(“请选择”);
打破
案例“2”:
myDice2.getValue();
系统输出打印(“1-重新掷骰子”);
系统输出打印(“2-获取值”);
系统输出打印(“4-获取最小值”);
系统输出打印(“请选择”);
打破
/*案例“3”:
myDice.getMaxValue();
系统输出打印(“1-重新掷骰子”);
系统输出打印(“2-获取值”);
系统输出打印(“4-获取最小值”);
系统输出打印(“请选择”);
打破
案例“4”:
myDice.getMinValue();
系统输出打印(“1-重新掷骰子”);
系统输出打印(“2-获取值”);
系统输出打印(“4-获取最小值”);
系统输出打印(“请选择”);
*/
违约:
System.out.println(“输入的选项无效”);
}
//如果输入的选项值不正确,它将退出程序
}而(选择<0);
系统出口(0);
}   }   
java:9:错误:应为类、接口或枚举 Dice2 myDice2; ^ java:10:错误:应为类、接口或枚举 myDice2=新骰子(); ^ 2个错误

退出代码:1


当我试图编译带有上述两个错误的程序时。很抱歉,我的帖子太长,代码太业余,我只是个初学者。任何帮助都将不胜感激。

如果
Dice
没有
扩展
Dice2
,那么这将是编译时错误

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);
}   }   

在java中,必须在类内声明字段,不能在类外声明字段

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

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

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

Dice2
更改为
Dice
。我假设你确实在某个地方有
骰子
课程,而
骰子
没有扩展
骰子2
课程。否则,请发布
Dice
Dice2
类的完整代码。我说的是改变

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

更好

Dice myDice2;
myDice2 = new Dice();

我可以把代码粘贴到你的邮箱吗?因为我不能用我的方式发布dice2类代码?抱歉。@bringmethejava如果我的答案真的能帮助你得到正确的答案,请接受它,以供将来的读者阅读。
myDice2=new Dice()也许这应该是
new Dice2()
Dice myDice2;
myDice2 = new Dice();
Dice myDice2 = new Dice();