?? module3.lst
字號:
listing 1
// Read a character from the keyboard.
class KbIn {
public static void main(String args[])
throws java.io.IOException {
char ch;
System.out.print("Press a key followed by ENTER: ");
ch = (char) System.in.read(); // get a char
System.out.println("Your key is: " + ch);
}
}
listing 2
// Guess the letter game.
class Guess {
public static void main(String args[])
throws java.io.IOException {
char ch, answer = 'K';
System.out.println("I'm thinking of a letter between A and Z.");
System.out.print("Can you guess it: ");
ch = (char) System.in.read(); // read a char from the keyboard
if(ch == answer) System.out.println("** Right **");
}
}
listing 3
// Guess the letter game, 2nd version.
class Guess2 {
public static void main(String args[])
throws java.io.IOException {
char ch, answer = 'K';
System.out.println("I'm thinking of a letter between A and Z.");
System.out.print("Can you guess it: ");
ch = (char) System.in.read(); // get a char
if(ch == answer) System.out.println("** Right **");
else System.out.println("...Sorry, you're wrong.");
}
}
listing 4
// Guess the letter game, 3rd version.
class Guess3 {
public static void main(String args[])
throws java.io.IOException {
char ch, answer = 'K';
System.out.println("I'm thinking of a letter between A and Z.");
System.out.print("Can you guess it: ");
ch = (char) System.in.read(); // get a char
if(ch == answer) System.out.println("** Right **");
else {
System.out.print("...Sorry, you're ");
// a nested if
if(ch < answer) System.out.println("too low");
else System.out.println("too high");
}
}
}
listing 5
// Demonstrate an if-else-if ladder.
class Ladder {
public static void main(String args[]) {
int x;
for(x=0; x<6; x++) {
if(x==1)
System.out.println("x is one");
else if(x==2)
System.out.println("x is two");
else if(x==3)
System.out.println("x is three");
else if(x==4)
System.out.println("x is four");
else
System.out.println("x is not between 1 and 4");
}
}
}
listing 6
// Demonstrate the switch.
class SwitchDemo {
public static void main(String args[]) {
int i;
for(i=0; i<10; i++)
switch(i) {
case 0:
System.out.println("i is zero");
break;
case 1:
System.out.println("i is one");
break;
case 2:
System.out.println("i is two");
break;
case 3:
System.out.println("i is three");
break;
case 4:
System.out.println("i is four");
break;
default:
System.out.println("i is five or more");
}
}
}
listing 7
// Demonstrate the switch without break statements.
class NoBreak {
public static void main(String args[]) {
int i;
for(i=0; i<=5; i++) {
switch(i) {
case 0:
System.out.println("i is less than one");
case 1:
System.out.println("i is less than two");
case 2:
System.out.println("i is less than three");
case 3:
System.out.println("i is less than four");
case 4:
System.out.println("i is less than five");
}
System.out.println();
}
}
}
listing 8
switch(i) {
case 1:
case 2:
case 3: System.out.println("i is 1, 2 or 3");
break;
case 4: System.out.println("i is 4");
break;
}
listing 9
switch(ch1) {
case 'A': System.out.println("This A is part of outer switch.");
switch(ch2) {
case 'A':
System.out.println("This A is part of inner switch");
break;
case 'B': // ...
} // end of inner switch
break;
case 'B': // ...
listing 10
/*
Project 3-1
A simple help system.
*/
class Help {
public static void main(String args[])
throws java.io.IOException {
char choice;
System.out.println("Help on:");
System.out.println(" 1. if");
System.out.println(" 2. switch");
System.out.print("Choose one: ");
choice = (char) System.in.read();
System.out.println("\n");
switch(choice) {
case '1':
System.out.println("The if:\n");
System.out.println("if(condition) statement;");
System.out.println("else statement;");
break;
case '2':
System.out.println("The switch:\n");
System.out.println("switch(expression) {");
System.out.println(" case constant:");
System.out.println(" statement sequence");
System.out.println(" break;");
System.out.println(" // ...");
System.out.println("}");
break;
default:
System.out.print("Selection not found.");
}
}
}
listing 11
// Show square roots of 1 to 99 and the rounding error.
class SqrRoot {
public static void main(String args[]) {
double num, sroot, rerr;
for(num = 1.0; num < 100.0; num++) {
sroot = Math.sqrt(num);
System.out.println("Square root of " + num +
" is " + sroot);
// compute rounding error
rerr = num - (sroot * sroot);
System.out.println("Rounding error is " + rerr);
System.out.println();
}
}
}
listing 12
// A negatively running for loop.
class DecrFor {
public static void main(String args[]) {
int x;
for(x = 100; x > -100; x -= 5)
System.out.println(x);
}
}
listing 13
for(count=10; count < 5; count++)
x += count; // this statement will not execute
listing 14
// Use commas in a for statememt.
class Comma {
public static void main(String args[]) {
int i, j;
for(i=0, j=10; i < j; i++, j--)
System.out.println("i and j: " + i + " " + j);
}
}
listing 15
// Loop until an S is typed.
class ForTest {
public static void main(String args[])
throws java.io.IOException {
int i;
System.out.println("Press S to stop.");
for(i = 0; (char) System.in.read() != 'S'; i++)
System.out.println("Pass #" + i);
}
}
listing 16
// Parts of the for can be empty.
class Empty {
public static void main(String args[]) {
int i;
for(i = 0; i < 10; ) {
System.out.println("Pass #" + i);
i++; // increment loop control var
}
}
}
listing 17
// Move more out of the for loop.
class Empty2 {
public static void main(String args[]) {
int i;
i = 0; // move initialization out of loop
for(; i < 10; ) {
System.out.println("Pass #" + i);
i++; // increment loop control var
}
}
}
listing 18
// The body of a loop can be empty.
class Empty3 {
public static void main(String args[]) {
int i;
int sum = 0;
// sum the numbers through 5
for(i = 1; i <= 5; sum += i++) ;
System.out.println("Sum is " + sum);
}
}
listing 19
// Declare loop control variable inside the for.
class ForVar {
public static void main(String args[]) {
int sum = 0;
int fact = 1;
// compute the factorial of the numbers through 5
for(int i = 1; i <= 5; i++) {
sum += i; // i is known throughout the loop
fact *= i;
}
// but, i is not known here.
System.out.println("Sum is " + sum);
System.out.println("Factorial is " + fact);
}
}
listing 20
// Demonstrate the while loop.
class WhileDemo {
public static void main(String args[]) {
char ch;
// print the alphabet using a while loop
ch = 'a';
while(ch <= 'z') {
System.out.print(ch);
ch++;
}
}
}
listing 21
// Compute integer powers of 2.
class Power {
public static void main(String args[]) {
int e;
int result;
for(int i=0; i < 10; i++) {
result = 1;
e = i;
while(e > 0) {
result *= 2;
e--;
}
System.out.println("2 to the " + i +
" power is " + result);
}
}
}
listing 22
// Demonstrate the do-while loop.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -