我是一名CS学生刚刚开始,我一直坚持与牛和公牛比赛。它编译并运行,但是在某个地方出现某种错误,我没有看到导致错误的牛和公牛数量(预期与我的输出的样本)。
奶牛和公牛的解释:公牛 - 正确位数。奶牛 - 如果它们处于正确位置则是正确的位数。
public class CowsAndBulls{
//Constants
public final static int NUM_DIGITS = 4;
public final static int MAX_VALUE = 9876;
public final static int MIN_VALUE = 1234;
public final static int MAX_GUESSES = 10;
//
// instances
private NumberPicker randomNumber;
private int answer;
private int guessesCopy;
private int bullStored;
private int cowStored;
//
public CowsAndBulls(int seed){
randomNumber = new NumberPicker(seed, MIN_VALUE, MAX_VALUE);
answer = randomNumber.nextInt();
guessesCopy = MAX_GUESSES;
}
////////////////////////////////////////////////////////
//Stuff between the comments is from a previous question that needs to be used in CowsAndBulls (not in a package) - I know it works as it's supposed to.
public static int[] toArray(int number){
String numString = Integer.toString(number);
int[] someArray = new int[numString.length()];
for (int i = 0; i < numString.length(); i++){
char c = numString.charAt(i);
int cVal = Character.getNumericValue(c);
someArray[i] = cVal;
}
return someArray;
}
public static int countMatches(int a, int b){ //Bulls
String stringA = Integer.toString(a);
int lengthAB = stringA.length();
int count = 0;
int[] arrayOutA = toArray(a);
int[] arrayOutB = toArray(b);
for (int i = 0; i < lengthAB; i++){
if (arrayOutA[i] == arrayOutB[i])
count += 1;
}
return count;
}
public static int countIntersect(int numA, int numB){ //Cows
String stringA = Integer.toString(numA);
int lengthAB = stringA.length();
int count = 0;
int[] arrayOutA = toArray(numA);
int[] arrayOutB = toArray(numB);
for (int i = 0; i < lengthAB; i++){
for (int j = 0; j < lengthAB; j++){
if ( arrayOutA[i] == arrayOutB[j]){
count += 1;
}
}
}
return count;
}
//////////////////////////////////////////////////////////////////
public int guessesRemaining(){
return guessesCopy;
}
public Result guess(int guessNumber){
int bulls = countMatches(answer, guessNumber);
bullStored = bulls;
int cows = countIntersect(answer, guessNumber);
cowStored = cows;
guessesCopy--;
return (new Result(cows, bulls));
}
public int giveUp(){
return (answer);
}
public boolean gameOver(){
if (guessesCopy == 0 || bullStored == 4)
return true;
else
return false;
}
以下是我们应该使用提供的类,并且无法以任何方式编辑:NumberPicker,Game和Result。 NumberPicker:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
/**
* Given a number range, a NumberPicker returns the numbers in the range in a random order
*/
public class NumberPicker {
private List<Integer> numbers;
/**
* Create a NumberPicker that uses the given seed value for randomisation and that
* returns the numbers in the range min to max (inclusive) in a random order.
* /
public NumberPicker(final int seed, final int min, final int max) {
numbers = new ArrayList<Integer>();
final Random random = new Random(seed);
for(int i = min; i<max+1; i++) {
numbers.add(i);
}
Collections.shuffle(numbers, random);
}
/**
* Determine whether the NumberPicker contains any more numbers..
*/
public boolean hasNext() { return !numbers.isEmpty(); }
/**
* Return a randomly selected number from the range.
*/
public int nextInt() { return numbers.remove(0); }
}
游戏课程:
import java.util.Scanner;
public class Game {
private Game() {}
public static void main(String[] inputs) {
Scanner input = new Scanner(System.in);
System.out.println("Your challenge is to guess a secret " + CowsAndBulls.NUM_DIGITS + " digit number.");
System.out.println("Enter randomisation seed value:");
CowsAndBulls cowsAndBulls = new CowsAndBulls(input.nextInt());
System.out.println("Make a guess:");
Result answer = cowsAndBulls.guess(input.nextInt());
while(!answer.isCorrect()&&cowsAndBulls.guessesRemaining()>0) {
System.out.println("Sorry that's incorrect.");
System.out.println("You have "+answer+".");
System.out.printf("You have %d guesses remaining\n", cowsAndBulls.guessesRemaining());
System.out.println("Make a guess:");
answer = cowsAndBulls.guess(input.nextInt());
}
if (answer.isCorrect()) {
System.out.println("Correct !");
}
else {
System.out.println("Sorry, you lose.");
}
}
}
最后,结果类:
/**
* A Result object records the outcome of a guess in the Cows and Bulls guessing game.
*
*
*/
public class Result {
private int cows;
private int bulls;
public Result(int cows, int bulls) {
assert(cows+bulls<=4);
this.cows=cows;
this.bulls=bulls;
}
public int cows() { return cows; }
public int bulls() { return bulls; }
public boolean isCorrect() { return bulls==4; }
public boolean equals(Object o) {
if (!(o instanceof Result)) {
return false;
}
else {
Result other = (Result)o;
return this.cows()==other.cows()&&this.bulls()==other.bulls();
}
}
public String toString() {
String result = this.cows()+(this.cows()!=1 ? " cows" : " cow");
result = result+" and "+this.bulls()+(this.bulls()!=1 ? " bulls" : " bull");
return result;
}
}
这是所有代码。重申:我不能改变除CowsAndBulls之外的任何类,我必须使用Game,Result和NumberPicker。以下是我的计划产生的预期产出......
Trial 1: Output not correct
The expected output was:
10
false
8913
true
Your program produced:
10
false
7407
false
Input supplied to your program:
construct 3
guessesRemaining()
gameOver()
giveUp()
gameOver()
Q
-------------------------------------
Trial 2: Output not correct
The expected output was:
10
0 cows and 0 bulls
9
1 cow and 0 bulls
8
2 cows and 0 bulls
7
3 cows and 0 bulls
6
4 cows and 0 bulls
5
Your program produced:
10
1 cow and 0 bulls
9
0 cows and 0 bulls
8
1 cow and 0 bulls
7
2 cows and 0 bulls
6
2 cows and 0 bulls
5
Input supplied to your program:
construct 4
guessesRemaining()
guess() 2358
guessesRemaining()
guess() 1235
guessesRemaining()
guess() 1735
guessesRemaining()
guess() 1749
guessesRemaining()
guess() 1746
guessesRemaining()
Q
----------------------------------------------
Trial 3: Output not correct
The expected output was:
10
0 cows and 0 bulls
9
0 cows and 1 bull
8
0 cows and 2 bulls
7
0 cows and 3 bulls
6
0 cows and 4 bulls
5
true
Your program produced:
10
1 cow and 0 bulls
9
1 cow and 0 bulls
8
1 cow and 0 bulls
7
1 cow and 0 bulls
6
1 cow and 0 bulls
5
false
Input supplied to your program:
construct 8
guessesRemaining()
guess() 2358
guessesRemaining()
guess() 2758
guessesRemaining()
guess() 2748
guessesRemaining()
guess() 6748
guessesRemaining()
guess() 6741
guessesRemaining()
gameOver()
Q
我可能只是愚蠢而没有看到,但任何帮助都非常感激。我是S.E(和Java)的新手,所以我的代码格式可能很奇怪 - 如果是这样,我会编辑它。谢谢:)。
答案 0 :(得分:2)
The expected output was:
10
false
8913
true
Your program produced:
10
false
7407
false
Input supplied to your program:
construct 3
guessesRemaining()
gameOver()
giveUp()
gameOver()
Q
据我所知,它告诉你它对giveUp()的期望数是不正确的。这表明你的程序以某种方式产生不同的数字(7404 vs 8913)。 这可能是由于您的MIN_VALUE和/或MAX_VALUE?
public static int countIntersect(int numA, int numB) { //Cows
String stringA = Integer.toString(numA);
int lengthAB = stringA.length();
int count = 0;
int[] arrayOutA = toArray(numA);
int[] arrayOutB = toArray(numB);
for (int i = 0; i < lengthAB; i++) {
for (int j = 0; j < lengthAB; j++) {
if (arrayOutA[i] == arrayOutB[j]) {
count += 1;
}
}
}
return count;
}
我不认为这就是你想要的。例如countIntersect(1111,1111)将返回16,我希望这是4或0?
至于你的编码,这里有一些小的反馈:
在函数giveUp
中,你不需要在return语句中使用(和)。
// Original
public int giveUp() {
return (answer);
}
// Suggested
public int giveUp() {
return answer;
}
在guess
函数中,您不需要先将其保存在本地int中。也是(和)再次。
// Original
public Result guess(int guessNumber) {
int bulls = countMatches(answer, guessNumber);
bullStored = bulls;
int cows = countIntersect(answer, guessNumber);
cowStored = cows;
guessesCopy--;
return (new Result(cows, bulls));
}
// Suggested
public Result guess(int guessNumber) {
bullStored = countMatches(answer, guessNumber);
cowStored = countIntersect(answer, guessNumber);
guessesCopy--;
return new Result(cowStored, bullStored);
}
在你的方法countIntersect
中,没有理由使用特殊的int []数组。字符数组也可以。
另外,lengthAB可以替换为2个数组之一的.length属性。
// Original
public static int countIntersect(int numA, int numB) { //Cows
String stringA = Integer.toString(numA);
int lengthAB = stringA.length();
int count = 0;
int[] arrayOutA = toArray(numA);
int[] arrayOutB = toArray(numB);
for (int i = 0; i < lengthAB; i++) {
for (int j = 0; j < lengthAB; j++) {
if (arrayOutA[i] == arrayOutB[j]) {
count += 1;
}
}
}
return count;
}
// Suggested
public static int countIntersect(int numA, int numB) { //Cows
int count = 0;
char[] arrayA = Integer.toString(numA).toCharArray();
char[] arrayB = Integer.toString(numB).toCharArray();
for (int i = 0; i < arrayA.length; i++) {
for (int j = i; j < arrayB.length; j++) {
if (arrayA[i] == arrayB[j]) {
if(i != j) {
count++;
}
break;
}
}
}
return count;
}
同样适合countMatches
。
// Original
public static int countMatches(int a, int b){ //Bulls
String stringA = Integer.toString(a);
int lengthAB = stringA.length();
int count = 0;
int[] arrayOutA = toArray(a);
int[] arrayOutB = toArray(b);
for (int i = 0; i < lengthAB; i++){
if (arrayOutA[i] == arrayOutB[i])
count += 1;
}
return count;
}
// Suggested
public static int countMatches(int a, int b) { //Bulls
int count = 0;
char[] arrayA = Integer.toString(a).toCharArray();
char[] arrayB = Integer.toString(b).toCharArray();
for (int i = 0; i < Math.min(arrayA.length,arrayB.length); i++) {
if (arrayA[i] == arrayB[i])
count += 1;
}
return count;
}
最后,您的gameOver
写得更简单。
//Original
public boolean gameOver() {
if (guessesCopy == 0 || bullStored == 4)
return true;
else
return false;
}
// Suggested
public boolean gameOver() {
return guessesCopy == 0 || bullStored == 4;
}