在while循环结束时不要求输入

时间:2018-04-24 03:00:31

标签: java while-loop

我对Java编码完全陌生,我非常感谢对作业的帮助。我正在创建一个程序,用于收集员工信息,如姓名,工资,工作时间和家属。

这是我到目前为止所做的:

package payroll;
import java.util.*;
import java.io.*;
/**
 *
 * @author whitn
 */
public class Payroll {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    Scanner kb = new Scanner(System.in);
    String[]namearray;                       //Setting up the arrays
    namearray = new String[1000];            //Assume there are not 1000+ people
    double[]hrs_worked;
    hrs_worked = new double[1000];
    double[]payarray;
    payarray = new double[1000];
    int[]dependarray;
    dependarray = new int[1000];

    String name = "";                            //Initialize input
    float hrs;
    double pay;
    int dependents;
    int Q = -1;

    System.out.println("Please input the employee name:"); //Collect employee info
    name = kb.nextLine();

    while( name != null)
    {
        System.out.println("Please input the hours the employee worked:");
        hrs = kb.nextFloat();
        System.out.println("Please input the employee's hourly pay:");
        pay = kb.nextDouble();
        System.out.println("Please input the number of dependents the employee"
                + " claims:");
        dependents = kb.nextInt();
        Q++;

        namearray[Q] = name;
        payarray[Q] = pay;   
        hrs_worked[Q] = hrs;
        dependarray[Q] = dependents;


        System.out.println("\n Please input the employee name or press enter to"
                + " end the process:");
        name = kb.nextLine();
    }

我遇到的问题是在while循环结束时它会询问下一个人的名字,而不是等待输入,while循环重新开始并立即询问hrs,pay,家属。没有空间输入字符串名称。

我真的很感激能够帮助我继续前进的任何见解。就像我之前说的那样,我是编码的新手,如果我把信息留出来,请告诉我!

谢谢

1 个答案:

答案 0 :(得分:1)

我不确定您要实现的目标,但您可以通过一些修改尝试下面的代码。

public static void main(String[] args) 
{
    Scanner kb = new Scanner(System.in);
    String[]namearray;                       //Setting up the arrays
    namearray = new String[1000];            //Assume there are not 1000+ people
    double[]hrs_worked;
    hrs_worked = new double[1000];
    double[]payarray;
    payarray = new double[1000];
    int[]dependarray;
    dependarray = new int[1000];

    String name = "";                            //Initialize input
    float hrs;
    double pay;
    int dependents;
    int Q = -1;

    while( true)
    {
        if(Q>-1)
        {
            System.out.println("\nPlease input the employee name or press enter to end the process:");
            name = kb.nextLine();

        }
        else
            System.out.println("Please input the employee name:"); //Collect employee info

        name = kb.nextLine();
        if(name==null || name.trim().equals("")) break;

        System.out.println("Please input the hours the employee worked:");
        hrs = kb.nextFloat();
        System.out.println("Please input the employee's hourly pay:");
        pay = kb.nextDouble();
        System.out.println("Please input the number of dependents the employee"
                + " claims:");
        dependents = kb.nextInt();
        Q++;

        namearray[Q] = name;
        payarray[Q] = pay;   
        hrs_worked[Q] = hrs;
        dependarray[Q] = dependents;
    }
}
相关问题