该程序工作正常,但是当它循环时,它处于评论循环的乞讨处。计数器在每次循环后添加它而不是重置。示例:转1:您在正确的位置有1个正确。然后,如果我在第2回合再次输入相同的数字,我会得到:你在正确的位置有2个正确。而且它会继续增加。
class Mastermind
{
public static void main (String[] args)
{
//Welcome
System.out.println ("Welcome to Matthew's game of Mastermind!");
//Variable
int Tally;
int guess1, guess2, guess3, guess4;
Scanner Guess = new Scanner (System.in);
int[] Secret_code = Mystery();
int[] Gameboard = Gameboard (Secret_code);
int resultCount = Comparing (Secret_code, Gameboard);
int resultCount2 = Comparing2 (Secret_code, Gameboard);
//Loop
for (Tally = 0; Tally <10; Tally++){
//User Input
System.out.println ("What is the number one peg in my code?");
guess1 = Guess.nextInt();
System.out.println ("What is the number two peg in my code?");
guess2 = Guess.nextInt();
System.out.println ("What is the number three peg in my code?");
guess3 = Guess.nextInt();
System.out.println ("What is the number four peg in my code?");
guess4 = Guess.nextInt();
//Array for guess's
Gameboard = new int [4];
Gameboard[0] = guess1;
Gameboard[1] = guess2;
Gameboard[2] = guess3;
Gameboard[3] = guess4;
//result Count
for (int i = 0; i < 4; i++) {
if (Gameboard[i] == Secret_code[i])
resultCount++;
}
System.out.println("You got " + resultCount + " correct in the right spot.");
//Result Count 2
int i = 0;
for (int e = 0; e < 4; e++) {
if (Gameboard[e] == Secret_code[i])
resultCount2++;
}
System.out.println ("You got " + resultCount2 + " correct but in the wrong spot");
if (Secret_code[0] == Gameboard[0] && Secret_code[1] == Gameboard[1] && Secret_code[2] == Gameboard[2] && Secret_code[3] == Gameboard[3]){
break;
}
if (Tally > 10){
System.out.println ("You have exceeded your 10 tries. You lose. Please play again.");
break;
}
}
//Goodbye
System.out.println ("Thank you for playing.");
//Winner
if (Gameboard[0] == Secret_code[0] && Gameboard[1] == Secret_code[1] && Gameboard[2] == Secret_code[2] && Gameboard[3] == Secret_code[3])
System.out.println ("You have won this game of Mastermind!");
}
public static int[] Mystery () {
//Random
int secretcode = (int)(6*Math.random()) + 1;
int secretcode1 = (int)(6*Math.random()) + 1;
int secretcode2 = (int)(6*Math.random()) + 1;
int secretcode3 = (int)(6*Math.random()) + 1;
//Secretcode print
//System.out.println (secretcode); Used this for testing to make sure the codes were being outputted correctly.
//System.out.println (secretcode1);
//System.out.println (secretcode2);
//System.out.println (secretcode3);
//Array
int [] Secret_code;
Secret_code = new int [4];
Secret_code[0] = secretcode;
Secret_code[1] = secretcode1;
Secret_code[2] = secretcode2;
Secret_code[3] = secretcode3;
return Secret_code;
}
public static int[] Gameboard (int[] Secret_code) {
//Variable Declaration
int guess1, guess2, guess3, guess4;
Scanner Guess = new Scanner (System.in);
//User Guess's
System.out.println ("What is the number one peg in my code?");
guess1 = Guess.nextInt();
System.out.println ("What is the number two peg in my code?");
guess2 = Guess.nextInt();
System.out.println ("What is the number three peg in my code?");
guess3 = Guess.nextInt();
System.out.println ("What is the number four peg in my code?");
guess4 = Guess.nextInt();
//Array
int [] Gameboard;
Gameboard = new int [4];
Gameboard[0] = guess1; //This puts the user's guess's into an array which just makes it easier to compare with the secret code's array
Gameboard[1] = guess2;
Gameboard[2] = guess3;
Gameboard[3] = guess4;
return Gameboard;
}
public static int Comparing (int[] Secret_code, int[] Gameboard){
int resultCount = 0;
for (int i = 0; i < 4; i++) {
if (Gameboard[i] == Secret_code[i])
resultCount++;
}
System.out.println("You got " + resultCount + " correct in the right spot.");
return resultCount;
}
public static int Comparing2 (int[] Secret_code, int[] Gameboard){
int resultCount2 = 0;
int i = 0;
for (int e = 0; e < 4; e++) {
if (Gameboard[e] == Secret_code[i])
resultCount2++;
}
System.out.println ("You got " + resultCount2 + " correct but in the wrong spot");
return resultCount2;
}
}
答案 0 :(得分:0)
因为永远不会重置resultCount
。它应该在for循环的开头设置为0。 resultCount2
答案 1 :(得分:0)
您有重复的编码。你要求用户输入main()和Gameboard()方法。你设置resultCount = Comparing()(打印结果的地方),再次设置在main()中你第二次打印结果。
循环中的,你应该调用Comparing方法(计数器重置并完成打印)而不是计算:
//result Count
resultCount = Comparing (Secret_code, Gameboard);
//Result Count 2
resultCount2 = Comparing2 (Secret_code, Gameboard);