下面是我的代码,基本上我希望程序做的是每次按下回车键时循环while语句,然后当" q"提交到控制台,我希望程序结束。我做错了什么?使用此代码,它只运行一次,如果我改变
if (moreEmps != "q") break;
到
if (moreEmps == "q") break;
然后无论输入什么都会继续。有人能引导我朝着正确的方向前进吗?谢谢!
import java.text.NumberFormat;
java.util.Scanner;
/**
* Input a number of hours and calculate the pay at $10.00 per hour
* Pay time and one half for any hours over 40.
*/
public class PayCalc
{
public static void main (String[] args)
{
// Declare constants to be used in payroll calculation
final double RATE = 10.00; // hourly rate
final int REGULAR_HOURS = 40; // hours paid at regular rate
double pay = 0.0;
Scanner input = new Scanner (System.in);
System.out.println("Employee Payroll Program");
System.out.println("Press enter to begin.");
String moreEmps = "";
while (moreEmps != input.nextLine())
{
System.out.print("Enter employee name: ");
String employee = input.next();
System.out.print("Enter hours worked: ");
int hours = input.nextInt();
if (hours > REGULAR_HOURS)
pay = RATE * REGULAR_HOURS + (hours - REGULAR_HOURS) * RATE * 1.5;
else
pay = hours * RATE;
NumberFormat formit = NumberFormat.getCurrencyInstance();
System.out.println();
System.out.print("Total Pay for ");
System.out.print(employee);
System.out.println(" : " + formit.format(pay));
System.out.println("Any more employees?");
System.out.println("Press enter to input another employee's information, or q to quit.");
moreEmps = input.next();
moreEmps.toLowerCase();
if (moreEmps != "q") break;
}
}
}
答案 0 :(得分:0)
您需要使用equals()
来比较字符串。