java - 如何编写交互式程序(参数和对象)

标签 java object parameters interactive

在我的计算机科学课上,我们遇到了以下问题:

Write a Java program that includes a method called pay that accepts two parameters: a real number for an employee's hourly wage and an integer for the number of hours the employee worked that day. The method should return how much money to pay the employee for this day.

例如,调用 pay(5.50, 6) 应返回 33.0。 对于超过 8 的任何时间,员工应获得正常小时工资 1.5 倍的“加类”工资。例如,调用 pay(4.00, 11) 应返回 50.0(计算方式为 (4.00 * 8) + (6.00 *3)) .

在此程序的 main 方法中包含四次对 pay 方法的调用。两次调用,每次调用都使用上面给出的示例数据,两次调用,每次调用都使用您确定的数据。

到目前为止,这就是我所得到的一切,但我觉得什么都没有:

import java.util.*;

public class Payjava {

    //obtain values
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        System.out.println(" Wage   : ");
        double wage = console.nextDouble();
        System.out.print(" Hours    :");
        int hours = console.nextInt();

        // compute results
        double overtime = 1.5;
        double overtime = hours > 8 * (wage * 1.5);
        wage = hours * wage;
    }
}

最佳答案

我建议首先将问题分解为更小的部分。 该问题要求一种计算员工工资的方法,因此这似乎是一个合理的起点。该问题为您提供了该方法的名称以及它应采用哪些参数:

...a method called pay that accepts two parameters: a real number for an employee's hourly wage and an integer for the number of hours the employee worked that day. The method should return how much money to pay the employee for this day.

所以你的方法将如下所示:

public double pay(double hourlyWage, int numberOfHours) {
//TODO: complete
}

然后开始在方法中添加逻辑 - 从不加类的简单情况开始,然后返回并添加加类计算。

最后,请注意,问题要求在主方法中包含四次对 pay 方法的调用 - 因此您实际上不需要提示输入。

关于java - 如何编写交互式程序(参数和对象),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29192211/

相关文章:

java - 是否应该在 equals 和 hashCode 中考虑 JPA 实体的 id 字段?

java - 如何在java代码中连接到调制解调器路由器

generics - 我可以在没有未经检查的异常的情况下使用 Collections.EMPTYLIST 吗?

java - 如何将对象添加到对象 HashMap 中的已排序集合

java - java的命令提示符表示法

java - 从 HashMap 中读取对象内容

ios - 移除 AppDelegate 在其他 ViewController 中的观察者

Javadocs - @param 名称未找到

c - 在 C 中使用 ShellExecute() 打开 .txt 的正确方法是什么

c++ - 交换 std::vector 作为函数参数