无法在循环中输入第一个字符串,它总是从第二个输入开始(Java)。

huangapple go评论69阅读模式
英文:

Not able to input the first string in a loop, it always starts from 2nd input in java

问题

以下是翻译的内容:

我已经创建了一个员工(Employee)类,在这个类中,我只传递了值并打印了数组。您可以看到:

package practicequestion;

import java.util.Scanner;
import practicequestion.employeeq.Employee;

public class EmployeeMain {
    public static void main(String[] args) {
        Employee[] obj = new Employee[3];
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您想要输入的数量");
        int n = sc.nextInt();
        sc.nextLine(); // 解决输入换行问题
        for (int i = 0; i < n; i++) {
            System.out.println("请输入名字");
            String firstname = sc.nextLine();
            System.out.println("请输入姓氏");
            String lastname = sc.nextLine();
            System.out.println("请输入邮箱");
            String email = sc.nextLine();
            System.out.println("请输入工资");
            int salary = sc.nextInt();
            System.out.println("请输入手机号码");
            long mobileno = sc.nextLong();
            System.out.println("请输入工作经验");
            int empexp = sc.nextInt();
            System.out.println("请输入员工编号");
            int empid = sc.nextInt();
            if (i < n) {
                obj[i] = new Employee(firstname, lastname, email, salary, empid, empexp, mobileno);
            }
        }
        for (int i = 0; i < 3; i++) {
            System.out.println("员工 " + i +  " 的值为:");
            obj[i].Display();
        }
        if (sc != null) {
            sc.close();   
        }
    }
}

但是我无法输入名字,因为每当我运行程序时,它都会从姓氏开始输入。它的类是这样的:

package practicequestion.employeeq;

public class Employee {
   String FirstName;
   String LastName;
   String Email;
   int Salary;
   long MobileNo;
   int EmployeeExp;
   int EmployeeId;

    public Employee(String FirstName,String LastName,String Email,int Salary,int EmployeeId,int EmployeeExp,long MobileNo) {
        this.FirstName = FirstName;
        this.LastName = LastName;
        this.Email = Email;
        this.Salary = Salary;
        this.EmployeeId = EmployeeId;
        this.EmployeeExp = EmployeeExp;
        this.MobileNo = MobileNo;
    }
    public void Display() {
        System.out.println(FirstName + "\n" + LastName + "\n" + Email + "\n" + Salary + "\n" + EmployeeId + "\n" + EmployeeExp + "\n" + MobileNo);
    }
}

有谁能解决这个问题吗?

英文:

I have created an Employee class in which I am only passing the value and printing the array. You can see:

package practicequestion;
import java.util.Scanner;
import practicequestion.employeeq.Employee;
public class EmployeeMain {
public static void main(String[] args) {
Employee[] obj = new Employee[3];
Scanner sc = new Scanner(System.in);
System.out.println(&quot;Enter the number of inputs you want &quot;);
int n = sc.nextInt();
for (int i = 0; i &lt; n; i++) {
System.out.println(&quot;Enter FirstName&quot;);
String firstname = sc.nextLine();
System.out.println(&quot;Enter LastName&quot;);
String lastname = sc.nextLine();
System.out.println(&quot;Enter Email&quot;);
String email = sc.nextLine();
System.out.println(&quot;Enter Salary&quot;);
int salary = sc.nextInt();
System.out.println(&quot;Enter Mobile Number&quot;);
long mobileno = sc.nextLong();
System.out.println(&quot;Enter Experience&quot;);
int empexp = sc.nextInt();
System.out.println(&quot;Enter Employee Id&quot;);
int empid = sc.nextInt();
if (i &lt; n) {
obj[i] = new Employee(firstname, lastname, email, salary, empid, empexp, mobileno);
}
}
for (int i = 0; i &lt; 3; i++) {
System.out.println(&quot;Employee &quot; + i +  &quot; values are: &quot;);
obj[i].Display();
}
if (sc != null) {
sc.close();   
}
}
}

But I am not able to input the firstname as whenever I run the program it starts input from last name. Its class is like this:

package practicequestion.employeeq;
public class Employee {
String FirstName;
String LastName;
String Email;
int Salary;
long MobileNo;
int EmployeeExp;
int EmployeeId;
public Employee(String FirstName,String LastName,String Email,int Salary,int EmployeeId,int EmployeeExp,long MobileNo) {
this.FirstName = FirstName;
this.LastName = LastName;
this.Email = Email;
this.Salary = Salary;
this.EmployeeId = EmployeeId;
this.EmployeeExp = EmployeeExp;
this.MobileNo = MobileNo;
}
public void Display() {
System.out.println(FirstName +&quot;\n&quot; + LastName + &quot;\n&quot; + Email + &quot;\n&quot; + Salary + &quot;\n&quot; + EmployeeId +&quot;\n&quot; + EmployeeId +&quot;\n&quot; + MobileNo);
}
}

Can anyone solve this issue?

答案1

得分: 1

这个问题已经在这里得到了解答: https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo

基本上问题出在你在进入循环之前调用了 Scanner.nextInt()。Scanner.nextInt() 只会读取用户输入的下一个整数,而不是整行内容,因此当你在它之后调用 Scanner.nextLine() 时,它会直接接受你输入整数时所在行的剩余部分(通常为空)。

要解决这个问题,你可以要么先调用一次 Scanner.nextLine(),要么使用 Scanner.nextLine() 来获取整数,然后将其转换为整型以便使用。

英文:

This question was already answered here: https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo

Basically the problem is with Scanner.nextInt() which you call before entering the loop.
Scanner.nextInt() only reads the next Integer the user inputs, not the full line, therefore when you call Scanner.nextLine() after it directly accepts the remaining (and empty) part of the line where you entered the integer.

To solve it, you either have to call Scanner.nextLine() once, or use Scanner.nextLine() to get the Integer and then cast it to use it as an Integer.

答案2

得分: 0

尝试这个。我会记录我所做更改的位置。

Scanner sc = new Scanner(System.in);
System.out.println("请输入您想要输入的数量:");
int n = sc.nextInt();
// 在获取输入数量后分配 Employee 数组。
Employee[] obj = new Employee[n];

for (int i = 0; i < n; i++) {

    // 在此处清除缓冲区中的换行符。这也解决了循环末尾的 `nextInt` 问题
	sc.nextLine();
	
	System.out.println("请输入名字:");
	String firstname = sc.nextLine();
	System.out.println("请输入姓氏:");
	String lastname = sc.nextLine();
	System.out.println("请输入电子邮件:");
	String email = sc.nextLine();
	System.out.println("请输入工资:");
	int salary = sc.nextInt();
	System.out.println("请输入手机号码:");
	long mobileno = sc.nextLong();
	System.out.println("请输入经验:");
	int empexp = sc.nextInt();
	System.out.println("请输入员工ID:");
	int empid = sc.nextInt();
    // 您不需要在这里验证 `i`。for 循环已经为您做了验证
	obj[i] = new Employee(firstname, lastname, email, salary,
			empid, empexp, mobileno);
	
}

// 使用输入数量 `n` 限制循环
for (int i = 0; i < n; i++) {
	System.out.println("第 " + i + " 位员工的值为:");
	obj[i].Display();
}

通常不建议关闭 Scanner。

英文:

Try this. I will document where I made the changes.

Scanner sc = new Scanner(System.in);
System.out.println(&quot;Enter the number of inputs you want &quot;);
int n = sc.nextInt();
// allocate the Employee array after you get the number of inputs.
Employee[] obj = new Employee[n];
for (int i = 0; i &lt; n; i++) {
// get the newline out of the buffer here. That also
// takes care of the `nextInt` problem at the end of this loop
sc.nextLine();
System.out.println(&quot;Enter FirstName&quot;);
String firstname = sc.nextLine();
System.out.println(&quot;Enter LastName&quot;);
String lastname = sc.nextLine();
System.out.println(&quot;Enter Email&quot;);
String email = sc.nextLine();
System.out.println(&quot;Enter Salary&quot;);
int salary = sc.nextInt();
System.out.println(&quot;Enter Mobile Number&quot;);
long mobileno = sc.nextLong();
System.out.println(&quot;Enter Experience&quot;);
int empexp = sc.nextInt();
System.out.println(&quot;Enter Employee Id&quot;);
int empid = sc.nextInt();
// you don&#39;t need to verify `i` here.  The for loop does that
// for you
obj[i] = new Employee(firstname, lastname, email, salary,
empid, empexp, mobileno);
}
// use the number of inputs `n` to limit the loop
for (int i = 0; i &lt; n; i++) {
System.out.println(&quot;Employee &quot; + i + &quot; values are: &quot;);
obj[i].Display();
}

And it is usually not a good practice to close the scanner.

huangapple
  • 本文由 发表于 2020年8月27日 21:19:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63616908.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定