使用循环接受其他用户输入,直到用户输入结束输入的值

时间:2018-06-14 02:05:23

标签: java

我是Java的新手。我需要一些帮助,使用循环接受其他用户输入,直到用户输入结束输入的值。我的问题从语句" System.out.println开始("你完成了吗?输入大写字母的Y / N")下面是我的代码。

公共类EmployeeData {

//declare variables
private String firstName;
private String lastName;
private int id;
private double hourlyWage;

//constructors
public EmployeeData(String firstName, String lastName, int id, double hourlyWage){

    this.firstName = firstName;
    this.lastName = lastName;
    this.id = id;
    this.hourlyWage = hourlyWage;
}

// getters and setters

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public double getHourlyWage() {
    return hourlyWage;
}

public void setHourlyWage(double hourlyWage) {
    this.hourlyWage = hourlyWage;
}

//method to return string
public String toString(){
    return "Employee Name: " + this.firstName +" "+ this.lastName + "\n"+ "Employee ID: " +this.id + "\n"+ "Hourly Wage "+ this.hourlyWage;
}

}

import java.util.Scanner;

公共类员工{

public static void main(String[] args){


    String firstName;
    String lastName;
    int id;
    double hourlyWage;

    Scanner input = new Scanner(System.in);
    System.out.println("Enter your First Name");
    firstName = input.nextLine();
    System.out.println("Enter your Last Name");
    lastName = input.nextLine();
    System.out.println("Enter your ID Number");
    id = input.nextInt();
    System.out.println("Enter your Hourly Wage");
    hourlyWage = input.nextDouble();


    EmployeeData em1 =  new EmployeeData("firstName" , "lastName" , id, hourlyWage);

    em1.setFirstName(firstName);
    em1.setLastName(lastName);
    em1.setId(id);
    em1.setHourlyWage(hourlyWage);



    String x = em1.toString();
    System.out.println(x);


    System.out.println("Are you are finished? Enter Y/N in uppercase");
    char c = input.next().charAt(0);
    if(c == 'N'){
        System.out.println("Enter your First Name");
        firstName = input.nextLine();
        System.out.println("Enter your Last Name");
        lastName = input.nextLine();
        System.out.println("Enter your ID Number");
        id = input.nextInt();
        System.out.println("Enter your Hourly Wage");
        hourlyWage = input.nextDouble();


        EmployeeData em2 =  new EmployeeData("firstName" , "lastName" , id, hourlyWage);

        em2.setFirstName(firstName);
        em2.setLastName(lastName);
        em2.setId(id);
        em2.setHourlyWage(hourlyWage);



        String z = em2.toString();
        System.out.println(z);
    }
    else{
        System.out.println("You have finished.");
    }



}

}

3 个答案:

答案 0 :(得分:1)

使用while loop并在完成所需操作后提示输入。循环将重复直到满足某个条件。即,直到用户在提示中输入条件(您可以将其放在执行块的末尾)

while(input != "value for exit") 
{

// do your code

//prompt for input
}

答案 1 :(得分:0)

这是编程的基础知识,但正如你所说的,你是Java的新手 - 建议你一般阅读recursive functions,然后按照递归方法实现它。

答案 2 :(得分:0)

你需要一个循环。不要在这里调用Scanner.next()(它不会消耗尾随空格)。我会给String.toUpperCase()添加一个电话。最后,要回答您的问题,请使用while循环。并且您可以在测试角色不是N之前执行分配。像,

char c;
while ((c = input.nextLine().toUpperCase().charAt(0)) != 'N') {
    System.out.println("Enter your First Name");
    firstName = input.nextLine();
    System.out.println("Enter your Last Name");
    lastName = input.nextLine();
    System.out.println("Enter your ID Number");
    id = input.nextInt();
    System.out.println("Enter your Hourly Wage");
    hourlyWage = input.nextDouble();

    EmployeeData em2 = new EmployeeData("firstName", "lastName", id, hourlyWage);

    em2.setFirstName(firstName);
    em2.setLastName(lastName);
    em2.setId(id);
    em2.setHourlyWage(hourlyWage);

    // String z = em2.toString();
    System.out.println(em2);
}
System.out.println("You have finished.");