【问题标题】:Learning to use methods, keep getting error: "cannot convert from void to int"学习使用方法,不断报错:“cannot convert from void to int”
【发布时间】:2015-07-22 15:11:39
【问题描述】:

我正在尝试制作一个使用方法打印出最大和最小数字的脚本,但是我不知道为什么我的方法不起作用......我在我不断收到错误的地方旁边放了一条评论。

我真的不需要帮助来让脚本正常工作,但是如果有人可以向我解释为什么我的方法不起作用,我将不胜感激。

代码如下:

import java.util.Scanner;

public class MinMax {
    public static void main(String[] args) {
        int nr1, nr2, nr3, biggest, smallest;
        Scanner lukija = new Scanner(System.in);

        System.out.print("Input first value: ");
        nr1 = lukija.nextInt();

        System.out.print("Input second value: ");
        nr2 = lukija.nextInt();

        System.out.print("Input third value: ");
        nr3 = lukija.nextInt();

        biggest = biggest(nr1, nr2, nr3); // here is where I keep getting the error
        smallest = smallest(nr1, nr2, nr3); // here too

        System.out.print(biggest + " was the biggest number.");
        System.out.print(smallest + " was the smallest number.");

    }


public static void biggest(int nr1, int nr2, int nr3){

    int biggest = 0;

    if (nr1>nr2 && nr1>nr3){
        nr1=biggest;
    } else if (nr2>nr1 && nr2>nr3){
        nr2=biggest;
    } else if (nr3>nr1 && nr3>nr2){
        nr3=biggest;
    }


}

public static void smallest(int nr1, int nr2, int nr3){

    int smallest = 0;

    if (nr1<nr2 && nr1<nr3){
        nr1=smallest;
    } else if (nr2<nr1 && nr2<nr3){
        nr2=smallest;
    } else if (nr3<nr1 && nr3<nr2){
        nr3=smallest;
    }


}
}

【问题讨论】:

  • 是最大和最小两个函数的返回值。您已经写过,当您实际上希望它们返回“int”时,它们会返回“void”(即什么也没有)。函数中的代码也是从后到前的。你需要写成“smallest = nr1”

标签: java methods max min


【解决方案1】:

修改方法的return 签名,将值分配给smallest(或biggest),然后返回变量。喜欢,

public static int biggest(int nr1, int nr2, int nr3){
    int biggest = 0;

    if (nr1>nr2 && nr1>nr3){
        biggest = nr1;
    } else if (nr2>nr1 && nr2>nr3){
        biggest = nr2;
    } else if (nr3>nr1 && nr3>nr2){
        biggest = nr3;
    }
    return biggest;
}

public static int smallest(int nr1, int nr2, int nr3){
    int smallest = 0;
    if (nr1<nr2 && nr1<nr3){
        smallest = nr1;
    } else if (nr2<nr1 && nr2<nr3){
        smallest = nr2;
    } else if (nr3<nr1 && nr3<nr2){
        // nr3=smallest;
        smallest = nr3;
    }
    return smallest;
}

你可以像这样简化上面的

public static int biggest(int nr1, int nr2, int nr3){
    int biggest = Math.max(nr1, nr2);
    return Math.max(biggest, nr3);
}

public static int smallest(int nr1, int nr2, int nr3){
    int smallest = Math.min(nr1, nr2);
    return Math.min(smallest, nr3);
}

【讨论】:

    【解决方案2】:

    问题是您试图通过不返回任何内容的方法为变量赋值。

    biggest = biggest(nr1, nr2, nr3); // here is where I keep getting the error
    

    请注意,您的方法签名设置为无效,这意味着不会返回任何内容

    public static void biggest(int nr1, int nr2, int nr3) {}
    

    您的最大()和最小()方法应该返回最大/最小值,方法签名中的void应该是int

    【讨论】:

      猜你喜欢
      • 2018-09-11
      • 2022-11-25
      • 2022-12-02
      • 1970-01-01
      • 2022-12-27
      • 1970-01-01
      • 2021-12-22
      • 2020-03-28
      • 2021-10-02
      相关资源
      最近更新 更多