2015-10-18 72 views
0

如何比較字符串與int?我正在製作一款Rock-paper-scissors遊戲,如何將用戶輸入的字符串轉換爲int,以便程序可以檢查誰贏了?如用戶輸入「搖滾」程序註冊爲等?如何比較字符串與整數?

package rpc; 

import java.util.Random; 
import java.util.Scanner; 

public class Main { 
    public static void main(String[] args) { 
     /* Random number generator */ 
     Random random = new Random(); 

     /* Scanner object for input */ 
     Scanner scanner = new Scanner(System.in); 

     /* 
     * Integer variables to hold the user and computer choice. 
     * 0 = Rock 
     * 1 = Paper 
     * 2 = Scissors 
     */ 
     String userChoice; 
     int computerChoice; 

     // Showing prompt and user input 
     System.out.println("Enter move (0 = Rock; 1 = Paper; 2 = Scissors):"); 
     userChoice = scanner.nextLine(); 

     // Checking if userChoice is 0, 1, or 2. 
     if (!userChoice.equalsIgnoreCase("Scissors") && !userChoice.equalsIgnoreCase("Paper") 
      && !userChoice.equalsIgnoreCase("rock")) { 
      System.out.println("Invalid choice. Ending program."); 

      // Exit program 
      Main.main(args); 
     } 

     // Generating random computer choice 
     computerChoice = random.nextInt(3); 

     // Determining the winner 

     // If the choices are equal, it's a tie. 
     if (userChoice == computerChoice) { 
      if (userChoice == 0) { 
       System.out.println("Both players chose rock!"); 
      } else if (userChoice == 1) { 
       System.out.println("Both players chose paper!"); 
      } else { 
       System.out.println("Both players chose scissors!"); 
      } 

      // Exit program 
      System.exit(0); 
     } 

     if (userChoice == 0) {  // User chooses rock 
      if (computerChoice == 1) { 
       System.out.println("You chose rock; Computer chose paper"); 
       System.out.println("Computer wins!"); 
      } else { 
       System.out.println("You chose rock; Computer chose scissors"); 
       System.out.println("You win!"); 
      } 
     } else if (userChoice == 1) { // User chooses paper 
      if (computerChoice == 0) { 
       System.out.println("You chose paper; Computer chose rock"); 
       System.out.println("You win!"); 
      } else { 
       System.out.println("You chose paper; Computer chose scissors"); 
       System.out.println("Computer wins!"); 
      } 
     } else { // User chooses scissors 
      if (computerChoice == 0) { 
       System.out.println("You chose scissors; Computer chose rock"); 
       System.out.println("Computer wins!"); 
      } else { 
       System.out.println("You chose scissors; Computer chose paper"); 
       System.out.println("You win!"); 
      } 
     } 
     scanner.close(); 
    } 
} 
+2

'Main.main(args); //退出程序'請重新考慮。 – Tom

+1

[如何從Java中的標準輸入讀取整數值]可能的重複(http://stackoverflow.com/questions/2506077/how-to-read-integer-value-from-the-standard-input-in- java) – BackSlash

+0

重複[我如何使if語句有多個字符串?](https://stackoverflow.com/questions/33199575/how-do-i-make-an-if-statement-have-multiple-strings )儘管得到了正確答案,但來自同一用戶的同一用戶使用不同的算法 –

回答

0

只需使用pareseInt和字符串轉換爲int

對於前:

if(Integer.parseInt(userChoice) == computerChoice) 

確保輸入是不是null formattable爲int

編輯:改變parese到解析

+2

您是否注意到包含'!userChoice.equalsIgnoreCase(「Scissors」)''等檢查項的'if'?所以_we_已經知道,用戶輸入一個字符串/單詞......他應該如何解析一個int? – Tom

0

Retrieving a random item from ArrayList

這不是你的問題的確切答案(Integer.parseInt(myInt)),但你可以嘗試更像這樣的可讀性,避免使用不必要的整數。並簡化您的代碼

生成您的arrayList,然後選擇隨機「計算機」的選擇。

List<String> posibilities = Arrays.asList("rock","paper","scissors"); 
String computerChoice = possibilites.get(Math.random(3)); 

然後做你的comparaison;)

/* Chose the possibilities */ 
List<String> posibilities = Arrays.asList("rock","paper","scissors"); 

/* Scanner object for input */ 
Scanner scanner = new Scanner(System.in); 

// Showing prompt and user input 
System.out.println("Enter move (0 = Rock; 1 = Paper; 2 = Scissors):"); 
String userChoice = scanner.nextLine(); 
userChoice = possibilities.get(Integer.parseInt(userChoice)); 

// Checking if userChoice is 0, 1, or 2. 
if(!possibilities.contains(userChoice)) { 
    System.out.println("Invalid choice. Ending program."); 

    // Exit program 
    Main.main(args); 
} 

// Generating random computer choice 
String computerChoice = possibilites.get(Math.random(3)); 

// Determining the winner 

// If the choices are equal, it's a tie. 
if(userChoice.equals(computerChoice)) { 
    System.out.println("Both players chose " + userChoice); 

    // Exit program 
    System.exit(0); 
} 

System.out.println("You chose " + userChoice + "; Computer chose " + computerChoice); 
if(userChoice.equals("rock")) { // User chooses rock 
    if(computerChoice.equals("paper")) { 
      System.out.println("Computer wins!"); 
    } else { 
     System.out.println("You win!"); 
    } 
} 
else if(userChoice.equals("paper")) { // User chooses paper 
    if(computerChoice.equals("rock")) { 
     System.out.println("You win!"); 
    } else { 
     System.out.println("Computer wins!"); 
    } 
} else { // User chooses scissors 
    if(computerChoice.equals("Scissors")) { 
     System.out.println("Computer wins!"); 
    } else { 
     System.out.println("You win!"); 
    } 
} 

scanner.close(); 
+0

你讀過這個問題了嗎? – BackSlash

+0

嗯,是的,但代碼將更直接的比較直接字符串,而不是經歷整個過程轉換成int。避免使用不必要的註釋 – Fundhor

+0

OP沒有問如何讓電腦選擇一個隨機數,他問如何將字符串轉換爲int。你的回答並不合理。重新閱讀這個問題。 – BackSlash

-2

使用的Integer.parseInt方法解析字符串爲int,然後比較其

+0

你有沒有注意到包含諸如'!userChoice.equalsIgnoreCase(「Scissors」)''的檢查的'if'?所以_we_已經知道,用戶輸入一個字符串/單詞......他應該如何解析一個int? – Tom

+2

@Tom謝謝你澄清 – idk0namz

2

你可以使用一個枚舉列舉的三種可能選擇:

enum Hand { 

    ROCK, 
    PAPER, 
    SCISSORS; 

    public static Hand from(String input) { 
     for (Hand hand : values()) { 
      if (hand.name().equalsIgnoreCase(input)) { 
       return hand; 
      } 
     } 
     throw new IllegalArgumentException("Invalid choice: " + input); 
    } 

} 

枚舉有一個n內部整數值(對應於它們定義的位置)。例如,ROCK.ordinal()將返回0。

+2

Interessting方法,但我不會依賴'ordinal'並在該枚舉中創建一個'int'字段並自己定義數字。 – Tom

+0

取決於使用方法/方法。在這種情況下,我猜想數字/序數不是必需的 - 枚舉可以相互比較。 – ataulm

+1

是的,他們可以比較,但OP使用隨機生成_number_爲計算機:P。 – Tom