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(
}
# 1 楼答案
然后,您可能希望创建所需数量的人员类,使用类实例来存储数据。然后,您可以将方法标题修改为:
在该方法中,您需要决定如何比较税收,但最有可能的情况是:
# 2 楼答案
符合OOP的编码应该是,有一个类person(或更好的雇员),带有以下字段:personNum、三个工资/薪金变量中的一个或多个、taxing。如果需要,添加名称等
现在,您可以使用这些类的实例来存储累积的数据,并将对象与compareTo进行比较
# 3 楼答案
你肯定走对了路。我将使用更多变量来简化税收比较: