我想在我的组织java应用程序中接受经理的全名…我尝试使用sc.nextline();但它显示了输入不匹配错误

jq6vz3qz  于 2021-08-20  发布在  Java
关注(0)|答案(4)|浏览(324)

下面是我的代码重点领域是案例1和案例2。考虑到我是初学者,请提供一些简单的方法来接受全名:

package Tester;

import java.util.Scanner;

import com.app.org.Emp;
import com.app.org.Mgr;
import com.app.org.Worker;

public class TestOrganization {

    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.println("How many people you want to recruit");
        Emp[] org = new Emp[sc.nextInt()]; //holder array ...its not a emp object...its array type of object
        boolean exit = false;
        int index = 0;
        while(!exit) {
        System.out.println("Options\n"+"1.Hire Manager\n"+"2.Hire Worker\n"+"3.Display information of all employees\n"
                + "4.Update Performance Bonus or Hourly Rate");
        switch (sc.nextInt()) {
        case 1:{
            if(index<org.length && index>=0) {
            System.out.println("Manager Details:  id,  name,  deptId, basic,  performBonus");
            org[index++] = new Mgr(sc.nextInt(),sc.nextLine(),sc.next(),sc.nextDouble(),sc.nextInt());
            }else {
                System.out.println("Invalid!!! index");
            }
            break;
        }
        case 2:{
            if(index<org.length && index>=0) {
                System.out.println("Worker Details: id, name, deptId, basic, hw, hr");
                org[index++] = new Worker(sc.nextInt(),sc.nextLine(),sc.next(),sc.nextDouble(),sc.nextDouble(),sc.nextDouble());
            }else System.out.println("invalid!!! index");
            break;
        }
        case 3:{
            System.out.println("Employees Details");
            for (Emp e : org) {
                if(e!=null)
                System.out.println(e);
            }

        }

输入:您希望招聘多少人5个选项1.招聘经理2.招聘工人3.显示所有员工的信息4.更新绩效奖金或小时工资1.经理详细信息:id、姓名、部门id、基本、PerformBunus 101 samarth shukla

zpgglvta

zpgglvta1#

混合 nextInt / nextDouble /... 和 nextLine 从扫描器上看总是个坏主意。这个 nextInt / nextDouble /... 在流中保留换行符,这意味着 nextLine 调用时,它只拾取换行符,这可能不是您想要的

dsekswqp

dsekswqp2#

除了我的评论之外,您还可以尝试使用 Integer.parseInt(read.nextLine()) 对于整数和 Double.parseDouble() 双倍。 read.nextLine() 用于常规字符串输入

dtcbnfnu

dtcbnfnu3#

实际参数列表和形式参数列表的长度不同。最好先使用nextline(),然后再使用其余部分。
例如:-

class Codechef
{
 public static void main (String[] args) throws java.lang.Exception
 {
    // your code goes here
      Scanner sc = new Scanner(System.in);
    Mgr obj = new Mgr(sc.nextLine(), sc.nextDouble(), sc.nextInt());

  }
}

class Mgr{
 public Mgr(String name, Double dNo, int id ){
    System.out.println(id +" and "+ name +"dNo "+ dNo);
 }
}
s5a0g9ez

s5a0g9ez4#

接受某些变量的输入并将其传递给对象构造函数。
查看此项了解更多死亡信息https://stackoverflow.com/a/13102066/7575841

package tester;

import java.util.Scanner;
import com.app.org.Employee;
import com.app.org.Manager;
import com.app.org.Worker;

public class TestOrganisation {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);

        System.out.print("What is the strength of organisation ? ");

        Employee[] employees = new Employee[cin.nextInt()];

        int count = 0;
        boolean again = true;

        do {
            System.out.println("*****Welcome To Virtual Organization*****");
            System.out.println("1. Hire Manager");
            System.out.println("2. Hire Worker");
            System.out.println("3. Display All");
            System.out.println("4. Update Performance Bonus/Hourly Rate");
            System.out.println("0. Wrap Up");
            System.out.print("Enter option number : ");

            switch(cin.nextInt() ) {
              case 1:
                if (count < employees.length) {
                  System.out.println("Hire a new manager.");
                  System.out.print("Enter Employee ID : ");
                  int id = cin.nextInt();
                  System.out.print("Enter Departemnt ID : ");
                  int deptID = cin.nextInt();
                  cin.nextLine();
                  System.out.print("Enter Name : ");
                  String name = cin.nextLine();
                  System.out.print("Enter Basic Salary : ");
                  double basic = cin.nextDouble();
                  System.out.print("Enter Performance Bonus : ");
                  double perfBonus = cin.nextDouble();
                  employees[count++] = new Manager(id, deptID, name, basic, perfBonus);
                  System.out.println("Manager Hired Successfully.\n");
                }
                else {
                  System.out.println("\nNo more vacancy available.\n");
                }
                break;
              case 2:
                if (count < employees.length) {
                  System.out.println("Hire a new worker.");
                  System.out.print("Enter Employee ID : ");
                  int id = cin.nextInt();
                  System.out.print("Enter Departemnt ID : ");
                  int deptID = cin.nextInt();
                  cin.nextLine();
                  System.out.print("Enter Name : ");
                  String name = cin.nextLine();
                  System.out.print("Enter Basic Salary : ");
                  double basic = cin.nextDouble();
                  System.out.print("Enter Hourly Rate : ");
                  double hourlyRate = cin.nextDouble();
                  System.out.print("Enter Hours Worked : ");
                  int hoursWorked = cin.nextInt();
                  employees[count++] = new Worker(id, deptID, name, basic, hourlyRate, hoursWorked);
                  System.out.println("Wroker Hired Successfully.\n");
                }
                else {
                  System.out.println("\nNo more vacancy available.\n");
                }
                break;
              case 3:
                if (count == 0) {
                  System.out.println("\nOnly Ghosts Work Here.\n");
                }
                else {
                  System.out.println("\nList of employees work in organization.");
                  for (Employee e: employees) {
                    if (e instanceof Worker) {
                      System.out.print(e);
                      System.out.println(", whose net salary is " + ((Worker)e).computeNetSalary());
                    }
                    else if (e instanceof Manager) {
                      System.out.print(e);
                      System.out.println(", whose net salary is " + ((Manager)e).computeNetSalary());
                    }
                    else {
                      System.out.println("Position is either empty or a ghost works here.");
                    }
                  }
                  System.out.println();
                }
                break;
              case 4:
                if (count == 0) {
                  System.out.println("\nOnly Ghosts Work Here.\n");
                }
                else {
                  System.out.print("Enter Employee ID : ");
                  int empID = cin.nextInt();
                  for (Employee e: employees) {
                    if (e != null) {
                      if(e.equals(empID)) {
                        if (e instanceof Worker) {
                        System.out.print("Enter new hourly rate : ");
                        double hourlyRate = cin.nextDouble();
                        ((Worker) e).setHourlyRate(hourlyRate);
                        System.out.println("Hourly Rate Updated.\n");
                      }
                      else if (e instanceof Manager) {
                        System.out.print("Enter new performance bonus : ");
                        double prefBonus = cin.nextDouble();
                        ((Manager) e).setPerfBonus(prefBonus);
                        System.out.println("Performance Bonus Updated.\n");
                      }
                      else {
                        System.out.printf("\nEmployee with %id is a ghost.\n", empID);
                      }
                    }
                      break;
                    }
                  }
                }
                break;
                case 0:
                  again = false;
                  System.out.println("Good Bye. Have a nice day.");
                  break;
                default:
                    System.out.println("Invalid Option. Please Try Again.");
            }
        } while(again);
        cin.close();
    }
}

相关问题