【问题标题】:Printf statement doesn't like my inputsPrintf 语句不喜欢我的输入
【发布时间】:2015-09-27 00:00:49
【问题描述】:
public int maxRate(int maxRate)
    {
        maxRate = 220 - age;

        return maxRate;
    }

    // calculate target rate
    public String targetRate(int targetRateMin, int targetRateMax, String targetRate)
    {

        targetRateMin = (age * (1/2));
        targetRateMax = (age * (17/20));
        targetRate = (targetRateMin + "-" + targetRateMax);

        return targetRate;
    }

    // display message
    public void displayMessage()
    {
        System.out.printf("Name: %s %s"
                + "\n Age: %d"
                + "\n Target Heart Rate: %s"
                + "\n Maximum Heart Rate: %d"
                , getFirstName(), getLastName(), calculateAge(age), targetRate(), maxRate());
    }

当我尝试在我的 printf 语句中使用 targetRate()maxRate 时,我很困惑为什么会出现错误(我收到红色波浪状的东西告诉我添加我不明白的东西) .是我的方法不对还是怎么回事?

【问题讨论】:

  • 请显示您的错误/异常消息。此外,在您的 printf 格式字符串中将 \n 更改为 %n
  • 你的方法:public int maxRate(int maxRate);你的方法调用:maxRate() ...我真的想知道问题出在哪里。

标签: java methods


【解决方案1】:

您没有为调用的方法提供有效参数。

getFirstName(), getLastName(), calculateAge(age), targetRate(), maxRate());

例如,您的 targetRate()maxRate() 没有参数。

根据你maxRate()targetRate()的方法签名,你需要

  • maxRate(int) 的 1 个 int 参数
  • targetRate(int, int, String) 的 2 个整数和 1 个字符串参数

【讨论】:

    【解决方案2】:

    最大速率()

    接受一个整数参数。

    【讨论】:

    • 并且targetRate 具有签名(int, int, String),并且它被调用时不带任何参数。
    猜你喜欢
    • 2011-03-18
    • 2013-02-18
    • 2013-11-03
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    • 2011-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多