어제 제가 while 루프에 관한 질문에 this을 물었더니 setter를 사용하여 내 메인 메소드에서 "correct"를 true로 설정해야한다고 들었습니다. 나는 세터와 게터에 대한 연구를했고 나는 완전히 잃어 버렸습니다. 그들이 정확히 무엇을하고이 상황에서 어떻게 사용합니까?Java 루프가 setter를 필요로합니다
편집 : Ben 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)
}
질문 질문 클래스의 하나의 방법은 : 자바, 특히 OO 내이 몇 가지 기초의 빈약 한 이해를 가지고있는 것처럼
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;
}
}
세터와 게터 올바른 변수에 액세스하는
questionsObject
에이 게터에 액세스 할 수 있습니다, 적어도 게터를 만들 때마다 반에 설명되어 있습니다 괜찮은 튜토리얼, 그들이 무엇인지 모르는 것에 대한 당신의 변명은 무엇입니까? – Kayaman게터와 설정자는 클래스의 변수에 대한 접근 자 및 변경자입니다. 이것들은 OOP (캡슐화)의 표준입니다. –
글쎄 @ 카야 만 나의 "변명"은 내가 이해하지 못한다는 것이다. 위에서 말했듯이 나는 그들에 대해 배우려고 노력했지만 이해하지 못합니다. 누군가가 뭔가 문제가있을 수 있음을 이해하는 것은 너무 미친 짓입니다. – KobiF