我帮助编写了这个程序作为骰子游戏的模拟,但由于语法/括号情况,我似乎无法让它运行。我想我在课堂上组织我的方法时遇到了麻烦。有什么提示吗?
import java.util.Random;
public class diceGame
{
private class DiceRoll
{
public static Random rng = new Random();
private int a, b, c; //rolls
public DiceRoll()
{
this.a = rng.nextInt(6);
this.b = rng.nextInt(6);
this.c = rng.nextInt(6);
}
public int getScore()
{
if (a ==6 && b == 6 && c ==6)
{
return 500;
System.out.println("Congratulations! You rolled three 6s! You just earned 500 points!!");
}
else if ((a == 6 && b == 6 && b != c) || (b == 6 && c == 6 && c !=a))
{
return 100;
System.out.println("Congratulations! You rolled two 6s! You just earned 100 points!!");
}
else if ((a == b && b != c) || (b == c && c !=a))
{
return 50;
System.out.println("Congratulations! You just earned 50 points!!");
}
else
{
System.out.println("Nice Try!");
}
}
public String toString()
{
return String.format("Rolled a %d, %d, and %d", a, b, c);
}
}
public static void main (String args[])
{
DiceRoll d;
while (true)
{
d = new DiceRoll();
d.getScore(); // gives you how much to change the player score
}
// finish client
}
}
}
答案 0 :(得分:2)
一看:
if (a ==6 && b == 6 && c ==6){
return 500;
System.out.println("Congratulations! You rolled three 6s! You just earned 500 points!!"); //This is the error!
}
从方法返回后,将不会运行任何其他代码,这将导致编译错误。我想这就是你想要的:
if (a ==6 && b == 6 && c ==6) {
System.out.println("Congratulations! You rolled three 6s! You just earned 500 points!!");
return 500;
}
另外,看看这个:
public static void main(String args[]) {
DiceRoll d;
while (true) {
d = new DiceRoll();
d.getScore(); // gives you how much to change the player score
} // finish client
}
}
} //Extra
删除标有" Extra"。
的花括号此外,Random rng
是静态的,但它是在非静态类中声明的。
将其更改为private static class DiceRoll
你的getScore()
方法被禁止了;它没有返回任何东西。
public int getScore() {
if (a == 6 && b == 6 && c == 6) {
return 500;
System.out.println("Congratulations! You rolled three 6s! You just earned 500 points!!");
} else if ((a == 6 && b == 6 && b != c) || (b == 6 && c == 6 && c != a)) {
return 100;
System.out.println("Congratulations! You rolled two 6s! You just earned 100 points!!");
} else if ((a == b && b != c) || (b == c && c != a)) {
return 50;
System.out.println("Congratulations! You just earned 50 points!!");
} else {
System.out.println("Nice Try!");
//Wut no return?
}
}
你可能想要:
public int getScore() {
if (a == 6 && b == 6 && c == 6) {
System.out.println("Congratulations! You rolled three 6s! You just earned 500 points!!");
return 500;
} else if ((a == 6 && b == 6 && b != c) || (b == 6 && c == 6 && c != a)) {
System.out.println("Congratulations! You rolled two 6s! You just earned 100 points!!");
return 100;
} else if ((a == b && b != c) || (b == c && c != a)) {
System.out.println("Congratulations! You just earned 50 points!!");
return 50;
} else {
System.out.println("Nice Try!");
return -1;
}
}
最好拨打diceGame
班级DiceGame
答案 1 :(得分:1)
您的代码中存在多个问题
下面的代码可以工作,只有在所有骰子都滚动6时才会终止。
import java.util.Random;
public class diceGame {
private class DiceRoll {
public Random rng = new Random();
private int a, b, c; // rolls
public DiceRoll() {
this.a = rng.nextInt(6);
this.b = rng.nextInt(6);
this.c = rng.nextInt(6);
}
public int getScore() {
if (a == 6 && b == 6 && c == 6) {
System.out
.println("Congratulations! You rolled three 6s! You just earned 500 points!!");
return 500;
} else if ((a == 6 && b == 6 && b != c)
|| (b == 6 && c == 6 && c != a)) {
System.out
.println("Congratulations! You rolled two 6s! You just earned 100 points!!");
return 100;
} else if ((a == b && b != c) || (b == c && c != a)) {
System.out
.println("Congratulations! You just earned 50 points!!");
return 50;
} else {
System.out.println("Nice Try!");
return 0;
}
}
public String toString() {
return String.format("Rolled a %d, %d, and %d", a, b, c);
}
}
public static void main(String args[]) {
DiceRoll d;
while (true) {
d = new diceGame().new DiceRoll();
if (d.getScore() == 500) { // gives you how much to change the player score
break;
}
}
// finish client
}
}
您需要修复所有这些以便掷骰子。
答案 2 :(得分:1)
您的代码存在很少问题。第一个是关于使用内部类的想法,所以内部类应该出去。所有方法都可以转到外部类。将滚动放入构造函数会使游戏失败,a,b和c将在实例的生命周期内保持不变或强制您为每个卷创建新实例。真的很糟糕的设计决定。我会把它移到getScore
。
最后while(true)
将继续下去,你将不得不杀死程序来阻止它。这是你的代码和检查评分逻辑,我不知道规则:
import java.util.Random;
public class diceGame {
public static Random rng = new Random();
private int a, b, c; //rolls
public int getScore() {
this.a = 1 + rng.nextInt(5);
this.b = 1 + rng.nextInt(5);
this.c = 1 + rng.nextInt(5);
if (a == 6 && b == 6 && c == 6) {
System.out.println("Congratulations! You rolled three 6s! You just earned 500 points!!");
return 500;
} else if ((a == 6 && b == 6 && b != c) || (b == 6 && c == 6 && c != a)) {
System.out.println("Congratulations! You rolled two 6s! You just earned 100 points!!");
return 100;
} else if ((a == b && b != c) || (b == c && c != a) || (a == c && b != a)) {
System.out.println("Congratulations! You just earned 50 points!!");
return 50;
} else {
System.out.println("Nice Try!");
return 0;
}
}
public String toString() {
return String.format("Rolled a %d, %d, and %d", a, b, c);
}
public static void main(String args[]) {
diceGame d = new diceGame();
for (int i = 0; i < 3; i++) {
System.out.println(d.getScore());
System.out.println(d.toString());
}
}
}