该代码应该询问事故的数量,并确定五个地区中哪个事故发生率最低,并打印事故数量和地区。我对java很新,我遇到了这个错误。它说第四类Homework中的方法getNumAccidents不能应用于给定的类型; required:double found:无参数原因:实际和正式列表的长度不同。我该如何解决这个问题?
import java.util.*;
public class fourthHomework
{
public static void main(String args[]) {
double smallest = 0;
double north = getNumAccidents();
double south = getNumAccidents();
double east = getNumAccidents();
double west = getNumAccidents();
double central = getNumAccidents();
getNumAccidents(smallest);
findLowest(north, south, east, west, central, smallest);
}
public static double getNumAccidents(double smallest) {
System.out.println("Please enter all accidents as positive numbers.");
Scanner input = new Scanner(System.in);
System.out.print("Please enter the amount of accidents: ");
smallest = input.nextDouble();
}
public static void findLowest(double north, double south, double east, double west, double central, double smallest) {
System.out.println("The lowest amount of accidents this year was " +smallest);
if (north == smallest)
{
System.out.println("The region that had the least amount of accidents was the north.");
}
if (south == smallest)
{
System.out.println("The region that had the least amount of accidents was the south.");
}
if (east == smallest)
{
System.out.println("The region that had the least amount of accidents was the east.");
}
if (west == smallest)
{
System.out.println("The region that had the least amount of accidents was the west.");
}
if (central == smallest)
{
System.out.println("The region that had the least amount of accidents was the central region.");
}
} }
答案 0 :(得分:0)
您的问题是因为在您的方法getNumAccidents(最小的双倍)中,您需要double作为参数。如果您声明北,南,东,西和中央的双打,则不要为该方法添加任何参数。你需要添加双打,否则它会运行。此外,既然你说该方法会返回一个double,你需要在方法的最后说
return smallest
答案 1 :(得分:0)
让主方法的开头显示:
public static void main(String args[]) {
double smallest = 0.0;
double north = getNumAccidents(smallest);
double south = getNumAccidents(smallest);
double east = getNumAccidents(smallest);
double west = getNumAccidents(smallest);
double central = getNumAccidents(smallest);
...
在你的getNumAccidents
方法中,由于它被声明为double,你需要返回一个double值:
...
smallest = input.nextDouble();
return smallest;
}
这不会完全修复该程序,但它可以帮助您解决问题。