Java 如何比较字符串

Java 如何比较字符串,java,Java,我试图让用户按下一个数字,然后输入他们想要看到的可用选项的字符串。到目前为止,一切都正常,除了用户键入字符串时,它不会给出首选输出*显示电子邮件而不是颜色、用户id等。这是我的代码。。再次感谢 public static void printStuff(Record[] objects) { Scanner in = new Scanner(System.in); System.out.println("Enter the number and record name you wou

我试图让用户按下一个数字,然后输入他们想要看到的可用选项的字符串。到目前为止,一切都正常,除了用户键入字符串时,它不会给出首选输出*显示电子邮件而不是颜色、用户id等。这是我的代码。。再次感谢

public static void printStuff(Record[] objects) {
   Scanner in = new Scanner(System.in);
   System.out.println("Enter the number and record name you would like to see");
   int x = in.nextInt();
   String bean = in.next();


        if (x == 1 || x == 2 || x == 3 || x == 4 || x == 5 && bean.equals("email")) {
            System.out.println(objects[x].getEmail());
        }
       else if (x == 1 || x == 2 || x == 3 || x == 4 || x == 5 && bean.equals("name")) {
            System.out.println(objects[x].getfirstName());
        }

       else if (x == 1 || x == 2 || x == 3 || x == 4 || x == 5 && bean.matches("last name")) {
            System.out.println(objects[x].getlastName());
        }

       else if (x == 1 || x == 2 || x == 3 || x == 4 || x == 5 && bean.matches("balance")) {
            System.out.println(objects[x].getBalance());
        }

       else if (x == 1 || x == 2 || x == 3 || x == 4 || x == 5 && bean.matches("color")) {
            System.out.println(objects[x].getColor());
        }

       else if (x == 1 || x == 2 || x == 3 || x == 4 || x == 5 && bean.matches("idnumber")) {
            System.out.println(objects[x].getnumID());
        }
 }

在所有if条件中,
&&
的优先级高于
|
,因此必须将条件更改为:

(x==1 | | x==2 | | x==3 | | x==4 | | x==5)和&bean.equals(“电子邮件”)

如果
x
是1到5中的某个值,并且bean等于
“email”
,则这与您想要的逻辑相对应。但是,请查看比较运算符,因为您可以将其简化为:


(1在所有if条件中,
和&
的优先级高于
|
,因此必须将条件更改为:

(x==1 | | x==2 | | x==3 | | x==4 | | x==5)和&bean.equals(“电子邮件”)

如果
x
是1到5中的某个值,并且bean等于
“email”
,则这与您想要的逻辑相对应。但是,请查看比较运算符,因为您可以将其简化为:


(1只需适当添加括号即可

(x == 1 || x == 2 || x == 3 || x == 4 || x == 5) && bean.equals("email")

只需适当地添加括号

(x == 1 || x == 2 || x == 3 || x == 4 || x == 5) && bean.equals("email")

这就是你可以通过减少条件来解决的方法。这仍然可以做得更好。如果其他人杀了我

public static void printStuff(Record[] objects) {
    Scanner in = new Scanner(System.in);
    System.out.println("Enter the number and record name you would like to see");

    int x = in.nextInt();

    String bean = in.next();

    Record record = objects[x];

    if (x >= 1 && x <= 5) {
        if (bean.equals("email")) {
            System.out.println(record.email);
        } else if (bean.equals("name")) {
            System.out.println(record.firstName);
        } else if (bean.equals("last name")) {
            System.out.println(record.lastName);
        } else if (bean.equals("balance")) {
            System.out.println(record.balance);
        } else if (bean.equals("idnumeric")) {
            System.out.println(record.numId);
        }
    }
}
publicstaticvoidprintstuff(记录[]对象){
扫描仪输入=新扫描仪(系统输入);
System.out.println(“输入您希望看到的编号和记录名称”);
int x=in.nextInt();
stringbean=in.next();
记录=对象[x];

如果(x>=1&&x这就是你可以通过减少你的条件来解决它的方法。这仍然可以做得更好。如果其他人杀了我

public static void printStuff(Record[] objects) {
    Scanner in = new Scanner(System.in);
    System.out.println("Enter the number and record name you would like to see");

    int x = in.nextInt();

    String bean = in.next();

    Record record = objects[x];

    if (x >= 1 && x <= 5) {
        if (bean.equals("email")) {
            System.out.println(record.email);
        } else if (bean.equals("name")) {
            System.out.println(record.firstName);
        } else if (bean.equals("last name")) {
            System.out.println(record.lastName);
        } else if (bean.equals("balance")) {
            System.out.println(record.balance);
        } else if (bean.equals("idnumeric")) {
            System.out.println(record.numId);
        }
    }
}
publicstaticvoidprintstuff(记录[]对象){
扫描仪输入=新扫描仪(系统输入);
System.out.println(“输入您希望看到的编号和记录名称”);
int x=in.nextInt();
stringbean=in.next();
记录=对象[x];

如果(x>=1&&x尝试一下,请始终避免重新键入案例,并提高代码的效率

public static void printStuff(Record[] objects) {
   Scanner in = new Scanner(System.in);
   System.out.println("Enter the number and record name you would like to see");
   int x = in.nextInt();
   String bean = in.next();


        if (x >= 1 && x =< 5 ) {
            if (bean.equals("email"))
               System.out.println(objects[x].getEmail());
            else if (bean.equals("name"))
               System.out.println(objects[x].getfirstName());
            else if (bean.matches("last name"))
               System.out.println(objects[x].getlastName());
            else if (bean.matches("balance"))
               System.out.println(objects[x].getBalance());
            else if (bean.matches("color"))
               System.out.println(objects[x].getColor());
            else if (bean.matches("idnumber"))
               System.out.println(objects[x].getnumID());
        }
 }
publicstaticvoidprintstuff(记录[]对象){
扫描仪输入=新扫描仪(系统输入);
System.out.println(“输入您希望看到的编号和记录名称”);
int x=in.nextInt();
stringbean=in.next();
如果(x>=1&&x=<5){
if(bean.equals(“电子邮件”))
System.out.println(对象[x].getEmail());
else if(bean.equals(“name”))
System.out.println(对象[x].getfirstName());
else if(bean.matches(“姓氏”))
System.out.println(对象[x].getlastName());
else if(bean.matches(“余额”))
System.out.println(对象[x].getBalance());
else if(bean.matches(“color”))
System.out.println(对象[x].getColor());
else if(bean.matches(“idnumber”))
System.out.println(对象[x].getnumID());
}
}

尝试一下,始终避免重新键入案例,使您的代码更有效率

public static void printStuff(Record[] objects) {
   Scanner in = new Scanner(System.in);
   System.out.println("Enter the number and record name you would like to see");
   int x = in.nextInt();
   String bean = in.next();


        if (x >= 1 && x =< 5 ) {
            if (bean.equals("email"))
               System.out.println(objects[x].getEmail());
            else if (bean.equals("name"))
               System.out.println(objects[x].getfirstName());
            else if (bean.matches("last name"))
               System.out.println(objects[x].getlastName());
            else if (bean.matches("balance"))
               System.out.println(objects[x].getBalance());
            else if (bean.matches("color"))
               System.out.println(objects[x].getColor());
            else if (bean.matches("idnumber"))
               System.out.println(objects[x].getnumID());
        }
 }
publicstaticvoidprintstuff(记录[]对象){
扫描仪输入=新扫描仪(系统输入);
System.out.println(“输入您希望看到的编号和记录名称”);
int x=in.nextInt();
stringbean=in.next();
如果(x>=1&&x=<5){
if(bean.equals(“电子邮件”))
System.out.println(对象[x].getEmail());
else if(bean.equals(“name”))
System.out.println(对象[x].getfirstName());
else if(bean.matches(“姓氏”))
System.out.println(对象[x].getlastName());
else if(bean.matches(“余额”))
System.out.println(对象[x].getBalance());
else if(bean.matches(“color”))
System.out.println(对象[x].getColor());
else if(bean.matches(“idnumber”))
System.out.println(对象[x].getnumID());
}
}

也许更容易阅读:

if (1 <= x && x <= 5) {
    switch (bean) {
        case "email": 
            System.out.println(record.email);
            break;
        case "name": 
            System.out.println(record.firstName);
            break;
        ...
    }
}
或者,如果未知
bean
不执行任何操作:

if (1 <= x && x <= 5) {
    switch (bean) {
        case "email" -> System.out.println(record.email);
        case "name" -> System.out.println(record.firstName);
        ...
    }
}
if(1 System.out.println(record.firstName);
...
}
}

也许更容易阅读:

if (1 <= x && x <= 5) {
    switch (bean) {
        case "email": 
            System.out.println(record.email);
            break;
        case "name": 
            System.out.println(record.firstName);
            break;
        ...
    }
}
或者,如果未知
bean
不执行任何操作:

if (1 <= x && x <= 5) {
    switch (bean) {
        case "email" -> System.out.println(record.email);
        case "name" -> System.out.println(record.firstName);
        ...
    }
}
if(1 System.out.println(record.firstName);
...
}
}

在表达式中使用
|
&
时,始终使用括号来说明优先级是什么。了解=,以及如何使用变量和nestid以避免重复。在表达式中使用
|
&
时,始终使用括号来说明优先级是什么is.。并了解关于=,以及如何使用变量和nestid if来避免重复。使用switch expression无法处理没有已知值的
bean
。@Andreas可以通过使用
default:
来处理,仅当需要打印某些内容时,问题建议的代码不是这样(没有最终的
else
子句)。在这种情况下,不需要
default
。如果值与任何情况都不匹配,代码将继续。开关表达式(上面的第二个代码段)需要
default
,除非给出了所有值(仅
enum
s):,因为表达式总是必须有结果JEP:“开关表达式的大小写必须是详尽的;对于任何可能的值,必须有匹配的开关标签。”使用switch expression无法处理没有已知值的
bean
。@Andreas它可以通过使用
default:
仅当需要打印某些内容时,问题所建议的代码不是这样的(没有final
else
子句)。在这种情况下,不需要
default
。如果