Exception in thread “main” java.util.IllegalFormatConversionException: d != java.lang.String

后端 未结 3 1566
深忆病人
深忆病人 2020-11-28 00:15

I\'m new to Java and I\'m not sure how to fix the error I\'m getting when trying to run this code:

import java.util.Scanner;
public class P3_3 
{

    publi         


        
3条回答
  •  南方客
    南方客 (楼主)
    2020-11-28 00:45

    The problem is here:

    System.out.printf("%d has ", number + "digits.");
    

    the %d format specifier requires an integer to be passed as second parameter to printf, but by concatenating number and "digits.", you actually passed a String.

    Fixed version:

    System.out.printf("has %d digits ", number);
    

    note that you cannot print both the original number and the number of digits, because you overwrote one with the other in the number variable. Maybe use two different ones.

提交回复
热议问题