如何比较字符串

我试图将其带到用户按下数字的位置,然后他们输入他们想要查看的可用选项的字符串。到目前为止,一切正常,除了当用户输入字符串时,它没有给出首选输出.. *显示电子邮件而不是颜色、用户 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());

        }

 }


千万里不及你
浏览 107回答 3
3回答

大话西游666

尝试一下,始终避免重新输入案例并使您的代码更加高效。public static void printStuff(Record[] objects) {&nbsp; &nbsp;Scanner in = new Scanner(System.in);&nbsp; &nbsp;System.out.println("Enter the number and record name you would like to see");&nbsp; &nbsp;int x = in.nextInt();&nbsp; &nbsp;String bean = in.next();&nbsp; &nbsp; &nbsp; &nbsp; if (x >= 1 && x =< 5 ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (bean.equals("email"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(objects[x].getEmail());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (bean.equals("name"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(objects[x].getfirstName());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (bean.matches("last name"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(objects[x].getlastName());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (bean.matches("balance"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(objects[x].getBalance());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (bean.matches("color"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(objects[x].getColor());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (bean.matches("idnumber"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(objects[x].getnumID());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;}

眼眸繁星

在所有 if 条件中,&&的优先级高于||,因此必须将条件更改为:(x == 1 || x == 2 || x == 3 || x == 4 || x == 5) && bean.equals("email").这对应于您想要的逻辑,如果x1 到 5 中的某个值 AND bean 等于"email"。但是,请研究比较运算符,因为您可以将其简化为:(1 <= x && x <= 5) && bean.equals("email").

牧羊人nacy

也许更容易阅读:if (1 <= x && x <= 5) {    switch (bean) {        case "email":             System.out.println(record.email);            break;        case "name":             System.out.println(record.firstName);            break;        ...    }}使用switch 表达式(Java 13, --enable-preview):if (1 <= x && x <= 5) {    System.out.println(switch (bean) {        case "email" -> record.email;        case "name" -> record.firstName;        case "last name"-> record.lastName;        ...        default -> "unrecognized " + bean;        // default -> throw new IllegalArgumentException(...);    });}或者,如果对未知不执行任何操作bean:if (1 <= x && x <= 5) {    switch (bean) {        case "email" -> System.out.println(record.email);        case "name" -> System.out.println(record.firstName);        ...    }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java