0

OK、じゃんけんゲームが動きました。終了する 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」と等しいかどうかを確認します。だったらやめます。そうでない場合は、整数に変換し、残りのロジックを使用します。

@MadProgrammer は、このコードをよりフォールト トレラントにする方法について良いアドバイスをしてくれました。

于 2013-02-10T05:33:32.110 に答える
1

次の 2 つのオプションがあります。

  • 数値 (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

次のようにプログラムを変更します。

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 に答える
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 に答える