Java 我应该使用哪种对象?
我不明白应该在Wind beaufort=新风(u__;)的标识符中放入哪个对象;。这里的第一行代码是构造函数,第二行代码是类Java 我应该使用哪种对象?,java,Java,我不明白应该在Wind beaufort=新风(u__;)的标识符中放入哪个对象;。这里的第一行代码是构造函数,第二行代码是类 import java.util.Scanner; public class WindSpeedCalc { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("W
import java.util.Scanner;
public class WindSpeedCalc
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Wind speed in km/h: ");
double kmH = scanner.nextDouble();
Wind wind = new Wind(kmH);
System.out.printf("The wind speed is equivalent to %.2f knots to two decimal places\n", wind.calculateKnots());
Wind beaufort = new Wind(idkk);
System.out.printf("The wind speed ranks around a %.1f on the Beaufort Scale\n", wind.calculateB());
System.out.print(wind.analyzeWind());
}
}
这是连接到上述构造函数的类。我不相信这个有什么问题
public class Wind
{
double kmH;
final double KM_IN_MI = 1.852;
public Wind(double speedKmH)
{
kmH = speedKmH;
}
public double calculateKnots()
{
double knots = kmH / KM_IN_MI;
return knots;
}
public int calculateB()
{
double mS = kmH / 3.6;
double pow = 2.0 / 3.0;
int b = (int)Math.round(Math.pow((mS / 0.836), pow));
if(b > 12)
b = 12;
return b;
}
public String analyzeWind()
{
String s = "";
if(kmH < 2)
s = "Wind is calm";
else if (kmH > 120)
s = "European storm";
else
s = "Moderate to strong wind";
return s;
}
}
公共级风
{
双公里小时;
最终双公里数=1.852;
公共风速(双速公里小时)
{
kmH=速度kmH;
}
公共双计算nots()
{
双结=公里小时/公里英寸/英里;
返回结;
}
公共整数计算b()
{
双mS=kmH/3.6;
双功率=2.0/3.0;
intb=(int)数学圆(Math.pow((mS/0.836),pow));
如果(b>12)
b=12;
返回b;
}
公共字符串analyzeWind()
{
字符串s=“”;
如果(公里小时<2)
s=“风是平静的”;
否则,如果(公里小时>120)
s=“欧洲风暴”;
其他的
s=“中强风”;
返回s;
}
}
您可以使用第一个对象访问calculateB()方法,而无需声明另一个对象
import java.util.Scanner;
public class WindSpeedCalc
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Wind speed in km/h: ");
double kmH = scanner.nextDouble();
Wind wind = new Wind(kmH);
System.out.printf("The wind speed is equivalent to %.2f knots to two decimal places\n", wind.calculateKnots());
System.out.printf("The wind speed ranks around a %.1f on the Beaufort Scale\n", (double)wind.calculateB());
System.out.print(wind.analyzeWind());
}
}
或如果出于某些原因要声明新对象,只需传递与以下代码相同的变量“kmH”:
import java.util.Scanner;
public class WindSpeedCalc
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Wind speed in km/h: ");
double kmH = scanner.nextDouble();
Wind wind = new Wind(kmH);
System.out.printf("The wind speed is equivalent to %.2f knots to two decimal places\n", wind.calculateKnots());
Wind beaufort = new Wind(kmH);
System.out.printf("The wind speed ranks around a %.1f on the Beaufort Scale\n", (double)beaufort.calculateB());
System.out.print(wind.analyzeWind());
}
}
如果没有其他速度值可供使用,则不需要第二个
风对象。只需使用一个Wind
对象并对其调用您的方法。提示:查看public int calculateB()
方法。你怎么能叫它?它返回什么?但是风不被认为是类吗?你会被类和对象弄糊涂<代码>风
是你的班级。类是对象的蓝图。您当前正在创建两个对象wind
(小写w)和beaufort
。你可以在这里读到更多你认为你能给我一点提示吗?嗯,不要粗鲁,但第一个是我已经尝试过的,当我运行它时,它会给我一个错误。错误是java.util.IllegalFormatConversionException:f!=java.base/java.util.Formatter$FormattSpecifier.failConversion(Formatter.java:4426)java.base/java.util.Formatter$FormattSpecifier.printFloat(Formatter.java:2951)java.base/java.util.Formatter$FormattSpecifier.print(Formatter.java:2898)java.base/java.util.Formatter.Formatter.Formatter(Formatter.java:2673)在WindSpeedCalc.main(WindSpeedCalc.java:18)@GeorgeParker的java.base/java.io.PrintStream.format(PrintStream.java:1053)和java.base/java.io.PrintStream.printf(PrintStream.java:949)的java.base/java.io.PrintStream.format(PrintStream.java:1053)中,您收到了错误,因为您的calculateB()方法返回了一个整数,并且您给它一个浮点格式。只需在其前面粘贴一个双掷,它应该是精细的编辑此行为:System.out.printf(“风速在蒲福标度上排列在%.1f左右,”(双)wind.calculateB());