padwithspaces.java

来自「《A first book of java》by Gary J.Bronson 」· Java 代码 · 共 28 行

JAVA
28
字号
public class PadWithSpaces
{
  public static void main(String[] args)
  {
    final int WIDTH = 8;  // this sets the size of the output field
    int n, numlength;
    String outstr = "";
    int testNumber;
    
    testNumber = 6;  // this is the number we will output
    outstr += testNumber;  // convert to a string
    numlength = outstr.length();  // get the number of integer digits
    n = WIDTH - numlength;  // determine number of spaces needed
    outstr = addSpaces(n) + outstr;
    System.out.println(outstr);
  }
    // this method creates a string with n space characters
  public static String addSpaces(int n)
  {
    StringBuffer temp = new StringBuffer(n);
    int i;
    for(i = 0; i < n; i++)
    temp.append(' ');    // append a blank
  
    return temp.toString();
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?