[英]How to change my code to make it more efficient?
在這段代碼中是在計算機和用戶之間玩石頭剪刀布。 我的代碼運行良好,但是,我正在嘗試一種更好的方法來詢問用戶是否想再次玩。 如果是,那么它將再次啟動程序,如果不是,那么它將停止。 我的“是”似乎有效,但沒有將停止,而不是 go 一直到。 有關如何執行此操作的任何建議或提示? 我將嘗試合並一個不同的 while 循環,但沒有工作。 do循環對這種情況有好處嗎? 謝謝!
//import scanner
import java.util.Scanner;
import java.util.*;
//declare variables and main methods
class Rock {
Scanner scan = new Scanner(System.in);
Random generator = new Random();
String response, name;
char choice;
int rounds, computerChoice, userScore, computerScore;
boolean playIntro = true;
boolean playGame = true;
//this method will run the entire progrma
public void playRPS(){
//while loop for beginning of game
while(playIntro){
System.out.println("This is a game of Rock Paper Scissors!");
System.out.println("Please enter your name: ");
name = scan.nextLine();
//while loop for the actual part of the game
while(playGame){
System.out.println("Type R (Rock), P (Paper), or S (Scissors): ");
choice = scan.nextLine().charAt(0);
computerChoice = generator.nextInt(3)+1;
//using switch and case for each choice
switch (choice){
//case for Rock
case 'R':
if(computerChoice==1){
System.out.println("Tie between you and the computer! Go again.");
break;
}
else{
if(computerChoice==2){
System.out.println("The computer beat you this round");
computerScore++;
break;
}
else{
System.out.println("You won this round");
userScore++;
break;
}
}
//case for Paper
case 'P':
if(computerChoice==2){
System.out.println("Tie between you and the computer! Go again.");
break;
}
else{
if(computerChoice==3){
System.out.println("The computer beat you this round");
computerScore++;
break;
}
else{
System.out.println("You won this round");
userScore++;
break;
}
}
//case for Scissors
case 'S':
if(computerChoice==3){
System.out.println("Tie between you and the computer! Go again.");
break;
}
else{
if(computerChoice==1){
System.out.println("The computer beat you this round");
computerScore++;
break;
}
else{
System.out.println("You won this round");
userScore++;
break;
}
}
}
System.out.println("You have "+userScore+" points and the computer has "+computerScore+" points");
if (userScore==5){
System.out.println("\nOut of 5 rounds, You beat the computer!");
playGame = false;
}
else if (computerScore==5){
System.out.println("\nOut of 5 rounds, The computer beat you.");
playGame = false;
}
}
askUser();
}
}
public void askUser(){
System.out.println("\nDo you want to play this Rock Paper Scissors again? Type yes: ");
response = scan.nextLine();
if (response.equalsIgnoreCase("yes")){
playGame = true;
userScore=0;
computerScore=0;
}
else{
playGame = false;
scan.nextLine();
}
}
public static void main() {
Rock prog = new Rock();
prog.playRPS();
}
}
我不會說這必然更有效甚至更好,但它更簡潔一些。 它的主要元素是。
當然,重要的是您的代碼可以正常工作。
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
import java.util.function.Function;
public class RockPaperScissors {
final static int PAPER = 1;
final static int ROCK = 2;
final static int SCISSORS = 3;
// the next two declarations allows the previous three to take on any values.
private Set<Integer> allowedMoves = Set.of(PAPER, ROCK, SCISSORS);
private List<String> moves = List.of("PAPER", "ROCK", "SCISSORS");
private String ROCK_WINS_MSG = "Rock crushes scissors";
private String SCISSORS_WINS_MSG = "Scissors cuts paper";
private String PAPER_WINS_MSG = "Paper covers rock";
private String COMPUTER_WINS = ", computer wins!";
private String YOU_WIN = ", you win!";
private Function<Integer, String> CHECK_PAPER =
(c) -> c == PAPER ? "It's a tie!" :
c == ROCK ? PAPER_WINS_MSG + YOU_WIN :
SCISSORS_WINS_MSG + COMPUTER_WINS;
private Function<Integer, String> CHECK_ROCK =
(c) -> c == ROCK ? "It's a tie!" :
c == SCISSORS ? ROCK_WINS_MSG + YOU_WIN :
PAPER_WINS_MSG + COMPUTER_WINS;
private Function<Integer, String> CHECK_SCISSORS =
(c) -> c == SCISSORS ? "It's a tie!" :
c == PAPER ? SCISSORS_WINS_MSG + YOU_WIN :
ROCK_WINS_MSG + COMPUTER_WINS;
private Map<Integer, Function<Integer, String>> evalUser =
Map.of(PAPER, CHECK_PAPER, ROCK, CHECK_ROCK, SCISSORS,
CHECK_SCISSORS);
public static void main(String[] args) {
new RockPaperScissors().play();
}
public void play() {
Random r = new Random();
Scanner scan = new Scanner(System.in);
while (true) {
System.out.printf("%n%d : %s%n%d : %s%n%d : %s%n%s%n",
PAPER, "PAPER", ROCK, "ROCK", SCISSORS,
"SCISSORS", "Any other integer to quit.");
System.out.print("Your move! ");
String str = scan.nextLine();
int move;
try {
move = Integer.parseInt(str);
if (!allowedMoves.contains(move)) {
break;
}
} catch (IllegalArgumentException ie) {
System.out.println("Only integers permitted.");
continue;
}
System.out.println("\nYou chose " + moves.get(move - 1));
int cmove = r.nextInt(3);
System.out.println(
"The computer chooses " + moves.get(cmove));
System.out.println(evalUser.get(move).apply(cmove + 1));
}
System.out.println("\nGame over!");
}
}
一旦建議您的代碼將是查看開關案例。 每種情況的代碼實際上是相同的。 我會尋找相似之處並將評估作為一種方法(我在代碼中並沒有真正做到這一點)。 然后在每種情況下,使用適當的 arguments 調用該方法。 一個這樣的論點將是基於上下文的“計算機”或“你”。
沒有任何東西可以設置playIntro
假,因此外循環永遠不會終止。
當askUser()
將playGame
設置為 false 時,內部循環終止,並且您進入外部循環,該外部循環繼續循環。
我根本看不出外循環存在的任何理由。 您只想打印介紹並詢問玩家的姓名一次。
這與其說是“效率”問題,不如說是正確性問題。
順便說一句,最好讓askUser()
返回一個真/假值,而不是設置一個成員變量。 然后你可以直接在'while'表達式中使用它。
playRPS()
的整體結構如下所示:
public void playRPS() {
... print intro, ask name ...
do {
... play one game ...
} while (askUser());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.