如何将字符串解析为整数

时间:2013-02-10 05:28:35

标签: java

好的,我的摇滚,纸张,剪刀游戏都在工作。除了要退出的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();


        }
    }
}

5 个答案:

答案 0 :(得分: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”。如果是的话,我就退出了。如果不是,我将其转换为整数并使用其余的逻辑。

@MadProgrammer对如何使这段代码更具容错性有一些很好的建议我会遵循,但我发布了这段代码,因为它直接回答了你的问题。

答案 1 :(得分: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
    }
    

答案 2 :(得分: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);
    }
}

答案 3 :(得分: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);
   }

答案 4 :(得分: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");
    }