我正在尝试接受用户输入两个人的小时工资和他们每年加班的小时数。
使用我研究过的一种算法,该程序将告诉这两个人他们每年赚的钱和他们缴纳的税额,这是基于他们赚的钱。
这一切都很好。然而,我现在尝试做的是在程序的末尾添加一行,说明谁交的税更多。这可以通过方法whoPaysMoreTaxes来完成,但是我不知道在该方法中包含什么内容。我知道我需要一个简单的if/ else if/ else语句来完成这项工作,但我不知道如何存储person 1的taxes和person 2的taxes并对它们进行比较。我相信输出结果应该如下所示。数字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(
}发布于 2016-11-01 11:25:34
符合OOP的编码应该是,拥有一个类person (或更好的employee),具有以下字段: personNum、三个工资/薪水变量中的一个或多个、税收。如果需要,请添加名称等。
现在,您可以使用这些类的实例来存储累积的数据,并将这些对象与compareTo进行比较。
发布于 2016-11-01 11:32:33
如果您遵循真正的面向对象编程原则,那么您可以创建一个单独的类来表示Person对象(或者考虑使用嵌套类)。那么每个Person实例可以具有以下属性:
然后,您可能希望创建所需的任意多个People类,使用类实例来存储数据。然后,您可以将方法标头修改为:
public Person who_payes_more_taxes(Person p1, Person p2): { ... }在该方法中,您需要决定如何比较税收,但最有可能的情况是:
if (p1.taxes_owed > p2.taxes_owed) { return p1 }发布于 2016-11-01 11:43:46
你绝对是在正确的轨道上。我会使用更多的变量来简化税收的比较:
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;
}
}
}https://stackoverflow.com/questions/40353667
复制相似问题