Java 智囊团资源泄漏?
我的代码运行良好,但我使用扫描仪的每一行都会警告我存在资源泄漏;'“userGuess”从未关闭,我不明白它的意思,可能需要一些帮助来解决它。此外,如果我的代码中还有其他值得修复的地方,我可以使用帮助。请注意,我对Java编程的知识有限。我也无法让我的TryCounter++工作Java 智囊团资源泄漏?,java,Java,我的代码运行良好,但我使用扫描仪的每一行都会警告我存在资源泄漏;'“userGuess”从未关闭,我不明白它的意思,可能需要一些帮助来解决它。此外,如果我的代码中还有其他值得修复的地方,我可以使用帮助。请注意,我对Java编程的知识有限。我也无法让我的TryCounter++工作 package masterMind2_1; import java.util.Scanner; public class MasterMind2_1 { public static void main(
package masterMind2_1;
import java.util.Scanner;
public class MasterMind2_1 {
public static void main(String[] args) {
System.out.println("This is MasterMind, a logic game");
System.out.println("To win you must guess correctly where each number is(The Numbers Range from 1-4)");
System.out.println("You will be told if you get one correct");
System.out.println("You will only get 10 tries, then you lose");
System.out.println("Lets begin");
//Declare Array
int [] answerArray;
answerArray= new int [4];
//Initialize Array
//Change these value to change the answers needed to win
answerArray[0]=2;
answerArray[1]=3;
answerArray[2]=2;
answerArray[3]=2;
// //Create Board
// System.out.println("-- -- -- --");
boolean guessedAll = false;
int guessedCount=0;
int tryCounter=0;
while(tryCounter<9 || !guessedAll){
System.out.println("What is the first Number?");
Scanner userGuess = new Scanner(System.in);
int num = userGuess.nextInt();
if (num==answerArray[0]) {
guessedCount++;
}
System.out.println("What is the Second Number?");
Scanner userGuess1 = new Scanner(System.in);
int num1 = userGuess1.nextInt();
if (num1==answerArray[1]) {
guessedCount++;
}
System.out.println("What is the Third Number?");
Scanner userGuess2 = new Scanner(System.in);
int num2 = userGuess2.nextInt();
if (num2==answerArray[2]) {
guessedCount++;
}
System.out.println("What is the Fourth Number?");
Scanner userGuess3 = new Scanner(System.in);
int num3 = userGuess3.nextInt();
if (num3==answerArray[3]) {
guessedCount++;
}
System.out.println("Your guess was "+ num+" "+num1+" "+num2+" "+num3);
if (num==answerArray[0]) {
System.out.println("First number was correct");
} else {
System.out.println("First number was incorrect");
}
if (num1==answerArray[1]) {
System.out.println("Second number was correct");
} else {
System.out.println("Second number was incorrect");
}
if (num2==answerArray[2]) {
System.out.println("Third number was correct");
} else {
System.out.println("Third number was incorrect");
}
if (num3==answerArray[3]) {
System.out.println("Fourth number was correct");
} else {
System.out.println("Fourth number was incorrect");
}
if (guessedCount==4) {
System.out.println("YAY you won!!");
guessedAll=true;
tryCounter=10;
} else {
System.out.println("Try again, except this time don't fail!");
guessedAll=false;
tryCounter++;
guessedCount=0;
}
}//What if I collected all of the values first
} //then told them if they were right or Wrong?
//Black and White Pegs?
//Fix TryCounter...Why isn't it working
}
谢谢你的帮助 错误消息告诉您从未在扫描仪对象上调用该方法。更糟糕的问题是,当您只需要一个扫描仪时,就创建了多个扫描仪 至于tryCounter不工作
while(tryCounter<9 || !guessedAll)
如果条件的任一部分为真,这将继续循环。我猜是这样!guessedAll的计算结果超过9次猜测,因此您的循环将继续运行。您需要将| |更改为&&以使其在9次尝试后停止循环。另外,打印出变量的值或使用调试器,这样你就可以在你期望的时候验证它们是否正在更改。有没有一种简单的方法来解决这个问题,或者这会很麻烦?你能告诉我将来如何解决它,这样我就不会再做了吗?@Zachary只需再次调用userGuess.nextInt来重用相同的扫描对象。不要为后续猜测创建另一个扫描仪。不工作不足以说明发生了什么。你期望它做什么?它在做什么呢?它没有像应该的那样计数。。。我越过了它应该计数和停止的点,但它并没有阻止我。。。我希望它在10次传球后停止。谢谢你我想你解决了我的问题。。。我以为| |是或者。。。我认为&&是而且。。。