gpt4 book ai didi

java - 骰子声明错误

转载 作者:行者123 更新时间:2023-12-02 06:30:42 28 4
gpt4 key购买 nike

Dice2 myDice2;
myDice2 = new Dice();

公共(public)类 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:错误:需要类、接口(interface)或枚举 骰子2 myDice2; ^RecordDice.java:10:错误:需要类、接口(interface)或枚举 myDice2 = new Dice(); ^2 个错误

Exit code: 1

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

最佳答案

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

public class RecordDice {
public Dice2 myDice2 = new Dice();
<小时/>

看来你的继承是倒退的。如果没有实际看到 DiceDice2 的类声明,我无法确定,但命名约定表明 Dice2 继承自 骰子

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

关于java - 骰子声明错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20079098/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com