使用If语句中定义的变量?

很抱歉,如果这听起来很愚蠢,但我似乎无法弄清楚如何使用我在If语句中定义的变量。

import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); //Prompt for Enchantment Type System.out.println("Armor/Tool/Weapon"); //Wait for response String input1 = scanner.nextLine(); if(input1 == "Armor"){ int type = 0; } else if(input1 == "Tool"){ int type = 1; } else if(input1 == "Weapon"){ int type = 2; } else { int type = 3; } //This is where I need to access the type int if(type == 1){ } } } 

我无法弄清楚如何在它的块之外使用类型字符串。 我知道我应该阅读范围和内容,但我真的希望有人向我解释。

  1. if的范围之外声明变量
  2. 使用.equals来比较字符串

固定代码:

 String input1 = scanner.nextLine(); int type; // scope if(input1.equals("Armor")){ int type = 0; } else if(input1.equals("Tool")){ int type = 1; } else if(input1.equals("Weapon")){ int type = 2; } else { int type = 3; } //This is where I need to access the String type if(type == 1){ } 

请注意,您也可以使用switch语句(仅限Java 7!):

 switch(input1) { case "Armor": type = 0; break; case "Tool": type = 1; break; ... 

另外,在您的情况下,您可以尝试indexOf

 import java.util.Arrays; List types = Arrays.asList("Armor", "Tool", "Weapon"); int type = types.indexOf(input1); if (type == -1) type = 3; // if it's not found 

如果要在if-statement之外使用变量, if-statement需要在if-statement之外声明它。

此外,您不使用==来比较String对象的内容相等性,只比较它们的identity 。 请改用.equals().equalsIgnoreCase()方法。

尽你所能,让你知道你在做什么。

 import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); //Prompt for Enchantment Type System.out.println("Armor/Tool/Weapon"); final int type; //Wait for response String input1 = scanner.nextLine(); if("Armor".equalsIgnoreCase(input1)){ type = 0; } else if("Tool".equalsIgnoreCase(input1)){ int type = 1; } else if("Weapon".equalsIgnoreCase(input1)){ type = 2; } else { type = 3; } //This is where I need to access the String type if(type == 1){ } } } 

更好的方法是打开Enum类型。

 public enum Item { ARMOR, TOOL, WEAPON } 

然后你可以将你的input1转换为Item枚举并打开它。

 final Item item = Item.valueOf(input1.toUpperCase()); switch(item) { case(ARMOR): // do stuff here break; case(TOOL): // do stuff here break; case(WEAPON): // do stuff here break; default: // do something with unrecognized input 

这将删除您现在在程序中的magic numbers

变量的范围在它定义的块内。因此,在if块中定义变量并使用它。

 int type = 0; if(input1 == "Armor"){ }else if(input1 == "Tool"){ type = 1; } else if(input1 == "Weapon"){ type = 2; } else { type = 3; } 

注意:还使用String equals方法来比较字符串而不是== 。 Equals方法检查两个字符串的内容是否相同,而==检查对象是否相等。

更新你的if和else块,如下所示:

 int type = 0; if("Armor".equals(input1)){ }else if("Tool".equals(input1){ type = 1; } else if("Weapon".equals(input1)){ type = 2; } else { type = 3; } 

您应该在if条件之外定义type

  int type = 0; if(("Armor".equalsIgnoreCase(input1)){ type = 0; } else if("Tool".equalsIgnoreCase(input1)){ type = 1; } else if("Weapon".equalsIgnoreCase(input1)){ type = 2; } else { type = 3; } if(type == 4) { } 

您的代码有多个错误。

  1. 你应该在循环之外定义类型变量
  2. 你应该将Strings与equals进行比较

 import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); //Prompt for Enchantment Type System.out.println("Armor/Tool/Weapon"); //Wait for response String input1 = scanner.nextLine(); int type; if(input1.equals("Armor")){ type = 0; } else if(input1.equals("Tool")){ type = 1; } else if(input1.equals("Weapon")){ type = 2; } else { type = 3; } //This is where I need to access the String type if(type == 1){ } } } 

您不应该超出范围访问。 只需在其中一个封闭范围内声明它。