我正在尝试一项计划挑战,该计划挑战需要输入空气,水或钢,并显示Java中以英尺为单位的声音传播速度。
我的驱动程序代码如下所示
import java.util.Scanner;
public class driver
{
public static void main(String[] args)
{
String type;
int distance;
int time = 0;
Scanner keyboard = new Scanner(System.in);
System.out.print("Input one of the following- Air, water, or steel.");
type = keyboard.nextLine();
System.out.print("Input distance in feet");
distance = keyboard.nextInt();
speedofsound sound = new speedofsound(distance);
if (type.equals("Air"))
time = sound.getSpeedInAir();
System.out.println(time);
if (type.equals("Water"))
time = sound.getSpeedInWater();
if (type.equals("Steel"))
time = sound.getSpeedInSteel();
System.out.println("The time it would take for" + type + "to travel" + distance + "feet is" + time);
}
}
,班级看起来像这样
public class speedofsound {// class stating
private int distance;//distance integer
private int time;
public speedofsound (int d)
{
distance = d; // from driver pass over dist in ft,
}
public int getSpeedInAir()
{
return (distance / 1100);
}
public int getSpeedInWater()
{
time = distance / 4900;
return time;
}
public int getSpeedInSteel()
{
time = distance / 16400;
return time;
}
}
当我在控制台中执行此操作时,我得到“输入以下内容之一 - 空气,水或钢。空气 输入距离(英尺)50 0 Airto travel50feet需要的时间是0“ 无论我输入什么,我都会得到0.什么东西?