在 Java 中使用 While 循环控制答案
Controlling Answer With While Loop in Java
我编写了一个钓鱼游戏模拟器。我想控制答案:如果答案不等于“Y”、“y”、“N”或“n”,再问:Would Like to fish?直到用户按下正确的键。我不知道如何编写此算法的代码以及我应该将该算法放在哪一行。我的代码在这里:
// ------------------ //
进口java.util.Random;
导入 java.util.Scanner;
public class钓鱼游戏{
public static void main(String[] args) {
Random dice = new Random();
Scanner scanner = new Scanner(System.in);
int randomGuess;
String choice = "Y";
int score = 0;
int points = 0;
int counter = 0;
System.out.println("Let's go fishing!!!");
System.out.println();
System.out.println("Would you like to fish?");
System.out.println("Type Y or y for yes");
System.out.println(" Type N or n for quit");
while (choice.equalsIgnoreCase("Y")) {
randomGuess = dice.nextInt(6) + 1;
if (randomGuess == 1) {
System.out.println("You caught an old shoe.");
points += 1; // If Random Guess is 1 player takes that message and wins 1 point
}
if (randomGuess == 2) {
System.out.println("You caught a huge fish!");
points += 100; // If Random Guess is 2,player takes that message and wins 100 points
}
if (randomGuess == 3) {
System.out.println("You caught a leaf ");
points += 2; // If Random Guess is 3,player takes that message and wins 2 points
}
if (randomGuess == 4) {
System.out.println("You caught a small fish ");
points += 50; // If Random Guess is 2,player takes that massage and wins 50 points
}
if (randomGuess == 5) {
System.out.println("You caught a rock ");
points += 3; // If Random Guess is 2,player takes that massage and wins 3 points
}
if (randomGuess == 6) {
System.out.println("You caught a garbage ");
points += 0; // If Random Guess is 2,player takes that massage and can not win any point
}
System.out.println("Want to try again? Y/N");
choice = scanner.nextLine();
counter++; // This structure counts repeat of game
}
if ((choice.equalsIgnoreCase("N"))) {
System.out.println("Game Over");
System.out.println();
System.out.println("Your final score is: " + (points));
System.out.println("Your game repeat is:" + counter);
double avarege = (double) points / counter; // This lane is for counting the average of points
System.out.printf("Your Average Point Is: %6.2f\n" , avarege);
if (avarege >= 20) {
System.out.println("GREAT JOB");
} else if (avarege >= 10) {
System.out.println("That is some fine fishing");
} else {
System.out.println("Try again in future!");
}
System.out.println("Thanks for playing!");
}
}
}
解法:
import java.util.Random;
import java.util.Scanner;
public class fishinggame {
public static void main(String[] args) {
Random dice = new Random();
Scanner scanner = new Scanner(System.in);
int randomGuess;
String choice = "Y";
int score = 0;
int points = 0;
int counter = 0;
System.out.println("Let's go fishing!!!");
System.out.println();
System.out.println("Would you like to fish?");
System.out.println("Type Y or y for yes");
System.out.println(" Type N or n for quit");
choice = scanner.next();
while (!choice.equalsIgnoreCase("Y") && !choice.equalsIgnoreCase("n")) {
System.out.println("Would you like to fish?");
System.out.println("Type Y or y for yes");
System.out.println(" Type N or n for quit");
choice = scanner.next();
}
while (choice.equalsIgnoreCase("Y")) {
randomGuess = dice.nextInt(6) + 1;
if (randomGuess == 1) {
System.out.println("You caught an old shoe.");
points += 1; // If Random Guess is 1 player takes that message and wins 1 point
}
if (randomGuess == 2) {
System.out.println("You caught a huge fish!");
points += 100; // If Random Guess is 2,player takes that message and wins 100 points
}
if (randomGuess == 3) {
System.out.println("You caught a leaf ");
points += 2; // If Random Guess is 3,player takes that message and wins 2 points
}
if (randomGuess == 4) {
System.out.println("You caught a small fish ");
points += 50; // If Random Guess is 2,player takes that massage and wins 50 points
}
if (randomGuess == 5) {
System.out.println("You caught a rock ");
points += 3; // If Random Guess is 2,player takes that massage and wins 3 points
}
if (randomGuess == 6) {
System.out.println("You caught a garbage ");
points += 0; // If Random Guess is 2,player takes that massage and can not win any point
}
System.out.println("Want to try again? Y/N");
choice = scanner.next();
counter++; // This structure counts repeat of game
}
if ((choice.equalsIgnoreCase("N"))) {
System.out.println("Game Over");
System.out.println();
System.out.println("Your final score is: " + (points));
System.out.println("Your game repeat is:" + counter);
double avarege = (double) points / counter; // This lane is for counting the average of points
System.out.printf("Your Average Point Is: %6.2f\n" , avarege);
if (avarege >= 20) {
System.out.println("GREAT JOB");
} else if (avarege >= 10) {
System.out.println("That is some fine fishing");
} else {
System.out.println("Try again in future!");
}
System.out.println("Thanks for playing!");
}
}
}
结果:
Let's go fishing!!!
Would you like to fish?
Type Y or y for yes
Type N or n for quit
y
You caught a leaf
Want to try again? Y/N
y
You caught a rock
Want to try again? Y/N
y
You caught a small fish
Want to try again? Y/N
y
You caught a small fish
Want to try again? Y/N
n
Game Over
Your final score is: 105
Your game repeat is:4
Your Average Point Is: 26,25
GREAT JOB
Thanks for playing!
我编写了一个钓鱼游戏模拟器。我想控制答案:如果答案不等于“Y”、“y”、“N”或“n”,再问:Would Like to fish?直到用户按下正确的键。我不知道如何编写此算法的代码以及我应该将该算法放在哪一行。我的代码在这里:
// ------------------ //
进口java.util.Random; 导入 java.util.Scanner;
public class钓鱼游戏{
public static void main(String[] args) {
Random dice = new Random();
Scanner scanner = new Scanner(System.in);
int randomGuess;
String choice = "Y";
int score = 0;
int points = 0;
int counter = 0;
System.out.println("Let's go fishing!!!");
System.out.println();
System.out.println("Would you like to fish?");
System.out.println("Type Y or y for yes");
System.out.println(" Type N or n for quit");
while (choice.equalsIgnoreCase("Y")) {
randomGuess = dice.nextInt(6) + 1;
if (randomGuess == 1) {
System.out.println("You caught an old shoe.");
points += 1; // If Random Guess is 1 player takes that message and wins 1 point
}
if (randomGuess == 2) {
System.out.println("You caught a huge fish!");
points += 100; // If Random Guess is 2,player takes that message and wins 100 points
}
if (randomGuess == 3) {
System.out.println("You caught a leaf ");
points += 2; // If Random Guess is 3,player takes that message and wins 2 points
}
if (randomGuess == 4) {
System.out.println("You caught a small fish ");
points += 50; // If Random Guess is 2,player takes that massage and wins 50 points
}
if (randomGuess == 5) {
System.out.println("You caught a rock ");
points += 3; // If Random Guess is 2,player takes that massage and wins 3 points
}
if (randomGuess == 6) {
System.out.println("You caught a garbage ");
points += 0; // If Random Guess is 2,player takes that massage and can not win any point
}
System.out.println("Want to try again? Y/N");
choice = scanner.nextLine();
counter++; // This structure counts repeat of game
}
if ((choice.equalsIgnoreCase("N"))) {
System.out.println("Game Over");
System.out.println();
System.out.println("Your final score is: " + (points));
System.out.println("Your game repeat is:" + counter);
double avarege = (double) points / counter; // This lane is for counting the average of points
System.out.printf("Your Average Point Is: %6.2f\n" , avarege);
if (avarege >= 20) {
System.out.println("GREAT JOB");
} else if (avarege >= 10) {
System.out.println("That is some fine fishing");
} else {
System.out.println("Try again in future!");
}
System.out.println("Thanks for playing!");
}
}
}
解法:
import java.util.Random;
import java.util.Scanner;
public class fishinggame {
public static void main(String[] args) {
Random dice = new Random();
Scanner scanner = new Scanner(System.in);
int randomGuess;
String choice = "Y";
int score = 0;
int points = 0;
int counter = 0;
System.out.println("Let's go fishing!!!");
System.out.println();
System.out.println("Would you like to fish?");
System.out.println("Type Y or y for yes");
System.out.println(" Type N or n for quit");
choice = scanner.next();
while (!choice.equalsIgnoreCase("Y") && !choice.equalsIgnoreCase("n")) {
System.out.println("Would you like to fish?");
System.out.println("Type Y or y for yes");
System.out.println(" Type N or n for quit");
choice = scanner.next();
}
while (choice.equalsIgnoreCase("Y")) {
randomGuess = dice.nextInt(6) + 1;
if (randomGuess == 1) {
System.out.println("You caught an old shoe.");
points += 1; // If Random Guess is 1 player takes that message and wins 1 point
}
if (randomGuess == 2) {
System.out.println("You caught a huge fish!");
points += 100; // If Random Guess is 2,player takes that message and wins 100 points
}
if (randomGuess == 3) {
System.out.println("You caught a leaf ");
points += 2; // If Random Guess is 3,player takes that message and wins 2 points
}
if (randomGuess == 4) {
System.out.println("You caught a small fish ");
points += 50; // If Random Guess is 2,player takes that massage and wins 50 points
}
if (randomGuess == 5) {
System.out.println("You caught a rock ");
points += 3; // If Random Guess is 2,player takes that massage and wins 3 points
}
if (randomGuess == 6) {
System.out.println("You caught a garbage ");
points += 0; // If Random Guess is 2,player takes that massage and can not win any point
}
System.out.println("Want to try again? Y/N");
choice = scanner.next();
counter++; // This structure counts repeat of game
}
if ((choice.equalsIgnoreCase("N"))) {
System.out.println("Game Over");
System.out.println();
System.out.println("Your final score is: " + (points));
System.out.println("Your game repeat is:" + counter);
double avarege = (double) points / counter; // This lane is for counting the average of points
System.out.printf("Your Average Point Is: %6.2f\n" , avarege);
if (avarege >= 20) {
System.out.println("GREAT JOB");
} else if (avarege >= 10) {
System.out.println("That is some fine fishing");
} else {
System.out.println("Try again in future!");
}
System.out.println("Thanks for playing!");
}
}
}
结果:
Let's go fishing!!!
Would you like to fish?
Type Y or y for yes
Type N or n for quit
y
You caught a leaf
Want to try again? Y/N
y
You caught a rock
Want to try again? Y/N
y
You caught a small fish
Want to try again? Y/N
y
You caught a small fish
Want to try again? Y/N
n
Game Over
Your final score is: 105
Your game repeat is:4
Your Average Point Is: 26,25
GREAT JOB
Thanks for playing!