简体   繁体   English

如何将 if 语句放在 if-else if 语句中

[英]How can I put an if statement inside of a if-else if statement

I am trying to make a salary calculator that if the applicant is under 18, they cannot receive overtime, because in the state I am in the maximum allowed for minors is 18 hours.我正在尝试制作一个工资计算器,如果申请人未满 18 岁,他们就不能加班,因为在我所在的州,未成年人的最长允许时间为 18 小时。 I have it set where you cannot earn overtime until you reach 40 hours.我已经设置了在达到 40 小时之前您无法获得加班费的设置。 Is there anything I can do for this?我能为此做些什么吗?

import java.util.Scanner;
public class SalaryV2
{
public static void main(String[] args)
{

//Declare and initialize variables
double totalSalary;
boolean isOvertime;
Scanner in = new Scanner(System.in);

//Input
System.out.print("Please enter your name (first last): ");
String firstName = in.next();
String lastName = in.nextLine();

System.out.print("Please enter your age: ");
String customerAge = in.nextLine();
double age = Double.parseDouble(customerAge);

System.out.print("\nWhat is your hourly rate of pay: ");
String rateOfPay = in.nextLine();
double payRate = Double.parseDouble(rateOfPay);

System.out.print("\nHow many hours did you work: ");
String totalHoursWorked = in.nextLine();
double totalHours = Double.parseDouble(totalHoursWorked);
System.out.println();

    //Processing

 if(17 > age)
{
    isOvertime = false;
        totalSalary = totalHours * payRate;
}
else if(totalHours > 40)
{
    if(totalHours > 40)
    {
        isOvertime = true;
        totalSalary = 40 * payRate + (totalHours - 40) * payRate * 1.5;
    }
    else
    {
        isOvertime = false;
        totalSalary = totalHours * payRate;
    }
}
    

    
    

//Output
System.out.print("Employee Name: " + lastName + ", " + firstName\n);
System.out.print("Hours worked: " + totalHours);
System.out.println("\t\tOvertime: " + isOvertime +);
System.out.println("Salary: " + totalSalary);

} } } }

Keep it simple, no need for else-if-if,保持简单,不需要 else-if-if,

import java.util.Scanner;
public class SalaryV2{
public static void main(String[] args){

//Declare and initialize variables
double totalSalary= 0;
boolean isOvertime = false;
Scanner in = new Scanner(System.in);

//Input
System.out.print("Please enter your name (first last): ");
String firstName = in.next();
String lastName = in.nextLine();

System.out.print("Please enter your age: ");
String customerAge = in.nextLine();
double age = Double.parseDouble(customerAge);

System.out.print("\nWhat is your hourly rate of pay: ");
String rateOfPay = in.nextLine();
double payRate = Double.parseDouble(rateOfPay);

System.out.print("\nHow many hours did you work: ");
String totalHoursWorked = in.nextLine();
double totalHours = Double.parseDouble(totalHoursWorked);
System.out.println();

//Processing

if(age >= 18 && totalHours > 40) {
  isOvertime = true;
  totalSalary = 40 * payRate + (totalHours - 40) * payRate * 1.5;
}else{
  isOvertime = false;
  totalSalary = totalHours * payRate;
}

//Output
System.out.print("Employee Name: " + lastName + ", " + firstName +'\n' );
System.out.print("Hours worked: " + totalHours + '\n');
System.out.println("Overtime: " + isOvertime );
System.out.println("Salary: " + totalSalary);

}}

With the following input STDIN firstname lastname 19 10 42使用以下输入 STDIN firstname lastname 19 10 42

Output :输出 :

$javac SalaryV2.java
$java -Xmx128M -Xms16M SalaryV2
Please enter your name (first last): Please enter your age: 
What is your hourly rate of pay: 
How many hours did you work: 
Employee Name: lastname, firstname
Hours worked: 42.0
Overtime: true
Salary: 430.0

If under 18 STDIN如果未满 18 岁标准输入

firstname lastname 17 10 42名字 姓氏 17 10 42

Output :输出 :

$javac SalaryV2.java
$java -Xmx128M -Xms16M SalaryV2
Please enter your name (first last): Please enter your age: 
What is your hourly rate of pay: 
How many hours did you work: 
Employee Name:  lastname, firstname
Hours worked: 42.0
Overtime: false
Salary: 420.0

Checked by executing the above code.通过执行上面的代码进行检查。

I didn't understand very well your question, but here is something you can improve.我不太明白你的问题,但这里有一些你可以改进的地方。

In the first if, you are not considering even 17 years old, you should put if(17 >= age) or if(18 > age) .在第一个 if 中,您甚至不考虑 17 岁,您应该输入if(17 >= age)if(18 > age)

Then, in the else if you put the same condition as the if below, so what you should do is:然后,在 else 中,如果您将条件与下面的 if 相同,那么您应该做的是:

else if(totalHours > 40)
{
    isOvertime = true;
    totalSalary = 40 * payRate + (totalHours - 40) * payRate * 1.5;
}
else
{
    isOvertime = false;
    totalSalary = totalHours * payRate;
}

And to make your code more efficient, you can put two conditions in one if.为了使您的代码更高效,您可以将两个条件放在一个 if 中。

if(18 > age && totalHours > 40)
{
    isOvertime = false;
    totalSalary = totalHours * payRate;
}
else
{
    isOvertime = true;
    totalSalary = 40 * payRate + (totalHours - 40) * payRate * 1.5;
}

I didn't check if the code actually works, but it should be fine我没有检查代码是否真的有效,但应该没问题

If you are a minor you cannot get overtime (strange as that may seem).如果你是未成年人,你不能加班(这看起来很奇怪)。
If you have less than 40 hours you cannot get overtime (everybody knows the weird consequences of that...).如果你的时间少于 40 小时,你就不能加班(每个人都知道这样做的奇怪后果......)。

So you can only get overtime if you are over 18 and have more than 40 hours.因此,只有年满 18 周岁且工作时间超过 40 小时才能获得加班费。

if((18 <= age) && (totalHours > 40))
{
    isOvertime = false;
    totalSalary = totalHours * payRate;
} else
{   
    isOvertime = true;
    totalSalary = 40 * payRate + (totalHours - 40) * payRate * 1.5;
    
}

Ie I do not see the need for nested if s.即我不认为需要嵌套if s。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM