0

好的,我的石头、纸、剪刀游戏正在运行。除了 Q 之外的其他所有操作都有效。由于我的扫描仪只接受整数,我如何将“Q”字符串传递给它。我假设我只是if(string.equals("Q") {break;}在 while 循环中添加一个简单的,我会很高兴去。让我知道你的想法。

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();


        }
    }
}
4

5 回答 5

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 对如何使此代码更具容错性提出了一些很好的建议,我将遵循这些建议,但我发布了此代码,因为它直接回答了您的问题。

于 2013-02-10T05:33:32.110 回答
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
    }
    
于 2013-02-10T05:35:35.730 回答
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);
    }
}
于 2013-02-10T05:30:40.510 回答
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);
   }
于 2013-02-10T05:45:30.370 回答
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");
    }
于 2013-02-10T06:43:49.037 回答