第六章第三十七题(格式化整数)(Format an integer)

第六章第三十七题(格式化整数)(Format an integer)

  • 6.37(格式化整数)使用下面的方法头编写一个方法,用于将整数格式化为指定宽度:
    public static String format(int number, int width)
    方法为数字number返回一个带有一个或多个以0作为前缀的字符串。字符串的位数就是宽度。比如,format(34,4)返回0034,format(34,5)返回00034。如果数字宽于指定宽度,方法返回该数字的字符串表示。比如,format(34,1)返回34。
    6.37(Format an integer)Write a method with the following header to format the integer with the specified width.
    public static String format(int number, int width)
    The method returns a string for the number with one or more prefix 0s. The size of the string is the width. For example, format(34, 4) returns 0034 and format(34, 5) returns 00034. If the number is longer than the width, the method returns the string representation for the number. For example, format(34, 1) returns 34.
    Write a test program that prompts the user to enter a number and its width, and displays a string returned by invoking format(number, width).
  • 参考代码:
package chapter06;

import java.util.Scanner;

public class Code_37 {
    public static void main(String[] args) {
        Scanner inputScanner = new Scanner(System.in);
        System.out.print("Enter the number: ");
        int number = inputScanner.nextInt();
        System.out.print("Enter the width: ");
        int width = inputScanner.nextInt();
        System.out.printf("The string is %s", format(number, width));
    }
    public static String format(int number, int width) {
        String numberString = String.valueOf(number);
        int lengthOfNumber = numberString.length();
        for(int i = 1;i <= width - lengthOfNumber;i++)
            numberString = "0" + numberString;
        return numberString;
    }
}

  • 结果显示:
Enter the number: 34
Enter the width: 5
The string is 00034
Process finished with exit code 0

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 VB.NET 中,可以使用 String.Format 方法进行字符串连接和格式化。这个方法可以接收一个格式化字符串和一个或多个要格式化的对象,然后返回一个格式化后的字符串。 下面是一个示例: ``` Dim name As String = "John" Dim age As Integer = 25 Dim str As String = String.Format("My name is {0} and I am {1} years old.", name, age) Console.WriteLine(str) ``` 在上面的示例中,我们定义了一个名为 name 的字符串变量和一个名为 age 的整数变量。然后,我们使用 String.Format 方法将这些变量插入到一个格式化字符串中,生成最终的字符串并将其输出到控制台。 在格式化字符串中,我们使用了 {0} 和 {1} 占位符来代表要插入的变量。这些占位符的索引从零开始,对应着后面传入 String.Format 方法的参数的位置。 除了用整数索引来指定要插入的参数,还可以使用参数名,如下所示: ``` Dim name As String = "John" Dim age As Integer = 25 Dim str As String = String.Format("My name is {name} and I am {age} years old.", name:=name, age:=age) Console.WriteLine(str) ``` 在上面的示例中,我们使用了参数名来指定要插入的变量,这样可以使代码更加清晰易懂。 除了占位符,格式化字符串中还可以包含一些格式化选项,用于控制输出的格式。例如,可以使用以下格式化选项将数字格式化为货币: ``` Dim price As Double = 1234.56 Dim str As String = String.Format("The price is {0:C}.", price) Console.WriteLine(str) ``` 在上面的示例中,我们使用了格式化选项 {0:C} 将 price 变量格式化为货币。输出的结果为 "$1,234.56"。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值