如何解决程序中的计数器已关闭
我必须为我的课堂写一个程序。
说明是
您的程序将选择一个随机的4位数字作为密码。
您的程序必须提示用户输入4位数字作为猜测值。
该程序将响应一条消息,指示用户猜测中的多少位数与秘密号码中相同位置的位数相同。
例如,如果密码是3749
,而用户的猜测是9753
,则程序将以消息“您匹配1”来响应,因为只有一位数字(7位)用户猜测中的数字与秘密号码中相同位置的数字相同。
该程序将允许用户继续输入猜测,直到他们猜测正确的密码为止。
用户输入密码后,程序将输出用户查找密码的猜测总数。
然后,程序将询问用户是否要再次播放。如果用户回答“是”,则程序将选择另一个随机的4位数字,并如上所述继续播放。
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;
}
}
}
}
}
由于某种原因,即使程序运行不正确,matchCount
也始终为4。
我以为if语句下的代码只有在条件为true时才会执行,但由于某种原因它还是会运行它。
解决方法
您的if
语句后有分号,使它们无用。格式应如下:
if (userGuess.substring(0,1).equals(secretNumber.substring(0,1))) {
matchCount++;
}
但由于当前正在编写程序,所以即使它们重复数字,matchCount也将继续增加。因此,如果数字为1234
,并且用户猜测2222
,则2223
的matchCount将为2。我建议为boolean
,digit1
,digit2
和digit3
设置digit4
的值。猜测数字时,将值设置为true。如果if
,则仅运行每个digit[x] = false
块。
编辑:经过反复试验,我使程序尽可能可靠。以前,即使您重复相同的数字,matchCount
也会保持增长,因此为防止这种情况,我实现了一些boolean
值。
此外,如果正确猜出了一个数字,但随后的尝试中没有猜到一个数字,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("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)) && digit1 == false)
{ //if the first digit is correct and hasn't been guessed already,matchcount increases
matchCount++;
digit1 = true;
}
if (!userGuess.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)) && 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)) && 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)) && 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);
}
}
}
}
}
,
首先,我认为您可以将代码更改为此,并且更易于维护,并且更具可读性
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;
}
}
}
}
}
并在每次猜测后重置matchCount
,如果阻止则为空
if
末尾的分号将它们关闭,并且无论如何都将执行它们旁边的块。修复:
if (userGuess.substring(0,1)))
{
matchCount++;
}
if (userGuess.substring(1,2)))
{
matchCount++;
}
if (userGuess.substring(2,3)))
{
matchCount++;
}
if (userGuess.substring(3,4)))
{
matchCount++;
}
matchCount在每次迭代时应为0
如果您碰巧不敢猜出完整的数字,但是至少有一位数字是正确的,那么matchCount
将是一个严格的正数。由于您未将其设置为0,因此matchCount
的行为方式错误。您需要重置它,例如
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;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。