1
개를 돌봐야하는 Java로 게임을 만들고 있습니다. 내 게임 메서드 자체를 호출하여이 메서드에서 내용을 여러 번 복사하여 붙여 넣지 않아도됩니다. 문제는 정수 값이 각 선택 항목을 통해 더 해지고 빼기 때문에 메서드를 다시 호출하면 변경된 값이 다시 기본값으로 변경되기 때문에 내가 선언 한 두 정수 값을 둘러 보는 방법을 모르겠다는 것입니다.정수 변수 재설정 방지
import java.io.File;
import java.util.Scanner;
public class Main {
public static Scanner kybd = new Scanner(System.in);
public static void main(String[] args) {
game();
}
public static void game() {
Integer diet;
diet = 5;
Integer happiness;
happiness = 10;
System.out.println("");
System.out.println("Dog \t Hunger: " + diet
+ "\n \t Happiness: " + happiness);
System.out.println("");
System.out.println("1. Feed \n2. Play \n3. Ignore");
System.out.println("");
System.out.print("> ");
String input = kybd.nextLine();
if (input.equalsIgnoreCase("1")) {
diet++;
game(); // This is supposed to go to the
// beginning with the changed value of diet.
} else if (input.equalsIgnoreCase("2")) {
happiness++;
game(); // This is supposed to go to the
// beginning with the changed value of happiness.
} else if (input.equalsIgnoreCase("3")) {
happiness--;
diet--;
game(); // This is supposed to go to the
// beginning with the changed value of happiness.
} else {
System.out.println("Invalid Input");
game(); // This is supposed to go the beginning
// where you can change your input but
// still has your changed values.
}
if (diet <= 0);
{
System.out.println("Your dog died because it did not eat.");
game(); // This is supposed to go to the beginning
// with the default values.
}
if (diet > 10);
{
System.out.println("Your dog died because it was overfed.");
game(); // This is supposed to go to the
// beginning with the default values.
}
if (happiness <= 0);
{
diet--;
System.out.println("Your dog is no longer happy. He will not eat.");
}
{
if (happiness > 10);
System.out.println("Your dog died because it was too excited.");
game(); // This is supposed to go to the
// beginning with the default values.
}
}
}
도움을 주셔서 감사합니다.하지만 개를 죽게 만들 수 있습니까? 다이어트가 0과 10을 넘을 때마다, 개는 죽지 않을 것이고 숫자는 계속 계속됩니다. – artshof
첫 번째 답변에서 내 편집을 참조하십시오. 감사합니다 – mistic
@artshof 당신은 개를 죽게해서는 안됩니다. 계속해서 살게하고 변수에 대한 이해할 수없는 가치로 논리를 무시하게하십시오. 그렇지 않으면'sendDogBackInTime'을 대신 사용하십시오. :-) – afsantos