import java.util.Scanner;
public class InputTest {
static void monster() {
System.out.println("You ran into a monster!");
System.out.println("He did 10 damage!");
health -= 10;
System.out.println("You have " + health + " health left!");
}
static void potion() {
System.out.println("You ran into a potion!");
System.out.println("It healed 10 damage!");
health += 10;
System.out.println("You have " + health + " health left!");
}
static void bag() {
System.out.println("You ran into a bag!");
System.out.println("It did... Nothing!");
}
static void random() {
double random = Math.random();
if(random >= 0 && random <= 0.33) {
monster();
} else if(random >= 0.24 && random <= 0.66) {
potion();
} else {
bag();
}
}
static void turn() {
System.out.println("Do you want to go left or right?");
Scanner turn = new Scanner(System.in);
String leftOrRight = turn.nextLine();
if(leftOrRight.equals("Right")) {
System.out.println("You turned right and...");
random();
} else if(leftOrRight.equals("Left")){
System.out.println("You turned left and...");
random();
} else {
System.out.println("You entered an invalid answer...");
System.exit(0);
}
}
public static void main(String[] args) {
int health = 50;
for(int i = 0; i < 10; i++) {
turn();
}
System.out.println("You won!");
}
}
这是非工作代码。 怪物();和健康();方法是我遇到麻烦的方法。 它输出我没有宣称'健康'。 我明白为什么说这个,有没有办法解决这个问题。 它只是不允许我在方法中使用健康整数。 如你所见,它应该采取健康并减去10。
答案 0 :(得分:-1)
使health成为类级静态变量。
import java.util.Scanner;
public class InputTest {
private static int health = 50;
/*rest of the methods as they are*/
}
答案 1 :(得分:-1)
请尝试以下代码:
import java.util.Scanner;
public class Test {
private static int health = 50;
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
turn();
}
System.out.println("You won!");
}
static void monster() {
System.out.println("You ran into a monster!");
System.out.println("He did 10 damage!");
health -= 10;
System.out.println("You have " + health + " health left!");
}
static void potion() {
System.out.println("You ran into a potion!");
System.out.println("It healed 10 damage!");
health += 10;
System.out.println("You have " + health + " health left!");
}
static void bag() {
System.out.println("You ran into a bag!");
System.out.println("It did... Nothing!");
}
static void random() {
double random = Math.random();
if (random >= 0 && random <= 0.33) {
monster();
} else if (random >= 0.24 && random <= 0.66) {
potion();
} else {
bag();
}
}
static void turn() {
System.out.println("Do you want to go left or right?");
Scanner turn = new Scanner(System.in);
String leftOrRight = turn.nextLine();
// turn.close();
if (leftOrRight.equals("Right")) {
System.out.println("You turned right and...");
random();
} else if (leftOrRight.equals("Left")) {
System.out.println("You turned left and...");
random();
} else {
System.out.println("You entered an invalid answer...");
System.exit(0);
}
}
}
答案 2 :(得分:-1)
见下面的修改后的代码。
import java.util.Scanner;
public class InputTest {
private static int health = 50;
static void monster() {
System.out.println("You ran into a monster!");
System.out.println("He did 10 damage!");
health -= 10;
System.out.println("You have " + health + " health left!");
}
static void potion() {
System.out.println("You ran into a potion!");
System.out.println("It healed 10 damage!");
health += 10;
System.out.println("You have " + health + " health left!");
}
static void bag() {
System.out.println("You ran into a bag!");
System.out.println("It did... Nothing!");
}
static void random() {
double random = Math.random();
if (random >= 0 && random <= 0.33) {
monster();
} else if (random >= 0.24 && random <= 0.66) {
potion();
} else {
bag();
}
}
static void turn() {
System.out.println("Do you want to go left or right?");
Scanner turn = new Scanner(System.in);
String leftOrRight = turn.nextLine();
if (leftOrRight.equals("Right")) {
System.out.println("You turned right and...");
random();
} else if (leftOrRight.equals("Left")) {
System.out.println("You turned left and...");
random();
} else {
System.out.println("You entered an invalid answer...");
System.exit(0);
}
}
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
turn();
}
System.out.println("You won!");
}
}
此致