【问题标题】:Java: argument list error in integer declarationJava:整数声明中的参数列表错误
【发布时间】:2014-11-23 18:01:32
【问题描述】:

所以我在第 18 行遇到了一个错误,它描述了:

HelloWorld.java:18: error: method calcThirdChance in class HelloWorld cannot be applied to given    types;
     int t3 = calcThirdChance(t1+t2);
              ^
required: int,int
found: int
reason: actual and formal argument lists differ in length

这对我来说意义不大,因为方法 calcThirdChance 返回一个 int,而我在该行中只声明一个 int,因此我不知道为什么它需要 2 个 int,据称这是错误的原因。

public class HelloWorld{

 public static void main(String []args){

    System.out.println(simMin(70,80));
 }
 public static int calcThirdChance(int t1, int t2){
    int c;
     c = -1*(t1+t2);
    double finalsend;
     finalsend = Math.pow(2.71, c/100)*2;
    finalsend*=150;
     return (int)finalsend;
 }
 public static int simMin(int t1, int t2){
     int t3 = calcThirdChance(t1+t2);
     int total=t1+t2+t3;
     int play = 50;
     if(play<=t1){
         return t1;
     }
     else if(play<=t1+t2){
         return t2;
     }
     else{
         return t3;
     }

 }
}

【问题讨论】:

    标签: java class methods int declaration


    【解决方案1】:

    calcThirdChance 接受 2 个参数

    int t3 = calcThirdChance(t1, t2); 
    

    【讨论】:

    • 但是只返回 1,我需要 calcThirdCHance 返回的值,它只有 1 个 int。 t1 和 t2 已声明
    • @user1832697 你将拥有它在t3
    【解决方案2】:

    这个

    calcThirdChance(t1+t2);
    

    传递t1+t2 的结果(一个值),但您的方法需要两个。我想你想要

    calcThirdChance(t1, t2); // <-- a comma
    

    【讨论】:

    • 哦,谢谢,我的眼镜丢了,看不清楚
    【解决方案3】:

    你得到的错误与calcThirdChance的返回类型无关。

    您将单个参数传递给方法:

    calcThirdChance(t1+t2);
    

    你需要通过两个:

    calcThirdChance(t1,t2);
    

    顺便说一句,看到calcThirdChance(int t1, int t2) 只需要它收到的参数的总和,您可以将其更改为:

    public static int calcThirdChance(int t)
    {
        int c;
        c = -1*t;
        double finalsend;
        finalsend = Math.pow(2.71, c/100)*2;
        finalsend*=150;
        return (int)finalsend;
    }
    

    这将使调用 calcThirdChance(t1+t2) 有效。

    【讨论】:

      猜你喜欢
      • 2020-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-20
      • 1970-01-01
      • 2017-01-18
      • 2013-08-02
      相关资源
      最近更新 更多