我想在我的组织java应用程序中接受经理的全名..我尝试使用sc.nextLine();但它显示InputMisMatc



下面是我的代码,重点是案例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("Optionsn"+"1.Hire Managern"+"2.Hire Workern"+"3.Display information of all employeesn"
+ "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,name,deptId,basic,performBonus101samarth shukla

接受某些变量中的输入并将其传递给对象构造函数。

检查此项以获取更多deatilshttps://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();
}
}

混合nextInt/nextDouble/。。。使用来自扫描仪的nextLine总是一个坏主意。nextInt/nextDouble/。。。在Stream上保留一个换行符,这意味着当调用nextLine时,它只会拾取换行符,而这可能不是您想要的

除了我所评论的内容之外,您还可以尝试读取分隔行上的每个输入,使用Integer.parseInt(read.nextLine())表示整数,Double.parseDouble()表示双精度。

用于常规字符串输入的read.nextLine()

实际参数列表和正式参数列表的长度构造函数java不同。最好先使用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);
}
}

最新更新