英文:
counter is off in program
问题
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Random random = new Random();
System.out.println("----- MASTERMIND -----");
System.out.println("Guess the 4 digit number!");
boolean keepGoing = true;
while (keepGoing == true) {
String secretNumber = String.format("%04d", random.nextInt(10000));
int guessCount = 0;
int matchCount = 0;
while (matchCount != 4) {
System.out.println("Enter your guess: ");
String userGuess = input.nextLine();
guessCount++;
if (userGuess.substring(0, 1).equals(secretNumber.substring(0, 1))) {
matchCount++;
}
if (userGuess.substring(1, 2).equals(secretNumber.substring(1, 2))) {
matchCount++;
}
if (userGuess.substring(2, 3).equals(secretNumber.substring(2, 3))) {
matchCount++;
}
if (userGuess.substring(3, 4).equals(secretNumber.substring(3, 4))) {
matchCount++;
}
System.out.println("You matched " + matchCount + " digit/digits.");
if (userGuess.equals(secretNumber)) {
System.out.println("Congratulations! You guessed the right number in " + guessCount + " guess/guesses.");
System.out.println("Would you like to play again? Enter Y for yes or N for no.");
String keepGoingYesOrNo = input.nextLine();
if (keepGoingYesOrNo.equals("N")) {
keepGoing = false;
}
}
}
}
}
英文:
I have to write a program for my class.
The instructions are
Your program will choose a random 4 digit number as the secret number.
Your program must prompt the user to enter a 4 digit number as their guess.
The program will respond with a message indicating how many of the digits in the user’s guess are the same as the digit in the same position in the secret number.
For example, if the secret number is 3749
, and the user’s guess is 9753
, then the program would respond with the message You matched 1, because only one of the digits (the 7) in the user’s guess is the same as the digits in the same position in the secret number.
The program will allow the user to continue to enter guesses until they guess the correct secret number.
After the user has entered the secret number, the program will output a count of the total number of guesses the user took to find the secret number.
Then the program will ask the user if they would like to play again. If the user answers “yes”, then the program will choose another random 4 digit number and play continues as described above.
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
Random random = new Random();
System.out.println("----- MASTERMIND -----");
System.out.println("Guess the 4 digit number!");
boolean keepGoing = true;
while (keepGoing = true)
{
String secretNumber = String.format("%04d", random.nextInt(10000));
int guessCount = 0;
int matchCount = 0;
while (matchCount != 4)
{
System.out.println("Enter your guess: ");
String userGuess = input.nextLine();
guessCount++;
if (userGuess.substring(0,1).equals(secretNumber.substring(0,1)));
{
matchCount++;
}
if (userGuess.substring(1,2).equals(secretNumber.substring(1,2)));
{
matchCount++;
}
if (userGuess.substring(2,3).equals(secretNumber.substring(2,3)));
{
matchCount++;
}
if (userGuess.substring(3,4).equals(secretNumber.substring(3,4)));
{
matchCount++;
}
System.out.println("You matched " + matchCount + " digit/digits.");
if (userGuess.equals(secretNumber))
{
System.out.println("Congratulations! You guessed the right number in " + guessCount + " guess/guesses.");
System.out.println("Would you like to play again? Enter Y for yes or N for no.");
String keepGoingYesOrNo = input.nextLine();
if (keepGoingYesOrNo.equals("N"));
{
keepGoing = false;
}
}
}
}
}
For some reason the matchCount
is always 4 once the program runs even when the numbers don't match.
I thought the code under the if statement would only be executed if the condition is true but for some reason it runs it anyways.
答案1
得分: 2
你在你的if
语句后面加了分号,使它们变得无效。格式应该像这样:
if (userGuess.substring(0,1).equals(secretNumber.substring(0,1))) {
matchCount++;
}
但是由于当前程序的编写方式,即使是重复的数字,matchCount
也会继续增加。所以如果数字是 1234
,用户猜测是 2222
,然后是 2223
,matchCount
将为二。我建议为 digit1
、digit2
、digit3
和 digit4
设置布尔值。当猜测了该数字时,将值设置为 true。只有在 digit[x] = false
时才运行每个 if
块。
编辑:经过一些试错,我尽量使程序更可靠。以前,即使重复相同的数字,matchCount
也会继续增加,为了防止这种情况,我实现了一些布尔值。
此外,如果一个数字被正确猜测,但在后续的猜测中没有被猜中,matchCount
会保持不变,所以我写了另一组 if
语句来在这种情况下减少 matchCount
。我还修复了代码中的一些其他小问题,包括在重新开始时重置 matchCount
,并在用户不想玩时成功结束程序。如果你有任何问题,请告诉我!以下是最终代码。
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
Random random = new Random();
System.out.println("----- MASTERMIND -----");
System.out.println("猜测4位数字!");
boolean keepGoing = true;
boolean digit1 = false;
boolean digit2 = false;
boolean digit3 = false;
boolean digit4 = false;
while (keepGoing == true)
{
String secretNumber = String.format("%04d", random.nextInt(10000));
int guessCount = 0;
int matchCount = 0;
while (matchCount != 4 && keepGoing == true)
{
System.out.println("输入你的猜测:");
System.out.println(secretNumber);
String userGuess = input.nextLine();
guessCount++;
if (userGuess.substring(0,1).equals(secretNumber.substring(0,1)) && digit1 == false)
{ //如果第一个数字正确且尚未猜中,matchCount 增加
matchCount++;
digit1 = true;
}
if (!userGuess.substring(0,1).equals(secretNumber.substring(0,1)) && digit1 == true)
{ //如果第一个数字不正确,但之前猜过它,matchCount 减少且 digit1 设置为 false
matchCount--;
digit1 = false;
}
if (userGuess.substring(1,2).equals(secretNumber.substring(1,2)) && digit2 == false)
{
matchCount++;
digit2 = true;
}
if (!userGuess.substring(1,2).equals(secretNumber.substring(1,2)) && digit2 == true)
{
matchCount--;
digit2 = false;
}
if (userGuess.substring(2,3).equals(secretNumber.substring(2,3)) && digit3 == false)
{
matchCount++;
digit3 = true;
}
if (!userGuess.substring(2,3).equals(secretNumber.substring(2,3)) && digit3 == true)
{
matchCount--;
digit3 = false;
}
if (userGuess.substring(3,4).equals(secretNumber.substring(3,4)) && digit4 == false)
{
matchCount++;
digit4 = true;
}
if (!userGuess.substring(3,4).equals(secretNumber.substring(3,4)) && digit4 == true)
{
matchCount--;
digit4 = false;
}
System.out.println("你匹配了 " + matchCount + " 个数字。");
if (userGuess.equals(secretNumber))
{
System.out.println("恭喜!你在 " + guessCount + " 次猜测中猜对了数字。");
System.out.println("你想再玩一次吗?输入 Y 表示是,输入 N 表示否。");
String keepGoingYesOrNo = input.nextLine();
if (keepGoingYesOrNo.equals("Y"))
{
keepGoing = true;
matchCount=0;
guessCount=0;
} else {
System.out.println("感谢你的游玩!");
System.exit(0);
}
}
}
}
}
英文:
You have semicolons after your if
statements making them useless. The format should be like this:
if (userGuess.substring(0,1).equals(secretNumber.substring(0,1))) {
matchCount++;
}
But also as the program is currently written, matchCount will keep increasing even if they duplicate numbers. So if the number was 1234
and the user guesses 2222
then 2223
matchCount will be two. I recommend setting boolean
values for digit1
, digit2
, digit3
, and digit4
. When the digit is guessed, set the value to true. Only run each if
block if digit[x] = false
.
Edit: After some trial and error I made the program as reliable as possible. Before, matchCount
would keep increasing even if you repeated the same digits, so to prevent this I implemented some boolean
values.
Furthermore, if a digit was correctly guessed but then not guessed in following attempts matchCount
would remain the same, so I wrote another set of if statements to decrease matchCount
in this event. I also fixed some other minor issues within the code including resetting matchCount
when replaying and also successfully ending the program when a user does not want to play. Let me know if you have any questions! Here is the final code.
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
Random random = new Random();
System.out.println("----- MASTERMIND -----");
System.out.println("Guess the 4 digit number!");
boolean keepGoing = true;
boolean digit1 = false;
boolean digit2 = false;
boolean digit3 = false;
boolean digit4 = false;
while (keepGoing = true)
{
String secretNumber = String.format("%04d", random.nextInt(10000));
int guessCount = 0;
int matchCount = 0;
while (matchCount != 4 && keepGoing == true)
{
System.out.println("Enter your guess: ");
System.out.println(secretNumber);
String userGuess = input.nextLine();
guessCount++;
if (userGuess.substring(0,1).equals(secretNumber.substring(0,1)) && digit1 == false)
{ //if the first digit is correct and hasn't been guessed already, matchcount increases
matchCount++;
digit1 = true;
}
if (!userGuess.substring(0,1).equals(secretNumber.substring(0,1)) && digit1 == true)
{ //if the first digit is incorrect but you previously guessed it, matchcount decreases and digit1 is set to false
matchCount--;
digit1 = false;
}
if (userGuess.substring(1,2).equals(secretNumber.substring(1,2)) && digit2 == false)
{
matchCount++;
digit2 = true;
}
if (!userGuess.substring(1,2).equals(secretNumber.substring(1,2)) && digit2 == true)
{
matchCount--;
digit2 = false;
}
if (userGuess.substring(2,3).equals(secretNumber.substring(2,3)) && digit3 == false)
{
matchCount++;
digit3 = true;
}
if (!userGuess.substring(2,3).equals(secretNumber.substring(2,3)) && digit3 == true)
{
matchCount--;
digit3 = false;
}
if (userGuess.substring(3,4).equals(secretNumber.substring(3,4)) && digit4 == false)
{
matchCount++;
digit4 = true;
}
if (!userGuess.substring(3,4).equals(secretNumber.substring(3,4)) && digit4 == true)
{
matchCount--;
digit4 = false;
}
System.out.println("You matched " + matchCount + " digit/digits.");
if (userGuess.equals(secretNumber))
{
System.out.println("Congratulations! You guessed the right number in " + guessCount + " guess/guesses.");
System.out.println("Would you like to play again? Enter Y for yes or N for no.");
String keepGoingYesOrNo = input.nextLine();
if (keepGoingYesOrNo.equals("Y"))
{
keepGoing = true;
matchCount=0;
guessCount=0;
} else {
System.out.println("Thanks for playing!");
System.exit(0);
}
}
}
}
}
答案2
得分: 2
首先,我认为你可以将代码更改为以下内容,这样更易于维护并且更易读。
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Random random = new Random();
System.out.println("----- 猜数字游戏 -----");
System.out.println("猜一个4位数的数字!");
boolean keepGoing = true;
while (keepGoing) {
String secretNumber = String.format("%04d", random.nextInt(10000));
int guessCount = 0;
int matchCount;
do {
matchCount = 0;
System.out.print("请输入你的猜测: ");
String userGuess = input.nextLine();
guessCount++;
for (int i = 0; i < secretNumber.length(); i++) {
if (secretNumber.charAt(i) == userGuess.charAt(i)) {
matchCount++;
}
}
System.out.println("你猜对了 " + matchCount + " 位数字。");
if (userGuess.equals(secretNumber)) {
System.out.println("恭喜!你在 " + guessCount + " 次猜测中猜对了数字。");
System.out.print("你想再玩一次吗?输入 Y 表示是,输入 N 表示否: ");
String keepGoingYesOrNo = input.nextLine();
if (keepGoingYesOrNo.equalsIgnoreCase("N")) {
keepGoing = false;
}
}
} while (matchCount != 4);
}
}
并且在每次猜测后重置 matchCount
。
英文:
First of all, I think you can change the code to this and it is easier to maintain and it is more readable
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
Random random = new Random();
System.out.println("----- MASTERMIND -----");
System.out.println("Guess the 4 digit number!");
boolean keepGoing = true;
while (keepGoing = true)
{
String secretNumber = String.format("%04d",
random.nextInt(10000));
int guessCount = 0;
int matchCount = 0;
while (matchCount != 4)
{
matchCount = 0
System.out.println("Enter your guess: ");
String userGuess = input.nextLine();
guessCount++;
for(int i = 0 ; i < secretNumber.length() ; i++){
if(secreetNumber.charAt[i] == userGuess.charAt[i])
matchCount++;
}
System.out.println("You matched " + matchCount + "
digit/digits.");
if (userGuess.equals(secretNumber))
{
System.out.println("Congratulations! You guessed the
right
number in " + guessCount + " guess/guesses.");
System.out.println("Would you like to play again? Enter
Y for yes or N for no.");
String keepGoingYesOrNo = input.nextLine();
if (keepGoingYesOrNo.equals("N"));
{
keepGoing = false;
}
}
}
}
}
And reset matchCount after each guess
答案3
得分: 2
# 空的 if 块
末尾的分号关闭了这些 `if` 语句,紧挨着它们的代码块无论如何都会被执行。修复如下:
if (userGuess.substring(0,1).equals(secretNumber.substring(0,1)))
{
matchCount++;
}
if (userGuess.substring(1,2).equals(secretNumber.substring(1,2)))
{
matchCount++;
}
if (userGuess.substring(2,3).equals(secretNumber.substring(2,3)))
{
matchCount++;
}
if (userGuess.substring(3,4).equals(secretNumber.substring(3,4)))
{
matchCount++;
}
# 每次迭代时 matchCount 应该为 0
如果你没有猜对完整的数字,但至少有一个数字是正确的,那么 `matchCount` 将是一个严格大于零的数字。由于你没有将它设为 0,`matchCount` 的行为会出现错误。你需要重置它,就像这样:
if (userGuess.equals(secretNumber))
{
System.out.println("恭喜!你在" + guessCount + "次猜测中猜对了数字。");
System.out.println("你想再玩一次吗?输入 Y 表示是,输入 N 表示否。");
String keepGoingYesOrNo = input.nextLine();
if (keepGoingYesOrNo.equals("N"));
{
keepGoing = false;
}
}
else
{
matchCount = 0;
}
英文:
Empty if blocks
The semicolon at the end of the if
s closes them and the block next to them is executed no matter what. Fix:
if (userGuess.substring(0,1).equals(secretNumber.substring(0,1)))
{
matchCount++;
}
if (userGuess.substring(1,2).equals(secretNumber.substring(1,2)))
{
matchCount++;
}
if (userGuess.substring(2,3).equals(secretNumber.substring(2,3)))
{
matchCount++;
}
if (userGuess.substring(3,4).equals(secretNumber.substring(3,4)))
{
matchCount++;
}
matchCount should be 0 at each iteration
If you happen not to guess the full number, but at least one of your digits are correct, then matchCount
will be a strictly positive number. Since you do not set it to 0, the matchCount
will behave in the wrong way. You need to reset it, like
if (userGuess.equals(secretNumber))
{
System.out.println("Congratulations! You guessed the right number in " + guessCount + " guess/guesses.");
System.out.println("Would you like to play again? Enter Y for yes or N for no.");
String keepGoingYesOrNo = input.nextLine();
if (keepGoingYesOrNo.equals("N"));
{
keepGoing = false;
}
}
else
{
matchCount = 0;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论