2013-02-10 49 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"); 
    }