2013-02-10 36 views
0

好吧我有我的搖滾,紙,剪刀遊戲工作。除Q之外的所有人都退出了作品。由於我的掃描儀只採用整數,我怎樣才能將「Q」字符串傳遞給它。我會假設我只是在while循環中添加一個簡單的if(string.equals("Q") {break;},我很樂意去。讓我知道你的想法。如何解析字符串作爲整數

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

public class RockPaperScissors 
{ 

    /** 
    * (Insert a brief description that describes the purpose of this method) 
    * 
    * @param args 
    */ 
    public static void main(String[] args) 
    { 
     int compint; 
     String usermove = ""; 
     String compmove = ""; 
     String winner = ""; 
     int count = 0; 
     int input=0; 


     Scanner in = new Scanner(System.in); 
     Random gen = new Random(); 

     System.out.print("Enter Rock(1), Paper(2), Scissors(3) {Q to quit]: "); 
     input=in.nextInt(); 
     while (count < 3) 
     { 
      compint = gen.nextInt(3) + 1; 

      if (input == 1) 
      { 
       usermove = "Rock"; 
      } 
      else if (input == 2) 
      { 
       usermove = "Paper"; 
      } 
      else if (input == 3) 
      { 
       usermove = "Scissors"; 
      } 

      if (compint == 1) 
      { 
       compmove = "Rock"; 
      } 
      else if (compint == 2) 
      { 
       compmove = "Paper"; 
      } 
      else if (compint == 3) 
      { 
       compmove = "Scissors"; 
      } 

      if (compint == input) 
      { 
       winner = "TIE"; 
      } 
      else if (compint == 1 && input == 3) 
      { 
       winner = "COMPUTER"; 
      } 
      else if (compint == 2 && input == 1) 
      { 
       winner = "COMPUTER"; 
      } 
      else if (compint == 3 && input == 2) 
      { 
       winner = "COMPUTER"; 
      } 
      else 
      { 
       winner = "USER"; 
      } 

      System.out.print("Computer: " + compmove + " | "); 
      System.out.print("You: " + usermove + " | "); 
      System.out.println("Winner: " + winner); 
      System.out.println(); 
      count++; 
      System.out.print("Enter Rock(1), Paper(2), Scissors(3) {Q to quit]: "); 
      input = in.nextInt(); 


     } 
    } 
} 
+2

使用'input = in.next()'(將輸入改爲'String'),檢查input.equalsIgnoreCase(「Q」)是否與'int'一致,的Integer.parseInt(輸入)'。這會拋出一個'NumberFormatException',你應該捕獲並提醒用戶只輸入有效的值 – MadProgrammer 2013-02-10 05:32:12

回答

1

此代碼爲我工作:

package com.sandbox; 

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

public class Sandbox { 

    /** 
    * (Insert a brief description that describes the purpose of this method) 
    * 
    * @param args 
    */ 
    public static void main(String[] args) { 
     int compint; 
     String usermove = ""; 
     String compmove = ""; 
     String winner = ""; 
     int count = 0; 
     String rawInput = null; 
     int input = 0; 


     Scanner in = new Scanner(System.in); 
     Random gen = new Random(); 

     System.out.print("Enter Rock(1), Paper(2), Scissors(3) {Q to quit]: "); 
     rawInput = in.next(); 
     if ("Q".equals(rawInput)) { 
      return;  //exit main 
     } 
     input = Integer.parseInt(rawInput); 

     while (count < 3) { 
      compint = gen.nextInt(3) + 1; 

      if (input == 1) { 
       usermove = "Rock"; 
      } else if (input == 2) { 
       usermove = "Paper"; 
      } else if (input == 3) { 
       usermove = "Scissors"; 
      } 

      if (compint == 1) { 
       compmove = "Rock"; 
      } else if (compint == 2) { 
       compmove = "Paper"; 
      } else if (compint == 3) { 
       compmove = "Scissors"; 
      } 

      if (compint == input) { 
       winner = "TIE"; 
      } else if (compint == 1 && input == 3) { 
       winner = "COMPUTER"; 
      } else if (compint == 2 && input == 1) { 
       winner = "COMPUTER"; 
      } else if (compint == 3 && input == 2) { 
       winner = "COMPUTER"; 
      } else { 
       winner = "USER"; 
      } 

      System.out.print("Computer: " + compmove + " | "); 
      System.out.print("You: " + usermove + " | "); 
      System.out.println("Winner: " + winner); 
      System.out.println(); 
      count++; 
      System.out.print("Enter Rock(1), Paper(2), Scissors(3) {Q to quit]: "); 
      input = in.nextInt(); 


     } 
    } 
} 

我在做什麼,走的是輸入的字符串。我將它保存到名爲rawInput的變量中。然後我檢查這是否等於「Q」。如果是這樣,我放棄了。如果不是,我將它轉換爲Integer並使用其餘的邏輯。

@MadProgrammer對如何讓這段代碼更容錯,我會遵循但是我發佈了這個代碼,因爲它直接回答你的問題。

+1

不要忘記在while循環中將最後一個'System.out'語句更改爲'rawInput = in.next(); (「Q」.equals(rawInput)){ return; // exit main } input = Integer.parseInt(rawInput);' – SkyVar 2013-02-10 05:44:15

0

Integer.parseInt(myString)

只要插上的myString

class JTest 
{ 
    public static void main(String[] args) 
    { 
     String a = "5"; 
     int b = Integer.parseInt(a); 
     System.out.println(b); 
    } 
} 
1

你有兩個選擇:

  • 要麼使用數值退出(0或1),或
  • 轉換程序同時接受字符串和數字。你這樣做的方式是通過Integer.parseInt()

    // This assumes that input is of type String instead 
    int option = 0; 
    System.out.print("Enter Rock(1), Paper(2), Scissors(3) {Q to quit]: "); 
    input=in.nextLine(); 
    try { 
        option = Integer.parseInt(input); 
    } catch (NumberFormatException nfe) { 
        // wasn't a number, so it was either bogus input or the quit option. 
        if("Q".equalsIgnoreCase(input)) { 
         System.out.println("Quitting."); 
         System.exit(0); 
        } else { 
         System.out.println("Bogus input."); 
        } 
    } 
    while (count < 3 && option != 0) { 
        // logic 
    } 
    
+0

再次想到,在用戶執行不正確時向用戶提供反饋也是可以的,但是,在用戶輸入有效輸入之前,我們需要在try/catch塊上進行循環,並添加'trim()'以清理空間。逐行閱讀有一個好處,可以防止奇怪的東西,例如用戶連續輸入2個數字。 – nhahtdh 2013-02-10 05:39:40

+0

@nhahtdh:是的。輸入一個空格是假輸入。這不是「搖滾」,「紙」,「剪刀」或「退出」。它還會是什麼?是的,我確實同意爲此需要一個eval循環 - 它主要是用字符串替代的。使用數字值作爲「退出」選項會更直接,因爲這是設計使用的。 – Makoto 2013-02-10 05:41:34

+0

也可以考慮空行,因爲用戶不小心輸入並忽略它。在我之前的評論中,我不應該認爲這樣做的內涵不好。我只想用'next()'指出一個替代設計。 – nhahtdh 2013-02-10 05:47:51

0

提供的用戶犯規比0-9其他和 「q」 或 「Q」 輸入那麼它應該工作:

Scanner sc = new Scanner(System.in); 
    String a = sc.next(); 
    if(a.equalsIgnoreCase("q")){ 
     System.out.println("quit game"); 
    }  
    else{ 
     int input = Integer.parseInt(a); 
    } 
0

更改您的程序如下:

public static void main(String... args) { 

     Scanner in = new Scanner(System.in); 
     int compint; 
     String usermove = ""; 
     String compmove = ""; 
     String winner = ""; 
     int count = 0; 
     int input = 0; 
     Random gen = new Random(); 
     Set<Boolean> set = new HashSet<Boolean>(); 
     boolean contains = false; 
     while (count < 3) { 
      System.out.print("Enter Rock(1), Paper(2), Scissors(3) {Q to quit]: "); 
      String nextLine=in.next().trim(); 
      do { 
       for (int index = 0; index < nextLine.length(); index++) { 
        set.add(Character.isLetter(nextLine.charAt(index))); 
       } 
       contains = set.contains(true); 
       if(contains) { 
       if (nextLine.equals("Q")) { 
        System.out.println("program exited"); 
        return; 
       } else { 
        System.out .print("Re-enter Rock(1), Paper(2), Scissors(3) {Q to quit]: "); 
        nextLine=in.next(); 
       } 
       } else { 
        input=Integer.parseInt(nextLine); 
       } 

       set.remove(true); 
      } while (contains); 

      compint = gen.nextInt(3) + 1; 

      if (input == 1) { 
       usermove = "Rock"; 
      } else if (input == 2) { 
       usermove = "Paper"; 
      } else if (input == 3) { 
       usermove = "Scissors"; 
      } 

      if (compint == 1) { 
       compmove = "Rock"; 
      } else if (compint == 2) { 
       compmove = "Paper"; 
      } else if (compint == 3) { 
       compmove = "Scissors"; 
      } 

      if (compint == input) { 
       winner = "TIE"; 
      } else if (compint == 1 && input == 3) { 
       winner = "COMPUTER"; 
      } else if (compint == 2 && input == 1) { 
       winner = "COMPUTER"; 
      } else if (compint == 3 && input == 2) { 
       winner = "COMPUTER"; 
      } else { 
       winner = "USER"; 
      } 

      System.out.print("Computer: " + compmove + " | "); 
      System.out.print("You: " + usermove + " | "); 
      System.out.println("Winner: " + winner); 
      System.out.println(); 
      count++; 
     } 
     System.out.println("program's end"); 
    }