好吧,我的程序又陷入了僵局。我需要重复一遍,但我不明白我在课文中读到的关于如何做的内容。它只涵盖重复值,如成绩簿程序。无论如何,我需要程序不断重复,直到用户输入“停止”作为员工姓名。到目前为止,这是我的代码:
package payroll_program_2; 导入 java.util.Scanner;
public class payroll_program_2 {
public static void main(String[] args) {
Scanner input = new Scanner( System.in );
float hours;
float rate;
String name;
float total_pay;
System.out.println("Please enter employee name");
name = input.nextLine();
if("stop".equals(name))
{ return;
}
System.out.println("Please enter hourly rate");
rate = input.nextFloat();
if (rate <0)
{
System.out.println("Pay rate cannot be negative");
System.out.println("Please enter hourly rate");
rate = input.nextFloat();
}
System.out.println("Please enter hours worked");
hours = input.nextFloat();
if (hours <0)
{
System.out.println("Hours cannot be negative");
System.out.println("Please enter hours worked");
hours = input.nextFloat();
}
System.out.println("Employee's total pay for this week");
total_pay = hours*rate;
System.out.printf("The total pay for %s is $%.2f\n", name, total_pay);
}
最佳答案
您需要添加一个while
循环或for
循环,如:
while (true) {
// .. read some input ...
if ( /* input is "stop" */ ) {
break; // this causes the loop to exit
}
}
关于java - 重复程序帮助?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4982803/