Learning to use methods, keep getting error: "cannot convert from void to int"
Learning to use methods, keep getting error: "cannot convert from void to int"
我正在尝试制作一个脚本,使用方法打印出最大和最小的数字,但是我不知道为什么我的方法不起作用......我在我不断得到的地方旁边发表了评论错误。
我真的不需要帮助让脚本工作,但是如果有人能向我解释为什么我的方法不起作用,我将不胜感激。
代码如下:
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;
}
}
}
修改方法的 return
签名,将值赋给 smallest
(或 biggest
),然后 return 变量。喜欢,
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);
}
问题是您试图从一个没有 return 任何东西的方法中为变量赋值。
biggest = biggest(nr1, nr2, nr3); // here is where I keep getting the error
请注意,您的方法签名设置为 void,这意味着不会 returned
public static void biggest(int nr1, int nr2, int nr3) {}
您的 biggest() 和 smallest() 方法应该 return biggest/smallest 值,方法签名中的 void 应该 int.
我正在尝试制作一个脚本,使用方法打印出最大和最小的数字,但是我不知道为什么我的方法不起作用......我在我不断得到的地方旁边发表了评论错误。
我真的不需要帮助让脚本工作,但是如果有人能向我解释为什么我的方法不起作用,我将不胜感激。
代码如下:
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;
}
}
}
修改方法的 return
签名,将值赋给 smallest
(或 biggest
),然后 return 变量。喜欢,
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);
}
问题是您试图从一个没有 return 任何东西的方法中为变量赋值。
biggest = biggest(nr1, nr2, nr3); // here is where I keep getting the error
请注意,您的方法签名设置为 void,这意味着不会 returned
public static void biggest(int nr1, int nr2, int nr3) {}
您的 biggest() 和 smallest() 方法应该 return biggest/smallest 值,方法签名中的 void 应该 int.