简体   繁体   English

我一直收到错误,说 D1D 尚未定义?

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

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.我正在制作一个星球探索文字游戏,你可以从 5 个不同的星球中进行选择,每一个都比它上面的更难,但也更足智多谋。 I have the Int Variable: "D1D", as the danger value, that is randomised.我有 Int 变量:“D1D”,作为危险值,它是随机的。 I have a death screen thing, that activates if D1D = 1, but it keeps saying, that it cant find D1D?我有一个死亡屏幕的东西,如果 D1D = 1 就会激活,但它一直说,它找不到 D1D? (I am using JDoodle) (我正在使用 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?");
                    }
                }
            }
        }





    }
}

you are declaring your variables locally (new within each if-block).您在本地声明变量(每个 if 块中的新变量)。 After the if-block the variable is gone again.在 if 块之后,变量又消失了。

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 块中为它们赋值:

    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.由于变量名称不一致,您在编译它时会遇到更多问题,但我希望这对您有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我不断收到错误消息说找不到符号-可变键盘 - I keep getting a error saying cannot find symbol - variable keyboard 获取Box2d实体已被触摸到的位置? - Getting location where a Box2d Body has been touched? 错误消息,指出已超出阵列 - Error message saying that the array has been exceeded 我正在创建一个2D游戏,我正在尝试导入一些字体,但我不断收到错误消息 - I am creating a 2d game, i am trying to import some fonts but i keep getting the error 我在 IntelliJ 上遇到一个错误,提示无法创建带有奇怪错误消息的类 - I've been getting an error on IntelliJ saying Cannot Create Class with an odd Error message 使用 Jagacy jar 自动化大型机应用程序时,收到错误消息,指出“KDB16104I 应用程序尚未定义到 CICS” - While automating a mainframe application with Jagacy jar, getting error message stating "KDB16104I Application has not been defined to CICS" 我不断收到编译器错误,说Rainfallmain.java:13:错误: <identifier> 预期。 - i keep getting a compiler error saying, Rainfallmain.java:13: error: <identifier> expected. 收到错误消息说我必须重写超类方法,但是父类具有该方法来重写 - Getting error saying I must override superclass method, but parent class has that method to override 我一直收到错误消息“找不到或加载主类gradebooktest.GradeBookTest”,我不确定为什么 - I keep getting an error saying “Could not find or load main class gradebooktest.GradeBookTest” I am not sure why 我一直收到一条错误消息,提示“无法从 int 转换为 Drawable”。 我正在尝试将图像分配给地点。 有没有办法解决这个问题? - I keep getting an error saying "cannot convert from int to Drawable". I'm trying to assign an image to places. Is there a way of working around this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM