[英]Loops and Conditionals in Java
编辑。 问题的字符串比较部分已经解决了,但是当在2个骰子中只滚动一个1时,代码仍然没有进入下一个玩家,只有当两个骰子都是1时才进入下一个玩家。
这是到目前为止的代码,第一个类几乎来自教科书,第二个类使用main方法就是我所做的。
该计划正在尝试创建一个名为pig的骰子游戏。 如果感兴趣,简单的规则是代码的底部,但主要问题。
我有的是它没有正确循环,当我没有把y
放在扫描仪中再次发出信号滚动它就像我输入y
一样继续,当我没有。 此外, if
语句不能正常工作,因为当玩家使用其中一个骰子滚动1
,它不会进入下一个玩家。
对不起,如果我没有正确解释问题。 另外,我不想再使用任何方法或类。 我假设有更快的方法来实现这一点,而不是我正在做的方式,但如果我想在我的代码中使用额外的方法,那将在以后使用。 我也有计算积分的问题,因为有时它并没有适当地改变它们,但是一旦其余部分正常工作我就能弄明白。
这是第一段代码。 这段代码对问题并不重要,但是如果你想知道我在main
调用哪些方法,你可以看看这些:
import java.util.Random;
public class PairOfDice {
private final int MAX = 6;
private int faceValue;
private int faceValue1;
Random generator0 = new Random();
Random generator1 = new Random();
public PairOfDice(){
faceValue = 1;
faceValue1 = 1;
}
public int getFaceValue() {
return faceValue;
}
public void setFaceValue(int faceValue) {
this.faceValue = faceValue;
}
public int getFaceValue1() {
return faceValue1;
}
public void setFaceValue1(int faceValue1) {
this.faceValue1 = faceValue1;
}
public int rollOne() {
faceValue = generator0.nextInt(MAX) + 1;
return faceValue;
}
public int rollTwo() {
faceValue1 = generator1.nextInt(MAX) + 1;
return faceValue1;
}
public int sumOfRoll() {
return faceValue + faceValue1;
}
@Override
public String toString() {
return "First roll of the die: \t" + rollOne()
+ "\nSecond roll of the die: " + rollTwo()
+ "\nThe sum of both rolls: \t" + sumOfRoll();
}
}
下一段代码是我自己的。 我已经更新了代码中的一些内容,现在使用.equals比较字符串时我更改了while条件并简化了if语句。
public class NewClass {
public static void main(String[] args) {
PairOfDice player1 = new PairOfDice();
PairOfDice player2 = new PairOfDice();
Scanner scan = new Scanner(System.in);
int p1 = 33, p2 = 0, turnp1 = 0, turnp2 = 0, signal = 1;
while (p1 <= 100 || p2 >= 100) {
int newp1total = p1;
turnp1 = 0;
while (turnp1 <= 20 && signal == 1) {
System.out.println("Player 1s Turn!");
int die1 = player1.rollOne();
int die2 = player1.rollTwo();
int sumofdice = player1.sumOfRoll();
System.out.println("Player 1: First Die: " + die1 + " Second Die:" + die2 + " Sum of Roll: " + sumofdice);
if (sumofdice == 2) {
p1 = 0;
turnp1 = 0;
signal = -1;
System.out.println("Player rolled a two 1s. All players points are forfeited. Next Players turn.");
System.out.println("Points this turn:" + turnp1);
System.out.println("Points this game: " + p1);
System.out.println();
} else if (die1 == 1 || die2 == 1) {
turnp1 = 0;
signal = -1;
p1 = newp1total;
System.out.println("Player rolled a 1. All points on this round are forfeited. Next Players turn.");
System.out.println("Points this turn:" + turnp1);
System.out.println("Points this game: " + p1);
System.out.println();
} else {
turnp1 += sumofdice;
p1 += sumofdice;
System.out.println("Points this turn:" + turnp1);
System.out.println("Points this game: " + p1);
System.out.println();
signal = 1;
}
}
signal = 1;
String yesno = "y";
int newp2total = p2;
while (yesno.toLowerCase().equals("y") && signal == 1) {
System.out.println("Player 2s Turn!");
int die1 = player2.rollOne();
int die2 = player2.rollTwo();
int sumofdice = player2.sumOfRoll();
System.out.println("Player 2: First Die: " + die1 + " Second Die:" + die2 + " Sum of Roll: " + sumofdice);
if (sumofdice == 2) {
p2 = 0;
turnp2 = 0;
signal = -1;
System.out.println("Player rolled a two 1s. All players points are forfeited. Next Players turn.");
System.out.println("Points this turn:" + turnp2);
System.out.println("Points this game: " + p2);
System.out.println();
} else if (die1 == 1 || die2 == 1) {
signal = -1;
turnp2 = 0;
p2 = newp2total;
System.out.println("Player rolled a 1. All points on this round are forfeited. Next Players turn.");
System.out.println("Points this turn:" + turnp2);
System.out.println("Points this game: " + p2);
System.out.println();
} else {
turnp2 += sumofdice;
p2 += sumofdice;
System.out.println("Points this turn:" + turnp2);
System.out.println("Points this game: " + p2);
System.out.println();
System.out.println("Try your luck? Y/N");
yesno = scan.next();
System.out.println();
signal = 1;
}
}
}
}
}
第一件事是第一件事,这是一个重要的概念,当比较字符串总是使用.equals,而不是==。 例如
yesno.toLowerCase() == "y" && signal == 1
应该
yesno.toLowerCase().equals("y") && signal.equals 1
这是因为y是一个字符串。 如果y是int ,那么你会使用==。 如果您正在比较y并且您不断更改值,那么您将使用=。 下一件事; 我知道使用getter和setter可能是教科书作业的一部分,但除非是; 不要使用它们。
阅读这篇文章 。
它使代码很难遵循。 在你的代码中,其他明智的工作,我喜欢你尊重java代码约定!
另外,确保对字符串使用.equals,对于整数使用==。 如果你要比较整数,我相信你是int变量,你需要使用==。 另外,我认为你可能会对概念感到困惑。 如果你有一个if语句并且正在比较事物并且需要BOTH语句为true,那么使用“&&”如果你只需要一个语句为真,那么使用|| (此键是带有班次的输入上方的键)
您正在使用==
比较字符串,这导致比较引用相等:替换
while (yesno.toLowerCase() == "y" && signal == 1) {
同
while (yesno.toLowerCase().equals("y") && signal == 1) {
要么
while (yesno.equalsIgnoreCase("y") && signal == 1) {
==
与equals()
:
==
测试引用相等性(即它们是否是相同的对象,即在字符串池中引用相同的字符串)。
但是, .equals()
检查值是否相等(它们的内容是否实际相同)。
但要注意null
字符串。 ==
可以很好地处理这些null
字符串,但是从null
字符串调用.equals()
会导致异常,因为使用null
对象调用该方法。
有关更多信息,请参阅字符串池
替换声明:
while (p1 <= 100 ^ p2 >= 100) {
同
while (p1 <= 100 || p2 >= 100) {
考虑:
int p1=1; int p2=2; if( p1==1 ^ p2==2){ // False here since it is TRUE^TRUE which is FALSE System.out.println("Same"); } if( p1==1 || p2==3){ // TRUE if either one is TRUE System.out.println("Same Again"); }
仅打印
又是老样子
这是因为^
是按位XOR
运算符,当且仅当两个操作数具有相同的真值时才是假的。
else if
等效, else if
替换else if
两个
else if ( die2 == 1 || die1 == 1 ) {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.