我需要为我的CIS课程编写一个程序,我觉得我已经设置了多数。为95%,简单地总结一下。我必须写一个程序,提示用户输入他们的姓名,工资率,工作小时数。然后它获取数据,计算总工资,然后减去税,然后将其全部打印到屏幕上。该程序应允许多个用户为员工数量添加数据,这迫使我相信我需要设置他们必须输入的值才能结束程序。问题是我遗憾地对如何编码它感到困惑,因此当输入值时,程序结束。我几乎肯定需要一个while循环,但任何形式的反馈都会非常感激。
package program1;
import java.util.*;
public class Program1 {
static Scanner console = new Scanner (System.in);
public static void main(String[] args) {
String firstName, lastName ;
double payRate;
int hoursWorked;
double netPay;
double grossPay;
String formatNet;
System.out.println("Please enter the employee's name. (Enter a -1 when finished): ") ;
firstName = console.nextLine();
lastName = console.nextLine();
System.out.println("Please enter the employee's pay rate. ");
payRate = console.nextDouble();
System.out.println("Please enter the employee's hours worked. ");
hoursWorked = console.nextInt();
if(hoursWorked > 40)
{
grossPay = payRate * hoursWorked * 1.5;
}
else
{
grossPay = payRate * hoursWorked;
}
netPay = grossPay - (grossPay * .15);
formatNet = String.format("%.2f", netPay);
System.out.println(firstName +" "+ lastName + "'s net pay is " + formatNet);
}
}
答案 0 :(得分:0)
是的,你需要一个循环。请尝试以下代码。
还要注意在{。}之后出现console.nextLine();
console.nextDouble();
和console.nextInt();
。
public static void main(String[] args) {
String firstName, lastName;
double payRate;
int hoursWorked;
double netPay;
double grossPay;
String formatNet;
while (true){
System.out.println("Please enter the employee's name. (Enter a -1 when finished): ");
firstName = console.nextLine();
if ("-1".equals(firstName.trim())) break;
lastName = console.nextLine();
System.out.println("Please enter the employee's pay rate. ");
payRate = console.nextDouble();
console.nextLine();
System.out.println("Please enter the employee's hours worked. ");
hoursWorked = console.nextInt();
console.nextLine();
if (hoursWorked > 40) {
grossPay = payRate * hoursWorked * 1.5;
} else {
grossPay = payRate * hoursWorked;
}
netPay = grossPay - (grossPay * .15);
formatNet = String.format("%.2f", netPay);
System.out.println(firstName + " " + lastName + "'s net pay is " + formatNet);
}
}
答案 1 :(得分:0)
使用do while
循环并使用某些条件可以终止循环,就像您可以提示用户“是否要继续(y / n)”并根据值重新进行迭代。
修改强>
do
{
//your code goes here
System.out.println("Do you want to continue(y/n)?");
isContinue = console.next();
}while(isContinue = "Y" || isContinue = "y")
在break
条件中检查firstname is -1
后,您可以使用if
语句
答案 2 :(得分:0)
这是一个解决方案。我投入了一些面向对象,方法提取和资源管理,以便在使用后始终关闭扫描仪。
import java.util.Scanner;
public class Program1 {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
try {
do {
enterEmployee(console);
System.out.println("Another employee? (q to quit)");
} while (!"q".equals(console.nextLine()));
} finally {
console.close();
}
}
private static void enterEmployee(Scanner console) {
Employee employee = new Employee();
System.out.println("Please enter the employee's first name: ");
employee.setFirstName(console.nextLine());
System.out.println("Please enter the employee's last name: ");
employee.setLastName(console.nextLine());
System.out.println("Please enter the employee's pay rate: ");
employee.setPayRate(console.nextDouble());
console.nextLine();
System.out.println("Please enter the employee's hours worked: ");
employee.setHoursWorked(console.nextInt());
console.nextLine();
System.out.println(employee + "'s net pay is " + String.format("%.2f", employee.getNetPay()));
}
public static class Employee {
private String firstName;
private String lastName;
private double payRate;
private int hoursWorked;
private double netPay;
private double grossPay;
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 double getPayRate() {
return payRate;
}
public void setPayRate(double payRate) {
this.payRate = payRate;
}
public int getHoursWorked() {
return hoursWorked;
}
public void setHoursWorked(int hoursWorked) {
this.hoursWorked = hoursWorked;
if (hoursWorked > 40) {
grossPay = payRate * hoursWorked * 1.5;
} else {
grossPay = payRate * hoursWorked;
}
netPay = grossPay - (grossPay * .15);
}
public double getNetPay() {
return netPay;
}
public double getGrossPay() {
return grossPay;
}
@Override
public String toString() {
return firstName + " " + lastName;
}
}
}