⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 padwithspaces.java

📁 《A first book of java》by Gary J.Bronson 北大出版社
💻 JAVA
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -