请问:关于二分法查询,和顺序输出该怎么搞?代码如下,2个类。该如何改进?

package employee2;


import java.util.Scanner;



public class Main {

static employee []per=new employee[10];

public int empid;

public static void main(String[] args) {


employee.printMenu();

System.out.println("Please choose number 1~4 to do next:");

Scanner input = new Scanner(System.in);

int j = 0;

while(input.hasNextInt()){


int chooseNumber = input.nextInt();

if(chooseNumber != 4){

switch(chooseNumber){

case 1:

System.out.println("请输入员工号 :");

int empid=input.nextInt();

       

System.out.println("请输入员工姓名 :");

String empname=input.next();

       

System.out.println("请输入公司名称 :");

String cname=input.next();

       

System.out.println("请输入工资 :");

int salary=input.nextInt();

       

employee p = new employee(empid, empname, cname, salary);

per[j] = p;

j++;

break;

case 2:

System.out.println("请输入要查询的员工号 :");

int empid1=input.nextInt();

int lower = 0;//第一个元素的下标

int high = per.length - 1;//最后一个元素的下标,几个数字下标就是长度减1

int index = 0;

while(lower<=high){

index = (lower+high)/2;//记录中间元素,用两边之和除2.

if(per[index].empid==(empid1)){//如果得到的数与要查找的数相等则break退出;

break;

}else if(per[index].empid < empid1){//如果得到的数要小于查找的数、就用下标加1;否则减一

lower=index+1;

}else{

high = index-1;

}

}

if(lower<=high){

System.out.println("员工号" + empid1 + "的基本信息为" + per[empid1].empid + " " + per[empid1].empname + " " 

+ per[empid1].cname + " " + per[empid1].salary);

}else{

System.out.println("没有该员工号");

}

case 3:

show();

default:

System.out.println("You input the number is not in 1~4 ! "

+ "Please try again!");

break;

}

}else{

System.out.println("Exit!");

System.exit(0);

}

//设置显示延迟,输出结果与重新显示面板之间的延迟;

try 

Thread.currentThread();

Thread.sleep(3000);//毫秒; 

catch(Exception e){

//重新显示计算器提示面板

employee.printMenu();

System.out.println("Please choose number 1~4 to do next:");

}

input.close();

}

    

    

    public static void show(){

        

        for(int i=0;i<per.length-1;i++){

       

        System.out.println(per[i].empid + " " + per[i].empname + " " 

+ per[i].cname + " " + per[i].salary);

        }

    }


}

package employee2;


class employee {

public int empid;

    public String empname;

    public String cname;

    public int salary;

    

    public employee(int empid,String empname,String cname,int salary){

   

    this.empid=empid;

    this.empname=empname;

    this.cname=cname;

    this.salary=salary;

    }

    

static void printMenu(){

System.out.println("*********Calculator*********");

System.out.println("****************************");

System.out.println("\t1. Add Employee\t" 

+ "\n\t2.Search Employee\t" 

+ "\n\t3.Sort Employee\t" 

+ "\n\t4.Exit\t");

System.out.println("****************************");

}

}


项某某
浏览 1552回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java