capitals.java
来自「Beginning Java 2, SDK 1.4 Edition Exerci」· Java 代码 · 共 36 行
JAVA
36 行
//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 + =
减小字号Ctrl + -
显示快捷键?