Java 使用If语句中定义的变量?
如果这听起来很愚蠢,很抱歉,但是我似乎不知道如何使用我在if语句中定义的变量Java 使用If语句中定义的变量?,java,Java,如果这听起来很愚蠢,很抱歉,但是我似乎不知道如何使用我在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.pri
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){
}
}
}
我不知道如何在它的块之外使用类型字符串。我知道我应该阅读范围和内容,但我真的希望有人能给我解释一下。你应该在
if
条件之外定义类型
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) {
}
变量的范围在其定义的块内。因此,从if块中定义变量并使用它
int type = 0;
if(input1 == "Armor"){
}else if(input1 == "Tool"){
type = 1;
}
else if(input1 == "Weapon"){
type = 2;
}
else {
type = 3;
}
注意:也使用Stringequals
方法来比较字符串,而不是=
。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语句
之外使用变量,则需要在If语句
之外声明它
另外,您也不会使用=
来比较字符串
对象的内容是否相等,这只会比较它们的标识
。请改用.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
转换为项
枚举并打开它
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
这将删除您当前程序中的幻数。您不应该从范围外访问。
只需在其中一个封闭作用域中声明它
在if
的范围之外声明变量
使用.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){
}
注意,您还可以使用开关语句(仅在Java 7中!):
此外,在您的情况下,您可以尝试indexOf
:
import java.util.Arrays;
List<String> types = Arrays.asList("Armor", "Tool", "Weapon");
int type = types.indexOf(input1);
if (type == -1) type = 3; // if it's not found
导入java.util.array;
列表类型=数组。asList(“盔甲”、“工具”、“武器”);
int type=types.indexOf(input1);
如果(类型==-1)类型=3;//如果找不到的话
您的代码有多个错误
您应该在循环之外定义类型变量
您应该将字符串与相等的字符串进行比较
如果循环?没有这样的事…对不起。。。更正了+1,也许是打开字符串的好时机。@PeterLawrey仅从Java 7开始。@JLO这里肯定有问题,但不是要问的问题。@thegrinner:是的,我看到了代码并标记为“重复”,但没有看到其他问题……投票结果是,似乎不止一个人通过了,请看评论!谁读了所有的老问题!不@captain,绝对不是。@captain不。这完全是错误的。仅供参考,由于某种原因,列表标记破坏了您的代码块。我用一个段落标记修复了它。当你创建它时,你需要给int-type
赋值,否则它会在以后的代码中生成一个错误。@Vallentin不,如果你写final int-type
,它就不会生成错误,但是如果你只写int-type
,你的IDE就会抛出错误“局部变量类型可能还没有初始化”,所以是的!我的eclipse没有初始化。而且,当它未初始化+1时,就不会显示它在Java7中是如何完成的。
import java.util.Arrays;
List<String> types = Arrays.asList("Armor", "Tool", "Weapon");
int type = types.indexOf(input1);
if (type == -1) type = 3; // if it's not found
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){
}
}
}