java - 如何存储2个变量并比较它们

标签 java if-statement

我正在尝试接受用户输入的两个人的小时工资以及他们每年的加类时间。

使用我研究过的算法,该程序将告诉人们他们每年赚多少钱以及他们缴纳的税额,这是基于他们赚的金额。

这一切都很好。然而,我现在想做的是在程序末尾添加一行,说明谁缴纳了更多税款。这可以通过 whoPaysMoreTaxes 方法来完成,但我不知道该方法中要包含什么。我知道我需要一个简单的 if/else if/else 语句来完成工作,但我不知道如何存储人员 1 的税款和人员 2 的税款并进行比较。我相信输出应该如下。数字 22、100、58 和 260 是用户输入的:

Person 1's hourly wage: 22
Person 1's overtime hours for the year: 100
You will make $45540 this year
And you will pay $9108 in taxes
Person 2's hourly wage: 58
Person 2's overtime hours for the year: 260
You will make $133980 this year
And you will pay $40194 in taxes.
Person 2 is paying more taxes.

我遇到的问题是找到一种方法来生成最后一行,表明谁缴纳了更多税款。

public class conditionalsAndReturn
{
   public static void main(String[] args)
   {
       Scanner console = new Scanner(System.in);
       taxes(console, 1);
       taxes(console, 2);
   }
   public static void taxes(Scanner console, int personNum)
   {
      System.out.print("Person " + personNum + "'s hourly wage: ");
      int wage = console.nextInt();
      System.out.print("Person " + personNum + "'s overtime hours for the year: ");
      double totalOvertimeHours = console.nextInt();        
      int salary = annualSalary(wage, totalOvertimeHours);
      System.out.println("You will make $" + salary + " this year");
      System.out.println("And you will pay $" + taxation(salary) + " in taxes");
      System.out.println();   
   }

   public static int annualSalary(int wage, double totalOvertimeHours)
   {
      double workHoursPerWeek = 40 + totalOvertimeHours / 48;
      return (int)(weeklyPay(wage, workHoursPerWeek) * 48); 
    } 

   public static double weeklyPay(int wage, double workHoursPerWeek)
   {
       if (workHoursPerWeek > 40)
       {  
           return (wage * 40) + ((wage + wage / 2.0) * (workHoursPerWeek - 40));    
       }
       else
       {
          return wage * workHoursPerWeek;  
       }
    } 

   public static int taxation(int salary)
   {
       if (salary < 20000)
       {
           return 0;
       }
       else if (salary > 100000)
       {
           return salary * 3 / 10; 
       }
       else
       {
           return salary * 2 / 10;
       }
   }

  public static String whoPaysMoreTaxes(
}

最佳答案

OOP 符合编码是,拥有一个类人员(或更好的员工),其字段为:personNum、三个工资/薪金变量中的一个或多个、税收。如果需要,添加名称等。

现在您可以使用这些类的实例来存储累积的数据,并使用compareTo 来比较对象。

关于java - 如何存储2个变量并比较它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40353667/

相关文章:

java - 如何将用户凭据传递给 WebLogic 11g 身份验证提供程序?

java - 如何启用 Yandex 翻译 API?

Java:实现可比较但条件 if 太多。我怎样才能避免它们?

javascript - 多维数组中的循环未显示预期内容

java - 无法单击从工厂方法传递的元素

java - 找不到要处理为 'ajaxSingle' 的组件

java - Textview 搞乱了阿拉伯字母

javascript - 为什么它在我的 if/else 语句中循环遍历 'else'?

javascript - "99 bottle of beer on the wall"代码不工作纯 js。 if 语句和 while 循环

Java闰年检查程序。对边缘值无响应