简体   繁体   English

Java编写简单程序

[英]Java writing simple program

I am writing a simple program that the user has to input the name of the employee, wage per hour and how many hours he will work and it will give out the name of the employee as well as how much he earned gross pay and net pay this week but I can't find a way to input the name as a text not a number. 我正在编写一个简单的程序,用户必须输入员工的姓名,每小时工资以及他将工作多少小时,并且它将给出员工的姓名以及他的总工资和净工资这个星期,但我找不到一种将名称输入为文字而非数字的方法。

class payroll
{
    public static void main(String[] args) 
    {
    Scanner input = new Scanner(System.in);
    double count, hours, wage, grosspay, netpay;
    int employee;
    for (;;)
    {
    System.out.print(" Please input the name of the employee followed by the hourly wage followed by the hours worked ");
    employee=input.nextDouble();
    hours = input.nextDouble();
    wage= input.nextDouble();
    grosspay= hours*wage;
    netpay= hours*wage*0.7;
    System.out.print( employee + " gross pay will be " + grosspay + " pounds." + employee + " net pay will be " + netpay + " pounds " + "Please insert name, hours and wage of the next worker." );
    }
    }
}

使用next()方法读取String

employee = input.next();

this is what is needed(mentioned by Arrem as an answer before) 这就是需要的(之前Arrem提到的答案)

Scanner in = new Scanner(System.in);
String abc = in.nextLine(); 
System.out.println("You entered string "+ abc);

使用nextLine()做到这一点,你应该没事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM