我正在编写一个程序,让用户输入 6 个温度读数,然后
- 返回最高原始值+celcius版本
- 返回原始值 + 转换为摄氏度版本。
设置数组值的代码如下:
System.out.print( "Enter Temperature:\t"); //Get the count...
Temp = LocalInput.nextInt();
WeatherSpots[K].CatchCount = Temp;
我收到的错误消息是这样的
java.util.IllegalFormatConversionException: f != java.lang.Integer
at java.util.Formatter$FormatSpecifier.failConversion(Unknown Source)
at java.util.Formatter$FormatSpecifier.printFloat(Unknown Source)
at java.util.Formatter$FormatSpecifier.print(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.lang.String.format(Unknown Source)
at p2list.WeeklyReport(p2list.java:102)
at p2list.main(p2list.java:33)"
我还发现了给我带来麻烦的确切短语:
String.format("%.2d", (WeatherSpots[K].CatchCount - 32) * 5 / 9)"
我知道当我的"%._"
没有正确的说明符,但我所有的变量和数组都是 int,所以 d 应该可以工作
这是其余的代码:
这就是我设置第一个数组的方式:
private static WeatherLocation[] WeatherSpots = new WeatherLocation[6];"
这是后面数组使用的类
public class WeatherLocations extends WeatherLocation {
public String LocationID;
public Integer CatchCount;"
arrays = WeatherSpots.LoccationID/Catchcount"
这里是catchcount
数组根据用户输入的温度进行设置
int K;
for(K = 0 ; K < 6 ; K++){
System.out.print( "Enter Temperature:\t");
Temp = LocalInput.nextInt();
WeatherSpots[K].CatchCount = Temp;
这是我尝试调用的方法WeatherSpots[K].catchcount
要转换为摄氏度的值
int K= 0;
for(K = 0 ; K < 6 ; K++){
System.out.println( "" + WeatherSpots[K].LocationID +"\t\t" + WeatherSpots[K].CatchCount + "\t\t" + String.format("%.2f", (WeatherSpots[K].CatchCount - 32) * 5 / 9));
如果我的数组和变量是正确使用的类型,那么会导致错误的原因是什么string.format
?