如何在Java中为字符串变量编写“if”语句?

时间:2011-02-12 06:53:45

标签: java string

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

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

}

1 个答案:

答案 0 :(得分:4)

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