У вас есть
if (...);
{
Таким образом, пустой оператор, когда условие истинно (;
), сопровождаемый блоком { ... }
, всегда вводится.
Переменная strInput
является слишком глобальным и изменяется везде и может позже ввести неверный оператор if.
В одном решении используется else if
:
String strInput=MyInput.readLine(); //gathers ihnput
if (strInput.equals("Exit"))
{
//ends game
endGame=999;
} else if (strInput.equals("joe")) {
//display hp and fishing rod damage
strInput="";
System.out.println("Joe has a total hp of " +joe.getHp());
System.out.println("Joes fishingrod does a total of "+joe.fr.getDamage() +" damage");
} else if (strInput.equals("f")) {
//display career total of fish caught
strInput="";
System.out.println("You caught a total of"+joe.GetCatches()+"fishes");
}
Вы также можете удалить переменную:
switch (MyInput.readLine()) { //gathers ihnput
case "Exit":
//ends game
endGame=999;
break;
case "joe":
//display hp and fishing rod damage
System.out.println("Joe has a total hp of " +joe.getHp());
System.out.println("Joes fishingrod does a total of "+joe.fr.getDamage() +" damage");
break;
case "f":
//display career total of fish caught
System.out.println("You caught a total of"+joe.GetCatches()+"fishes");
breaK;
default:
System.out.println("Wrong input");
}
Подойдет и конкретная локальная переменная.
String menuAnswer = MyInput.readLine();
switch (menuAnswer) {
...
default:
System.out.println("Wrong input: " + menuAnswer);
}
Соглашения Java В отличие от других языков, польская запись strInput
не используются в Java. Также переменные по соглашению начинаются с маленькой буквы. Переменные объявляются как можно ближе к первому использованию. Имена классов начинаются с заглавной буквы.