这并不复杂,我只需要知道我的错误。我是这个程序来显示骰子和滚动数字。在那之后,我希望它添加它们,如果它们超过15就赢了,如果不是它的损失。如果有人想知道这是什么,这个程序只是我们在课堂上使用的一本书的一个小练习。我正在接收非法的表达式,并在解析时达到了结束。
public class BradySkuza36
{
public static void main( String[] args )
{
int roll1, roll2, roll3, roll4, roll5;
roll1 = 1 + (int)(Math.random()*6);
roll2 = 1 + (int)(Math.random()*6);
roll3 = 1 + (int)(Math.random()*6);
roll4 = 1 + (int)(Math.random()*6);
roll5 = 1 + (int)(Math.random()*6);
System.out.println("\nYou rolled: " + roll1 + " " + roll2 + " " + roll3 + " " + roll4 + " " + roll5);
showDice(roll1);
showDice(roll2);
showDice(roll3);
showDice(roll4);
showDice(roll5);
if(rollAdd >= 15)
{
System.out.println("You win!);
}
else
{
System.out.println("Sorry maybe next time.");
}
}
public static void showDice( int roll )
{
System.out.println("+ +");
if ( roll == 1 )
{
System.out.println("| |");
System.out.println("| o |");
System.out.println("| |");
}
else if ( roll == 2 )
{
System.out.println("|o |");
System.out.println("| |");
System.out.println("| o|");
}
else if ( roll == 3 )
{
System.out.println("|o |");
System.out.println("| o |");
System.out.println("| o|");
}
else if ( roll == 4 )
{
System.out.println("|o o|");
System.out.println("| |");
System.out.println("|o o|");
}
else if ( roll == 5 )
{
System.out.println("|o o|");
System.out.println("| o |");
System.out.println("|o o|");
}
else if ( roll == 6 )
{
System.out.println("|o o|");
System.out.println("|o o|");
System.out.println("|o o|");
}
System.out.println("+ +");
}
public static void rollAdd(int added)
{
added = roll1 + roll2 + roll3 + roll4 + roll5;
}
}
}
答案 0 :(得分:0)
我做了你的功课
public class Help {
public static void main(String[] args) {
int roll1, roll2, roll3, roll4, roll5;
roll1 = 1 + (int) (Math.random() * 6);
roll2 = 1 + (int) (Math.random() * 6);
roll3 = 1 + (int) (Math.random() * 6);
roll4 = 1 + (int) (Math.random() * 6);
roll5 = 1 + (int) (Math.random() * 6);
System.out.println("\nYou rolled: " + roll1 + " " + roll2 + " " + roll3
+ " " + roll4 + " " + roll5);
showDice(roll1);
showDice(roll2);
showDice(roll3);
showDice(roll4);
showDice(roll5);
int rollAdd = roll1 + roll2 + roll3 + roll4 + roll5;
if (rollAdd >= 15) {
System.out.println("You win!");
} else {
System.out.println("Sorry maybe next time.");
}
}
public static void showDice(int roll) {
System.out.println("+ +");
if (roll == 1) {
System.out.println("| |");
System.out.println("| o |");
System.out.println("| |");
} else if (roll == 2) {
System.out.println("|o |");
System.out.println("| |");
System.out.println("| o|");
} else if (roll == 3) {
System.out.println("|o |");
System.out.println("| o |");
System.out.println("| o|");
} else if (roll == 4) {
System.out.println("|o o|");
System.out.println("| |");
System.out.println("|o o|");
} else if (roll == 5) {
System.out.println("|o o|");
System.out.println("| o |");
System.out.println("|o o|");
} else if (roll == 6) {
System.out.println("|o o|");
System.out.println("|o o|");
System.out.println("|o o|");
}
System.out.println("+ +");
}
}
System.out.println();
遗漏了"
rollAdd
rollAdd()
方法中定义的所有变量均未声明}
答案 1 :(得分:0)
rollAdd
函数的类中定义它们才能使用它们。此致,我建议您重新审视您的编程主题。
答案 2 :(得分:0)
您的代码中存在一些小问题。
首先,你最后还有一个}
。移动它。其次,使用rolladd
致电rolladd(roll1,roll2,roll3,roll4,roll5)
并将方法更改为
public static int rollAdd(int roll1,int roll2,int roll3,int roll4,int roll5)
{
int added = roll1 + roll2 + roll3 + roll4 + roll5;
return added;
}
最后,您错过了<{p>中的"
System.out.println("You win!);