我正在开发一个有两个不同类的牛和公牛游戏:第一个类看起来像这样:import java.util.*; 导入 java.util.Scanner;
public class CowsAndBulls {
int UserNumber=0;
int CompNumber=0;
int bulls=0;
int cows=0;
Scanner input = new Scanner(System.in);
public void getUserNumber() {
System.out.println("Please enter your guess!");
int UserValue=input.nextInt();
UserNumber=UserNumber+ UserValue;
System.out.println("Your guess is : " + UserNumber);
}
public void getCompNumber() {
Random generator= new Random();
int pickedNumber=generator.nextInt(10000) + 1000;
CompNumber =CompNumber+ pickedNumber;
System.out.println("The computer has guessed : " + CompNumber );
}
}
它只是为计算机生成一个随机数并要求用户输入他们的猜测。我的游戏课遇到了很多麻烦,具体来说,我无法让我的 while 循环运行,
public class CowsAndBullsPlay {
public void play() {
int GuessCount=0;
int UserNumber = 0;
int CompNumber = 0;
int cows = 0;
int bullcount=0;
int cowcount=0;
int bulls = 0;
Boolean GameEnds;
GameEnds=false;
while (GameEnds=false) {
CowsAndBulls user= new CowsAndBulls();
user.getUserNumber();
CowsAndBulls comp= new CowsAndBulls();
comp.getCompNumber();
String UserString=String.valueOf(UserNumber);
String CompString=String.valueOf(CompNumber);
String U=UserString;
String C=CompString;
for (int i=0; i<4; i++)
for (int k=i+1; k<4; k++) {
if(U.charAt(i)==C.charAt(k)) {
bullcount ++;
bulls=bulls + bullcount;
}
else if(U.contains(C.charAt(i) + U)) {
//for (int v=0; v<4; v++)
//for (int j=0; j<4; j++) {
cowcount ++;
cows=cows + cowcount;
}
GuessCount++;
}
if (bulls==4) {
GameEnds= true;
System.out.println("Congratulations!");
System.out.println("it took you:" + GuessCount);
}
else {
System.out.println("cows: " + cows);
System.out.println("Bulls:" + bulls);
}
}
}
}
它应该检查奶牛然后检查公牛,然后如果公牛不等于四,循环应该重复——调用前两个方法,要求用户输入并要求计算机随机生成一个数字,然后猜测计数应该保持跟踪循环迭代。
代码编译,但它不会运行 while 循环
这是我的测试员课程:
public class CowsAndBullsTester {
public static void main(String[] args) {
int bears = 0;
int cows = 0;
Boolean GameEnds = false;
System.out.println("welcome to Cows Versus Bulls death match!");
CowsAndBulls user= new CowsAndBulls ();
user.getUserNumber();
System.out.println("Now the computer will pick a number!");
CowsAndBulls comp= new CowsAndBulls ();
comp.getCompNumber();
System.out.println("Let's see how you did!");
CowsAndBullsPlay game= new CowsAndBullsPlay();
game.play();
}
}
非常感谢任何帮助。我确定有一些愚蠢的错误,我只是不确定是什么。这只是我的第三个任务,我很难自己找出问题所在:)
谢谢!