java 如何在运行时获取所有变量的输入?[duplicate]

0qx6xfy6  于 2022-11-20  发布在  Java
关注(0)|答案(2)|浏览(141)

This question already has answers here:

Scanner is skipping nextLine() after using next() or nextFoo()? (24 answers)
Closed last year.
I have created two files one with having private variables and getters and setters, and other with taking input from the user and displaying the output in the console.
When I execute the program, it runs without error but when the input is given, it takes 3 inputs out of 4.
I am unable to get input for the fourth variable.
File with getters and setters👇

package tryProject;

public class Employee {
    

     private String name;
     private int yearJoin;
     private  int salary;
     private  String address;
     
     
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getYearJoin() {
        return yearJoin;
    }
    public void setYearJoin(int yearJoin) {
        this.yearJoin = yearJoin;
    }
    public int getSalary() {
        return salary;
    }
    public void setSalary(int salary) {
        this.salary = salary;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }   
    
}

File to take input and give output

package tryProject;

import java.util.Scanner;

public class EmployeeInfo {

    public static void main(String[] args) {
        Employee e = new Employee();
    
        Scanner s = new Scanner(System.in);  
            
        System.out.println("Enter details: ");
        System.out.println("Name: ");
        String input_name = s.nextLine();
        e.setName(input_name);
        System.out.println(e.getName());
        
        System.out.println("Salary: ");
        int input_salary = s.nextInt();
        e.setSalary(input_salary);
        System.out.println(e.getSalary());
        
        System.out.println("Year of Join: ");
        int input_YearJoin = s.nextInt();
        e.setYearJoin(input_YearJoin);
        System.out.println(e.getYearJoin());
        
        System.out.println("Address: ");
        String input_Address = s.nextLine();
        e.setAddress(input_Address);
        System.out.println(e.getAddress());
        
    }

}
sqyvllje

sqyvllje1#

我测试过你的程序,它确实打印了姓名、薪水和年份,而地址却打印了一个空行。原因是当你给予“加入年份”时,你输入一些数字,然后按“回车”键。当你按“回车”键时,你实际上给予一个空行(“\n”)这仍然是一个输入,它被地址域接受,这就是为什么程序不等待你的地址行,如果你用调试器,你会注意到的。如果你把String input_Address = s.nextLine();改成String input_Address = s.next();,然后运行你的程序,你就会明白我的意思了。
我建议你在阅读加入年份后加上scanner.nextLine();,这样程序会读取下一个按“Enter”键产生的空行,然后读取你的实际地址数据。

System.out.println("Year of Join: ");
int input_YearJoin = scanner.nextInt();
e.setYearJoin(input_YearJoin);
System.out.println(e.getYearJoin());
scanner.nextLine();

您可以阅读这篇文章以获得更多见解:Scanner is skipping nextLine() after using next() or nextFoo()?

plupiseo

plupiseo2#

尝试使用.next()而不是.nextLine()。
我也会把它改成这个名字。
我更改了您的代码,使其正常工作:

public static void main(String[] args) {
        Employee e = new Employee();
        Scanner s = new Scanner(System.in); 
            
        System.out.println("Enter details: ");
        System.out.println("Name: ");
        String input_name = s.next(); //changed .nextLine() to .next()
        e.setName(input_name);
        System.out.println(e.getName());
        
        System.out.println("Salary: ");
        int input_salary = s.nextInt();
        e.setSalary(input_salary);
        System.out.println(e.getSalary());
        
        System.out.println("Year of Join: ");
        int input_YearJoin = s.nextInt();
        e.setYearJoin(input_YearJoin);
        System.out.println(e.getYearJoin());
        
        System.out.println("Address: ");
        String input_Address = s.next(); //changed .nextLine() to .next()
        e.setAddress(input_Address);
    }

相关问题