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

📄 capitals.java

📁 Beginning Java 2, SDK 1.4 Edition Exercise Code samples for this book
💻 JAVA
字号:
//Chapter 3, Exercise 4

public class Capitals {
  public static void main(String args[])   {
    int counter = 0;               // Counter for number of capitals generated. 
    int numberToGenerate = 20;     // Number of capitals to generate.
    char symbol = 0;               // Variable to store a random character.

    // While there are still letters to generate:
    while( counter < numberToGenerate ) {
      // Generate a random symbol between A and Z:
      // This relies on the fact that the codes for the letters are in a
      // contiguous sequence from 'A' to 'Z'. IF we add 1 to 'A' we get the
      // code for 'B', if we add 2 we get 'C', and so on. Thus to create a 
      // random capital letter from 'A' to 'Z' we can add a random integer
      // between 0 and 25 to 'A'.
      symbol = (char)(26*Math.random() + 'A');
      switch(symbol) {
        //Vowels ignored:
        case 'A':
        case 'E':
        case 'I':
        case 'O':
        case 'U':
          break;
        default:
          //Consonant displayed:
          System.out.print(symbol + " ");
          counter++;
          break;
      }
    }
  }
}

⌨️ 快捷键说明

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