有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我正在尝试接受用户输入的两个人的小时工资和他们每年加班的小时数

使用我研究过的一种算法,这个程序会告诉两个人他们每年挣多少钱,以及他们缴纳多少税,这是基于他们挣多少钱

这一切都很好。然而,我现在要做的是在节目的末尾加上一行,说明谁缴纳了更多的税。这将通过方法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) 个答案

  1. # 1 楼答案

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

    • 小时工资
    • 加班时间
    • 收入
    • 欠税

    然后,您可能希望创建所需数量的人员类,使用类实例来存储数据。然后,您可以将方法标题修改为:

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

    在该方法中,您需要决定如何比较税收,但最有可能的情况是:

    if (p1.taxes_owed > p2.taxes_owed) { return p1 }
    
  2. # 2 楼答案

    符合OOP的编码应该是,有一个类person(或更好的雇员),带有以下字段:personNum、三个工资/薪金变量中的一个或多个、taxing。如果需要,添加名称等

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

  3. # 3 楼答案

    你肯定走对了路。我将使用更多变量来简化税收比较:

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