2013-11-04 163 views
1

我一直在試圖修復這個程序一段時間。基本上,這是一款Rock-Paper-Scissors類型的遊戲,除了輸入驗證之外,所有功能都可以使用。任何幫助,將不勝感激。在Java中輸入驗證字符串

這是我的代碼:

import java.util.Scanner; 
public class RockPaperScissors 
{ 
    public static void main(String[] args) 
    { 
     Scanner scan = new Scanner (System.in); 
     System.out.println("Player 1: Choose rock, paper or scissors:"); 
     String player1 = scan.next() .toLowerCase(); 
     //Player 1 Input Validation 
     if ((player1 != ("rock")) 
     || (player1 != ("paper")) 
     || (player1 != ("scissors"))) 
     { 
      System.out.println("Thats not right, choose rock, paper or scissors"); 
     } 
     //Send Back to input 
     System.out.println("Player 2: Choose rock, paper or scissors:"); 
     String player2 = scan.next() .toLowerCase(); 
     //Player 2 Input Validation 
     if ((player2 != ("rock")) 
     || (player2 != ("paper")) 
     || (player2 != ("scissors"))) 
     { 
      System.out.println("Please choose rock, paper or scissors"); 
     } 
     System.out.println("Player 1 chose " + player1); 
     System.out.println("Player 2 chose " + player2); 


    //For Player 1 to win 
    if((player1.equals("rock"))&&(player2.equals("scissors")) 
    ||(player1.equals("scissors"))&&(player2.equals("paper")) 
    ||(player1.equals("paper"))&&(player2.equals("rock"))) 
    { 
     System.out.println ("Player 1 Wins!"); 
    } 

    //For a draw 
    if (player1.equals(player2)) 
    { 
     System.out.println ("Its a Draw!"); 
    } 

    //For Player 2 to win 
    if ((player2.equals("rock"))&&(player1.equals("scissors")) 
    ||(player2.equals("scissors"))&&(player1.equals("paper")) 
    ||(player2.equals("paper"))&&(player1.equals("rock"))) 
    { 
     System.out.println("Player 2 wins!"); 
    } 

} 

這是我得到的輸出(岩石和紙,是用戶輸入):

Player 1: Choose rock, paper or scissors: 
Rock 
Thats not right, choose rock, paper or scissors 
Player 2: Choose rock, paper or scissors: 
Paper 
Please choose rock, paper or scissors 
Player 1 chose rock 
Player 2 chose paper 
Player 2 wins! 
+0

此外,使用常量,而不是到處複製值;它不太容易輸入錯誤。 – chrylis

回答

2

而不是使用==!=時要使用equals()equalsIgnoreCase()方法來比較字符串。

並且當使用Scanner時,使用nextLine()方法而不是next()

使用常量而不是變量字符串。

當玩家的輸入不正確,並且您希望玩家再次輸入輸入時,最好使用循環結構,如while,當用戶輸入有效的輸入時,跳出循環或使用在函數內循環並返回一個值。

編輯:回答你的問題上意見試試這個

class Player { 
    String choice; 
} 

class Game { 

static final ROCK = "ROCK"; 
static final PAPER = "PAPER"; 
static final SCISSORS = "SCISSORS"; 

    Scanner scanner = new Scanner(); 
    Player[] players = new Player[2]; //assuming you only want 2 players Use ArrayList for unspecified number of users and use a separate method to initialize it. 

public Game { 
     for(int i =0 ; i<2 ; i++) { 
     players[i]= new Player(); 
     } 
} 

private String getPlayerChoice() { 
    System.out.println("Please enter your choice"); 
    System.out.println("1.Rock\n2.Paper\n3.Scissors") 
    return scanner.nextLine(); 
} 

void getInputAndValidate(Player p) { 
    p.choice = getPlayerChoice(); 
    while(true) { 
    if(p.choice.equalsIgnorecase(ROCK) || 
     p.choice.equalsIgnorecase(PAPER) || 
     p.choice.equalsIgnorecase(SCISSORS)) { 
     break; 
    } 
    else { 
     System.out.println("Please enter a valid input"); 
     p.choice = getPlayerChoice(); \\ use the scanner as an instance variable. 
    } 
    } 
} 

void getInput() { 
    for(int i = 0; i<2;i++) { 
    System.out.println("Player " + (i+1)); 
    getInputAndValidate(players[i]); 
    } 
} 

void compute(){ 
    // implement your game logic here 
} 
public static void main(String[] args) { 
    Game g = new Game(); 
    g.getInput(); 
    g.compute(); 
} 

} 
+0

我使用了這個,當我跑步時我得到一個無限循環?是因爲我只用了部分? –

+0

如果你不使用break語句'while(true)'總是保持爲真,循環將進入無限執行。當輸入值爲有效值時,break語句將控制流程從循環中分離出來。 –

+0

應該在while循環之後...整個while循環僅用於驗證...正如我告訴你把它放在一個方法中,並通過傳遞第一個玩家的輸入來調用該方法,然後提示第二個玩家給出輸入。再次調用用戶iunput驗證的方法。然後執行遊戲邏輯。 –

1
if ((player1 != ("rock")) 
    || (player1 != ("paper")) 
    || (player1 != ("scissors"))) 

更改所有=到!等於或等於IgnoreCase()以避免案例混淆

if ((!player1.equalsIgnoreCase("rock")) 
    || (!player1.equalIgnoreCase("paper")) 
    || (!player1.equalsIgnoreCase("scissors")))