【问题标题】:Begginer struggle at format string格式字符串的初学者斗争
【发布时间】:2019-03-04 01:40:30
【问题描述】:

它告诉我字符串不能转换为系统... 但我真的不知道我犯了什么错误...... 在软件开发方面还是很新的.. 请帮帮我...

import java.util.Scanner;

public class Serioustake1 
{
    public static void main(String[]args)
    {
        int age;
        String a = "Donald John Trump" ,b = "Height :",c = "Age :", d = "Name : ";
        float height;   

        Scanner input = new Scanner(System.in);

        System.out.print("Please enter the height of president:");
        height=input.nextFloat();

        System.out.print("Please enter the age of president:");
        age=input.nextInt();

        System.out.println("\n\n\t\t\t$$$$$$$$$$$$$$$$$$$$$$$$$$");
        System.out.println("\t\t\t\tPresident Details");
        System.out.println("\t\t\t$$$$$$$$$$$$$$$$$$$$$$$$$$$");

        System formatString = String.format("%-15s%-10s\n%-15s%-10.1f\n%-15s%-10d",
                                              d,a,b,height,c,age);
        System.out.print(formatString);

    }
}

【问题讨论】:

    标签: jcreator


    【解决方案1】:

    String.format()的返回类型为String

    所以改变

       System formatString = String.format("%-15s%-10s\n%-15s%-10.1f\n%-15s%-10d",
                                              d,a,b,height,c,age);
    

       String formatString = String.format("%-15s%-10s\n%-15s%-10.1f\n%-15s%-10d",
                                              d,a,b,height,c,age);
    

    【讨论】:

    • 当然..但它需要时间,它说我可以在 7 分钟内接受它
    猜你喜欢
    • 1970-01-01
    • 2021-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多