昨天我问了this有关while循环没有结束的问题,并被告知我需要在使用setter的主要方法内设置“correct”为true。我对制定者和获得者进行了一些研究,并且我完全迷失了。他们究竟做了什么以及如何在这种情况下使用它们?Java循环需要setter
编辑:谢谢本Wainwright为您的答案!
主要方法:
while (lives > 0 && correct == false) {
startTime = System.currentTimeMillis();
timeObject.time2();
levelinfoObject.levelInfo(currentlevel);
timeObject.time1();
levelinfoObject.livesInfo(lives);
timeObject.time1();
levelinfoObject.skipsInfo(skips);
timeObject.time2();
questionsObject.questionOne(lives, correct, choice, skips, currentlevel)
}
问题在问题类中的一个方法:
public void questionOne(int lives, boolean correct, String choice, int skips, int currentlevel) {
Scanner scanner = new Scanner(System.in);
System.out.println(" ");
System.out.println("Question: If there are 6 apples in a tree and you take 4, how many do you have?");
timeObject.time2();
System.out.println("A: 3");
System.out.println("B: 4");
System.out.println("C: 2");
System.out.println("D: 6");
while (correct == false && lives > 0) {
choice = scanner.nextLine();
switch(choice) {
case "a":
System.out.println("WRONG! Try again.");
lives = lives - 1;
break;
case "b":
System.out.println("CORRECT! You have the 4 you took obviously.");
correct = true;
break;
case "c":
System.out.println("WRONG! Try again.");
lives = lives - 1;
break;
case "d":
System.out.println("WRONG! Try again.");
lives = lives - 1;
break;
case "skip":
if (skips > 0) {
System.out.println("You have skipped level " + currentlevel + "!");
skips = skips - 1;
correct = true;
}
else {
System.err.println("You do not have any skips left!");
}
break;
default:
System.err.println("Please type an answer.");
break;
}
}
getter和setter方法访问正确的变量在每半个小时解释体面的教程,你不知道他们是什么借口? – Kayaman
Getters和setter是类的变量的访问器和变体。这些对于OOP(封装)是非常标准的。 –
那么@Kayaman我的“借口”是我不明白。就像我上面所说的,我尝试了解它们,但我不明白。对于你来说太疯狂了,因为有人可能会遇到麻烦。 – KobiF