如何使用在if语句之外定义的变量

时间:2015-07-05 14:51:24

标签: java

我一直在尝试制作一个计算器,当我在if语句中定义变量时,它说:

  

线程“main”中的异常java.lang.Error:未解决的编译问题:       BlockNum无法解析为变量       在firsttry.Main.main(Main.java:57)

现在我知道你必须在if语句之外定义变量,因为变量范围,但当我尝试这样做时:

package firsttry;

import java.util.Scanner;

public class Main {
    public static void main(String args[]){
        int BlockNum;
        BlockNum = 1;

我收到错误:

  

线程“main”中的异常java.lang.Error:未解决的编译问题:       重复的局部变量BlockNum
      在firsttry.Main.main(Main.java:36)

那么我究竟如何解决这个问题呢?从我所看到的,变量范围的唯一解决方案是在if语句之外定义变量,但它似乎对我没用?

我的完整代码:

package firsttry;

import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        int BlockNum;
        BlockNum = 1;
        csv bGather = new csv();
        ToolName atool = new ToolName();
        Scanner input = new Scanner(System.in);
        //Welcoming Message
        System.out.println("Welcome to Minecraft Build/excavate calculator"
            + "\nIts suggested you use tools that are suitable for the block"
            + "\nthat you are breaking as its much faster"
            + "");

        //GATHERES USER VARIABLES
        System.out.println("What block are you gathering? (use minecraft block ids)");
        String block = input.next();

        System.out.println("What tool are you using , 1 for using hand and up to 4 for diamond");
        int tool = input.nextInt();
        System.out.println("Do you want to enter the dimensions of the area you are excavating"
            + "or just the exact block number"
            + "1 for dimensions "
            + "2 for exact number");
        int TypeOfSelection = input.nextInt();
        if (TypeOfSelection == 1) {
            System.out.println("Height of the area");
            int Height = input.nextInt();
            System.out.println("length of the area");
            int length = input.nextInt();
            System.out.println("width of the area");
            int width = input.nextInt();
        } else if (TypeOfSelection == 2) {
            System.out.println("Exact amount of blocks");
            int BlockNum = input.nextInt();
        } else {
            System.out.println("ERRORRRR");
        }

        //CSV FILE STUF
        String ToolName = atool.NameOfTool(tool);
        String blockname = bGather.name(block);

        double blockbreak = bGather.speed(tool,block);
        input.close();

        //Overall calculations
        if (TypeOfSelection == 2) {
            System.out.println(BlockNum);
        }

        System.out.println("You are gatherering " + blockname + " using a " +       ToolName + " \nand it will take you " + blockbreak + " Seconds per block");
    }
}

3 个答案:

答案 0 :(得分:1)

else if (TypeOfSelection == 2){
        System.out.println("Exact amount of blocks");
        int BlockNum = input.nextInt();
}

如果int BlockNum与您在顶部声明的变量相同,请删除int部分。

else if (TypeOfSelection == 2){
    System.out.println("Exact amount of blocks");
    BlockNum = input.nextInt();
}

答案 1 :(得分:0)

}else if (TypeOfSelection == 2){
            System.out.println("Exact amount of blocks");
            int BlockNum = input.nextInt();
        }    

在上面的代码段中,BlockNum与以下代码中的代码发生冲突:

int BlockNum;
BlockNum = 1;

因为,在if内,它看到已经定义了具有相同名称的变量,

解决方案:

在任何一种情况下更改变量的名称(例如,使用 int BlockNum1 = input.nextInt();)。

答案 2 :(得分:0)

您正在重新声明具有相同名称BlockNum的变量。

如果您在if之外引用相同的变量,则只需调用

}else if (TypeOfSelection == 2){
        System.out.println("Exact amount of blocks");
         BlockNum = input.nextInt();
    }

如果你没有引用同一个变量,你可以重命名其中一个变量,如

  }else if (TypeOfSelection == 2){
            System.out.println("Exact amount of blocks");
             int internalBlockNum = input.nextInt();
        }

您无法在具有相同名称的方法中创建多个变量。