Java开关状态,数组搜索 //带有菜单和对象数组的TestEmployeesProgram驱动程序。 导入java.util.*; 公共类测试员工计划{ 公共静态扫描仪控制台=新扫描仪(System.in); 公共静态void main(字符串[]args){ 最终int最大值=7; 员工[]=新员工[最大值]; int选择,k; 字符串名; 布尔未找到; 员工[0]=新经理(“杰里·布洛格斯”,“gfr”,5,38.5); 员工[1]=新经理(“乔·布洛格斯”,“民主德国”,4,32.5); 员工[2]=新管理人员(“玛丽·詹宁斯”,“nnv”,35.3,88.5,34.3); 员工[3]=新职员(“Brian Jones”,“bbl”,42.4,78.5,23.5,45.3); 员工[4]=新经理(“John Bloggs”,“gvr”,5,33.5); 员工[5]=新管理员(“Bridget Jennings”,“nvv”,45.3,98.5,36.3); 雇员[6]=新职员(“杰克·琼斯”,“bbb”,43.4,78.5,23.5,47.3); //初读 choice=showMenu(); //继续,直到4/4退出 while(选项!=MAX){ 开关(选择){ 案例1://经理 System.out.println(); System.out.printf(“%s%-16s%-10s%6s”,“姓名”,“Id”,“工时”,“工资”); System.out.println(“\n===================================================================”); 对于(k=0;k

Java开关状态,数组搜索 //带有菜单和对象数组的TestEmployeesProgram驱动程序。 导入java.util.*; 公共类测试员工计划{ 公共静态扫描仪控制台=新扫描仪(System.in); 公共静态void main(字符串[]args){ 最终int最大值=7; 员工[]=新员工[最大值]; int选择,k; 字符串名; 布尔未找到; 员工[0]=新经理(“杰里·布洛格斯”,“gfr”,5,38.5); 员工[1]=新经理(“乔·布洛格斯”,“民主德国”,4,32.5); 员工[2]=新管理人员(“玛丽·詹宁斯”,“nnv”,35.3,88.5,34.3); 员工[3]=新职员(“Brian Jones”,“bbl”,42.4,78.5,23.5,45.3); 员工[4]=新经理(“John Bloggs”,“gvr”,5,33.5); 员工[5]=新管理员(“Bridget Jennings”,“nvv”,45.3,98.5,36.3); 雇员[6]=新职员(“杰克·琼斯”,“bbb”,43.4,78.5,23.5,47.3); //初读 choice=showMenu(); //继续,直到4/4退出 while(选项!=MAX){ 开关(选择){ 案例1://经理 System.out.println(); System.out.printf(“%s%-16s%-10s%6s”,“姓名”,“Id”,“工时”,“工资”); System.out.println(“\n===================================================================”); 对于(k=0;k,java,Java,我正在运行程序,案例4中的姓名搜索直接进入默认的“未找到员工姓名”,不允许用户输入。我查看了代码,但找不到错误,有什么提示或帮助吗 //TestEmployeesProgram driver with menu & object array. import java.util.*; public class TestEmployeesProgram { public static Scanner console = new Scanner(System.in); public sta

我正在运行程序,案例4中的姓名搜索直接进入默认的“未找到员工姓名”,不允许用户输入。我查看了代码,但找不到错误,有什么提示或帮助吗

//TestEmployeesProgram driver with menu & object array.
import java.util.*;
public class TestEmployeesProgram {

public static Scanner console = new Scanner(System.in);

public static void main(String[] args) {

    final int MAX = 7;

    Employee employee[] = new Employee[MAX];

    int choice,k;
    String name;
    boolean notFound;

    employee[0] = new Manager("Jerry Bloggs","gfr",5,38.5);
    employee[1] = new Manager("Joe Bloggs","gdr",4,32.5);
    employee[2] = new Admin("Mary Jennings","nnv",35.3,88.5,34.3);
    employee[3] = new Clerk("Brian Jones","bbl",42.4,78.5,23.5,45.3);
    employee[4] = new Manager("John Bloggs","gvr",5,33.5);
    employee[5] = new Admin("Bridget Jennings","nvv",45.3,98.5,36.3);
    employee[6] = new Clerk("Jack Jones","bbb",43.4,78.5,23.5,47.3);

    //Initial Read
    choice = showMenu();

    //Continue Until 4/Exit
    while (choice != MAX) {

        switch (choice) {
        case 1://Manager

            System.out.println();
            System.out.printf("%s %-16s %-10s %6s","Name","Id","Hours Worked","Pay");
            System.out.println("\n==================================================");

            for (k = 0; k < MAX; ++k)
            {
                if (employee[k] instanceof Manager){ //use of string method instance of.


                    System.out.println(employee[k].toString());
                }
            }
            break;

        case 2://Administration

            System.out.println();
            System.out.printf("%s %-16s %-10s %6s %-19s","Name","Id","Hours Worked","Pay","Admin Quota");
            System.out.println("\n==================================================");

            for (k = 0; k < MAX; ++k)
            {
                if (employee[k] instanceof Admin){
                System.out.println(employee[k].toString());

                }
            }
            break;

        case 3://Clerk

            System.out.println();
            System.out.printf("%s %-16s %-10s %6s %-19s","Name","Id","Hours Worked","Pay","Admin Quota","Units Sold");
            System.out.println("\n==================================================");

            for (k = 0; k < MAX; ++k)
            {
                if (employee[k] instanceof Clerk){
                System.out.println(employee[k].toString());
                }
            }
            break;
case 4://名称搜索
系统输出打印(“输入员工姓名:”);
name=console.nextLine();
k=-1;
notFound=true;
而((k
有两个错误。第一点是:

        case 4://Name search

            System.out.print("Enter employee name: ");
            name = console.nextLine();

            k = -1;
            notFound = true;



            while ((k < MAX-1) && (notFound))
            {
                ++k;
                if (name == employee[k].getName()){

                    System.out.println();
                    System.out.printf("%s %-16s %-10s %6s %-19s","Name","Id","Hours Worked","Pay","Admin Quota","Units Sold");
                    System.out.println("\n==================================================");

                    System.out.println(employee[k].toString());
                    System.out.println();
                    notFound = false;
                }


            }//end of case 4 while.
             if (notFound){
                System.out.println("Employee name not found\n");
            }
            break;

        case 7://exit
            System.out.println("Program exiting...");
            System.exit(0);

        default:
            System.out.println("Invalid menu choice 1..3 of 7 to Exit");



        }//end of switch

        //sub read 
        choice = showMenu();

    }//end of while 






}//end of main

//Menu method for employee selection.
public static int showMenu()
{

    int choice;
    System.out.println();

    System.out.println("Employee Program Menu");

    System.out.println("1.Show Manager pay details ");
    System.out.println("2.Show Admin pay details ");
    System.out.println("3.Show Clerk pay details ");
    System.out.println("4.Search by employee name ");
    System.out.println("7.Exit");


    System.out.print("Enter option: ");
    choice = console.nextInt();


    return choice;
}
}
nextInt
方法不使用行尾字符。请尝试以下方法:

System.out.print("Enter option: ");
choice = console.nextInt();
 if (name == employee[k].getName())   
第二个错误是,在这里您应该使用
equals
而不是
=
来比较字符串:

System.out.print("Enter option: ");
String line = console.nextLine();
choice = Integer.parseInt(line);
请尝试以下方法:

System.out.print("Enter option: ");
choice = console.nextInt();
 if (name == employee[k].getName())   

=
操作符测试两个字符串是否为同一对象(即字符串在内存中的相同位置)。

有两个错误。第一点是:

        case 4://Name search

            System.out.print("Enter employee name: ");
            name = console.nextLine();

            k = -1;
            notFound = true;



            while ((k < MAX-1) && (notFound))
            {
                ++k;
                if (name == employee[k].getName()){

                    System.out.println();
                    System.out.printf("%s %-16s %-10s %6s %-19s","Name","Id","Hours Worked","Pay","Admin Quota","Units Sold");
                    System.out.println("\n==================================================");

                    System.out.println(employee[k].toString());
                    System.out.println();
                    notFound = false;
                }


            }//end of case 4 while.
             if (notFound){
                System.out.println("Employee name not found\n");
            }
            break;

        case 7://exit
            System.out.println("Program exiting...");
            System.exit(0);

        default:
            System.out.println("Invalid menu choice 1..3 of 7 to Exit");



        }//end of switch

        //sub read 
        choice = showMenu();

    }//end of while 






}//end of main

//Menu method for employee selection.
public static int showMenu()
{

    int choice;
    System.out.println();

    System.out.println("Employee Program Menu");

    System.out.println("1.Show Manager pay details ");
    System.out.println("2.Show Admin pay details ");
    System.out.println("3.Show Clerk pay details ");
    System.out.println("4.Search by employee name ");
    System.out.println("7.Exit");


    System.out.print("Enter option: ");
    choice = console.nextInt();


    return choice;
}
}
nextInt
方法不使用行尾字符。请尝试以下方法:

System.out.print("Enter option: ");
choice = console.nextInt();
 if (name == employee[k].getName())   
第二个错误是,在这里您应该使用
equals
而不是
=
来比较字符串:

System.out.print("Enter option: ");
String line = console.nextLine();
choice = Integer.parseInt(line);
请尝试以下方法:

System.out.print("Enter option: ");
choice = console.nextInt();
 if (name == employee[k].getName())   
=
操作符测试两个字符串是否为同一对象(即字符串在内存中的相同位置)

将此更改为

if (name == employee[k].getName())
将此更改为

if (name == employee[k].getName())

这是一个普遍的问题。当使用nextLine()读取整数值时,只读取整数值字符,\n并将其保留在缓冲区中,之后调用nextLine()时,它不会提示用户输入,因为它已经有一个“\n”

要避免这种情况,请在showMenu方法中执行以下操作

if (name.equals(employee[k].getName()))
使用nextInt()获得选择后

showMenu的最后一部分是:

 console.nextLine();
您应该使用.equals()方法来比较字符串,如下所示

 choice=console.nextInt();
 console.nextLine();
 return choice;

祝你好运:)

这是一个常见的问题。当使用nextLine()读取整数值时,只读取整数值字符,\n并将其保留在缓冲区中,之后调用nextLine()时,它不会提示用户输入,因为它已经有一个“\n”

为了避免这种情况,,