如何存储2个变量并进行比较

时间:2016-11-01 03:18:06

标签: 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(
}

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

如果您遵循真正的面向对象编程原则,那么您可以创建一个表示Person对象的单独类(或考虑嵌套类)。然后每个Person实例都可以具有以下属性:

  • hourly_wage
  • overtime_hours
  • taxes_owed

然后,您需要根据需要创建任意数量的People类,使用类实例来存储数据。然后,您可以将方法标题修改为:

public Person who_payes_more_taxes(Person p1, Person p2): { ... }

在方法内部,您需要决定如何比较税收,但很可能看起来像:

if (p1.taxes_owed > p2.taxes_owed) { return p1 }

答案 2 :(得分:0)

你肯定是在正确的轨道上。我会使用更多变量来简化税收比较:

import java.util.Scanner;

public class ConditionalsAndReturn
{
    public static void main(String[] args)
    {
        int personOneWage;
        int personOneOvertime;
        double personOnePayBeforeTax;
        double personOneTaxes;
        double personOneNetIncome;

        int personTwoWage;
        int personTwoOvertime;
        double personTwoPayBeforeTax;
        double personTwoTaxes;
        double personTwoNetIncome;

        Scanner scan = new Scanner(System.in);

        System.out.print("Person 1's hourly wage: ");
        personOneWage = scan.nextInt();
        System.out.print("Person 1's overtime hours for the year: ");
        personOneOvertime = scan.nextInt();
        personOnePayBeforeTax = (40 * personOneWage) + (personOneOvertime * personOneWage * 1.5);
        personOneTaxes = taxes(personOnePayBeforeTax);
        personOneNetIncome = personOnePayBeforeTax - personOneTaxes;
        System.out.println("You will make $" + personOneNetIncome + " this year");
        System.out.println("And you will pay $" + personOneTaxes + " in taxes");

        System.out.print("Person 2's hourly wage: ");
        personTwoWage = scan.nextInt();
        System.out.print("Person 2's overtime hours for the year: ");
        personTwoOvertime = scan.nextInt();
        personTwoPayBeforeTax = (40 * personTwoWage) + (personTwoOvertime * personTwoWage * 1.5);
        personTwoTaxes = taxes(personTwoPayBeforeTax);
        personTwoNetIncome = personTwoPayBeforeTax - personTwoTaxes;
        System.out.println("You will make $" + personTwoNetIncome + " this year");
        System.out.println("And you will pay $" + personTwoTaxes + " in taxes");

        if (personOneTaxes > personTwoTaxes)
        {
            System.out.println("Person 1 is paying more in taxes.");
        }
        else
        {
            System.out.println("Person 2 is paying more in taxes.");
        }

        scan.close();
    }

    private static double taxes(double payBeforeTax)
    {
        if (payBeforeTax < 20000)
        {
            return 0;
        }
        else if (payBeforeTax > 100000)
        {
            return payBeforeTax * 3 / 10;
        }
        else
        {
            return payBeforeTax * 2 / 10;
        }
    }
}
相关问题