gpt4 book ai didi

java - 奶牛和公牛代码 - Java

转载 作者:行者123 更新时间:2023-11-30 08:33:35 25 4
gpt4 key购买 nike

我是一名刚开始学习 CS 的学生,我一直在玩 Cows and Bulls 游戏。它编译并运行,但在某处存在某种我没有看到的错误,这会导致 Cows 和 Bulls 的数量不正确(预期输出与我的输出的样本)。

Cows和Bulls的解释:Bulls——正确数字的个数。奶牛 - 如果它们处于正确的位置,则正确的位数。

  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。数字选择器:

    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)的新手,所以我的代码格式可能很奇怪 - 如果是这样,我会编辑它。谢谢 :)。

最佳答案

   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 与 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;
}

关于java - 奶牛和公牛代码 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39386188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com