在Java中使用While循环控制答案 [英] Controlling Answer With While Loop in Java
本文介绍了在Java中使用While循环控制答案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我编写了一个钓鱼游戏模拟器.我想控制答案:如果答案不等于"Y","y","N",则答案为"n".或"n",再问一遍:想钓鱼吗?直到用户按下正确的键.我不知道该如何编码该算法,我不应该将该算法放在哪一行.我的代码在这里:
I coded a fishing game simulator. I want to control the answer: If answer is not equal to "Y","y","N" or "n", Ask again : Would Like to fish? until until the user presses the correct key. I do not know how to code this algorithm and which line should I put that algorithm. My code is here:
//--------------------//
// --------------------//
import java.util.Random;导入java.util.Scanner;
import java.util.Random; import java.util.Scanner;
公共类钓鱼游戏{
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!
这篇关于在Java中使用While循环控制答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文