gpt4 book ai didi

java - 如何在 Java 中为字符串变量编写 "if"语句?

转载 作者:行者123 更新时间:2023-11-30 07:34:50 24 4
gpt4 key购买 nike

这是我目前所拥有的,我将问题写成评论,放在我需要帮助的行上。任何进一步的建议也将受到欢迎。当用户输入“停止”作为员工姓名时,我只需要它来结束程序。提前致谢。

package payroll_program_2;
import java.util.Scanner;

public class Main {
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.next();
if (stop) //THIS IS WHAT I NEED HELP WITH. I DO NOT KNOW HOW TO WRITE
end program //IT CORRECTLY, SO I JUST TYPED WHAT I NEED IT TO DO.
{

}
System.out.println("Please enter hourly rate");
rate = input.nextFloat();
if (rate <0)
{
System.out.printf("Pay rate cannot be negative");

}
System.out.println("Please enter hours worked");
hours = input.nextFloat();
if (hours <0)
{
System.out.printf("Hours cannot be negative");

}
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);

}

最佳答案

if (name.equals("stop")) {  
return;
}

关于java - 如何在 Java 中为字符串变量编写 "if"语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4976782/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com