Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 字符串。四舍五入格式,can';找不到非法的格式转换源错误?_Java_String Formatting - Fatal编程技术网

Java 字符串。四舍五入格式,can';找不到非法的格式转换源错误?

Java 字符串。四舍五入格式,can';找不到非法的格式转换源错误?,java,string-formatting,Java,String Formatting,我正在写一个程序,让用户输入6个温度读数,然后 返回最高原始值+celcius版本 返回原始值并转换为摄氏度版本 设置数组值的代码如下所示: System.out.print( "Enter Temperature:\t"); //Get the count... Temp = LocalInput.nextInt(); WeatherSpots[K].CatchCount = Temp; 我得到的错误消息是 java.util.IllegalFormatC

我正在写一个程序,让用户输入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)"
    
    我知道当我的
    “%.\u”
    没有正确的说明符时会发生错误,但我的所有变量和数组都是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;
    
    如果我的数组和变量是使用
    string.format
    的正确类型,会导致错误的原因是什么

    In

    String.format("%.2f", (WeatherSpots[K].CatchCount - 32) * 5 / 9)
    
    您正在尝试打印格式为
    double
    s或
    float
    s的
    int
    。这会导致非法格式转换异常:f!=java.lang.Integer。因为整数除法会截断,所以打印小数点后有两位的整数不是很有用。只需除以浮点数
    9.0
    而不是
    int
    9,就可以得到一个可以用
    %.2f
    格式化的浮点数

    在你的

    String.format("%.2d", (WeatherSpots[K].CatchCount - 32) * 5 / 9)
    

    格式
    %.2d
    无效,因为打印小数点后带位的整数是没有意义的。

    @user1476390:因为这个答案解决了您的问题,您应该点击丹尼尔的答案左边的向上箭头,向上投票给丹尼尔的答案(就像我所做的那样),然后你应该点击它左边的大复选标记来接受它。
    String.format("%.2d", (WeatherSpots[K].CatchCount - 32) * 5 / 9)