【发布时间】: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”