Experimental purposes:
Familiarity with Boolean operations
Proficiency in the use of branch statements and circular statement Control program execution process
Implement multiple branches with Swich statements.
Learn to control the loop termination condition, the normal exit of the loop, the use of the continue statement, the break statement.
Experimental Steps and Contents:
1. Write a program, when the keyboard input "WASD" one of the four keys (both uppercase and lowercase), the output corresponds to the direction. (Note using a For loop makes the program more humane)
2. Randomly select a number from 1-1000, repeatedly let the user guess what, until the user guessed right or the user quit. Each guess tells the user that the result of the guess is either too large or too small. Use a marker value to determine if the user wants to quit. When the user guesses right, tell him the number of guesses. At the end of each game, ask the user if they want to continue playing until the user chooses to finish.
package experiment4; import java.util.Scanner; public class HiLo {public static void main (string[] Arg
s) {int number=1,yournumber,i=1;
String choice = "Y";
while (Choice.equalsignorecase ("Y")) {number= (int) (Math.random () *1000) +1;
System.out.println ("Please enter a number (1-1000)");
Scanner scan=new Scanner (system.in);
Yournumber=scan.nextint (); if (yournumber>1000) {System.out.println ("the number you entered does not meet the requirement.)
Please re-enter ");
Yournumber=scan.nextint (); while (Yournumber<=1000&&number!=yournumber) {if (number>yournumber) {System.out.println ("you enter the
Small number, please re-enter ");
Yournumber=scan.nextint ();
i++;
} if (Number<yournumber) {System.out.println ("you entered a large number, please re-enter");
Yournumber=scan.nextint ();
i++; } if (Number==yournumber) {System.out.println ("Congratulations, guess it.") You have guessed the "+i+" time to guess.
Enter Y to continue the game, N give up the game ");
Choice=scan.nextline ();
Choice=scan.nextline (); }
}
}
}
Package experiment4;
Import Java.util.Scanner;
public class WSAD {public static void main (String [] args) {System.out.println ("Please enter one in WSAD");
Scanner scan=new Scanner (system.in);
String str;
Str=scan.nextline (); Boolean play = Str.equalsignorecase ("W") |str.equalsignorecase ("S") |str.equalsignorecase ("A") |str.equalsignorecase
("D");
while (true) {while (play) {if (Str.equalsignorecase ("W")) System.out.println ("Up");
if (Str.equalsignorecase ("S")) System.out.println ("bottom");
if (Str.equalsignorecase ("A")) System.out.println ("left");
if (Str.equalsignorecase ("D")) System.out.println ("right");
System.out.println ("Please enter one of the Wsad");
Str=scan.nextline ();
Play = Str.equalsignorecase ("W") |str.equalsignorecase ("S") |str.equalsignorecase ("A") |str.equalsignorecase ("D");
while (!play) {System.out.println ("you entered a request that does not meet the requirements, please re-enter one of the Wsad");
Str=scan.nextline (); Play = Str.equalsignorecase ("W") |str.equalsignorecase ("S") |str.equalsignorecase ("A") |stR.equalsignorecase ("D");}
}
}
}