我一直得到一个错误,说”D1D”未被定义?

huangapple go评论82阅读模式
英文:

I keep getting an error, saying that D1D has not been defined?

问题

import java.lang.Math;
import java.util.Scanner;

public class PlanetExplorer {
    public static void main(String[] args) {
        System.out.println("Explorable Planets:");
        System.out.println(" ");
        System.out.println("Mars:");
        System.out.println("Resources: 50 ");
        System.out.println("Danger: 10%");
        System.out.println(" ");
        System.out.println("Jupiter:");
        System.out.println("Resources: 75 ");
        System.out.println("Danger: 50%");
        System.out.println(" ");
        System.out.println("Titan:");
        System.out.println("Resources: 50 ");
        System.out.println("Danger: 30%");
        System.out.println(" ");
        System.out.println("Uranus:");
        System.out.println("Resources: 100 ");
        System.out.println("Danger: 60%");
        System.out.println(" ");
        System.out.println("Neptune:");
        System.out.println("Resources: 150 ");
        System.out.println("Danger: 75%");

        Scanner Planet1 = new Scanner(System.in);
        String P1 = Planet1.next();

        if ((P1.equals("Mars"))) {
            int min = 1;
            int max = 90;
            int range = max - min + 1;
            int D1R = 50;
            int D1D = (int)(Math.random() * range) + min;
        }

        if ((P1.equals("Jupiter"))) {
            int min = 1;
            int max = 50;
            int range = max - min + 1;
            int D1R = 100;
            int D1D = (int)(Math.random() * range) + min;
        }

        if ((P1.equals("Titan"))) {
            int min = 1;
            int max = 70;
            int range = max - min + 1;
            int D1R = 50;
            int D1D = (int)(Math.random() * range) + min;
        }

        if ((P1.equals("Uranus"))) {
            int min = 1;
            int max = 40;
            int range = max - min + 1;
            int D1R = 60;
            int D1D = (int)(Math.random() * range) + min;
        }

        if ((P1.equals("Neptune"))) {
            int min = 1;
            int max = 25;
            int range = max - min + 1;
            int D1R = 150;
            int D1D = (int)(Math.random() * range) + min;
        }

        if (D1D == 1) {
            int Mmin = 1;
            int Mmax = 3;
            int Mrange = Mmax - Mmin + 1;
            int Message = (int)(Math.random() * Mrange) + Mmin;
            if (Message == 1) {
                System.out.println("You Died!");
                System.out.println("Better Luck Next Time!");
            }
            else {
                if (Message == 2) {
                    System.out.println("Failure!");
                }
                else {
                    if (Message == 3) {
                        System.out.println("You Died!");
                        System.out.println("Replay?");
                    }
                }
            }
        }
    }
}
英文:

I am making a planet exploration text game, where you choose from 5 different planets, each one harder than the one above it, but also more resourceful. I have the Int Variable: "D1D", as the danger value, that is randomised. I have a death screen thing, that activates if D1D = 1, but it keeps saying, that it cant find D1D?
(I am using JDoodle)

import java.lang.Math;
import java.util.Scanner;
public class PlanetExplorer {
public static void main(String[] args) {
//Planets:
System.out.println("Explorable Planets: ");
System.out.println(" ");
System.out.println("Mars: ");
System.out.println("Resorces: 50 ");
System.out.println("Danger: 10%");
System.out.println(" ");
System.out.println("Jupiter: ");
System.out.println("Resorces: 75 ");
System.out.println("Danger: 50%");
System.out.println(" ");
System.out.println("Titan: ");
System.out.println("Resorces: 50 ");
System.out.println("Danger: 30%");
System.out.println(" ");
System.out.println("Uranus: ");
System.out.println("Resorces: 100 ");
System.out.println("Danger: 60%");
System.out.println(" ");
System.out.println("Neptune: ");
System.out.println("Resorces: 150 ");
System.out.println("Danger: 75%");
//Detecting the planet
Scanner Planet1 = new Scanner(System.in);
String P1 = Planet1.next();
// D_R = Day (1-5) Resources
// D_D = Day (1-5) Danger
//If Mars:
if ((P1.equals("Mars"))) {
int min = 1;
int max = 90;
int range = max - min + 1;
int  D1R = 50;
int D1D = (int)(Math.random() * range) + min;
// If Jupier
}
if ((P1.equals("Jupiter"))) {
int min = 1;
int max = 50;
int range = max - min + 1;
int  D1R = 100;
int D1D = (int)(Math.random() * range) + min;
//If Titan
}
if ((P1.equals("Titan"))) {
int min = 1;
int max = 70;
int range = max - min + 1;
int  D1R = 50;
int D1D = (int)(Math.random() * range) + min;
//If Uranus
}
if ((P1.equals("Uranus"))) {
int min = 1;
int max = 40;
int range = max - min + 1;
int  D1R = 60;
int D1D = (int)(Math.random() * range) + min;
//If Neptune
}
if ((P1.equals("Neptune"))) {
int min = 1;
int max = 25;
int range = max - min + 1;
int  D1R = 150;
int D1D = (int)(Math.random() * range) + min;
}
//Here \|/
if (D1D = 1) {
int Mmin = 1;
int Mmax = 3;
int Mrange = Mmax - Mmin + 1;
int Message = (int)(Math.random() * Mrange) + Mmin;
if (Message == 1) {
System.out.println("You Died!");
System.out.println("Better Luck Next Time!");
}
else {
if (Message == 2) {
System.out.println("Failure!");
}
else {
if (Message == 3) {
System.out.println("You Died!");
System.out.println("Replay?");
}
}
}
}
}
}

答案1

得分: 1

你正在局部声明变量(在每个 if 代码块内部声明)。
在 if 代码块之后,变量会再次消失。

你应该在之前声明这些变量:

public static void main(String[] args) {
    int min, max, range, Message, D1R, D1D;

并且只在 if 代码块中为它们赋值:

if ((P1.equals("Neptune"))) {
    min = 1;
    max = 25;
    range = max - min + 1;
    D1R = 150;
    D1D = (int)(Math.random() * range) + min;

由于变量名不一致,编译时可能会出现更多问题,但我希望这能帮助你。

英文:

you are declaring your variables locally (new within each if-block).
After the if-block the variable is gone again.

you should declare the variables before:

public static void main(String[] args) {
int min, max, range, Message, D1R, D1D;

and the only assign values to them in the if-blocks:

    if ((P1.equals("Neptune"))) {
min = 1;
max = 25;
range = max - min + 1;
D1R = 150;
D1D = (int)(Math.random() * range) + min;

you will have some more problems compiling this since the variable names are not consistent, but I hope this will help you.

huangapple
  • 本文由 发表于 2020年10月15日 21:02:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/64372177.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定