簡體   English   中英

嗨,我是 Java 的新手,我在試圖找出底部“case SCISSORS:”的問題時遇到了麻煩,有人可以幫我:)

[英]Hi, im new to Java and im having trouble trying to figure out the problem with “case SCISSORS:” at the bottom can somebody help me:)

package 優素福;

導入 java.util.Scanner;

公共 class 迷你 {

private static final int ROCK = 1;
private static final int SCISSORS =2;
private static final int PAPER =3;
private static Scanner input;

public static void main(String[] args) {
    input = new Scanner(System.in);
    int Scissor=0;
    int Rock=0;
    int Paper=0;
    System.out.println("player1: choose(1)-Rock, (2)-Scissors, or(3)-Paper");
    int player1=input.nextInt();
    System.out.println("player2: choose(1)-Rock, (2)-Scissors, or(3)-Paper");
    int player2=input.nextInt();
    if(player1==player2)
    {
        System.out.println("It's a tie,try again");
    }else {
        switch(player1) {
        case ROCK:  
        if(player2==Scissor)
            System.out.println("player1 wins");
        else
            System.out.println("player2 wins");
        break;
        case SCISSORS:
            if(player2 ==Paper)
                System.out.println("player1 wins");
            else
                System.out.println("player2 wins");
            break;
        case PAPER:
            if(player2==Rock)
            System.out.println("player1 wins");
        else
            System.out.println("player2 wins");
        break;
        }
    }
}

}

當我播放程序時,我不知道該怎么做,如描述中所示的錯誤消息顯示。

import java.util.Scanner;

public class mini {

private static final int ROCK = 1;
private static final int SCISSORS =2;
private static final int PAPER =3;
private static Scanner input;

public static void main(String[] args) {
    input = new Scanner(System.in);
    int Scissor=0;
    int Rock=0;
    int Paper=0;
    System.out.println("player1: choose(1)-Rock, (2)-Scissors, or(3)-Paper");
    int player1=input.nextInt();
    System.out.println("player2: choose(1)-Rock, (2)-Scissors, or(3)-Paper");
    int player2=input.nextInt();
    if(player1==player2)
    {
        System.out.println("It's a tie,try again");
    }else {

        switch(player1) {
        case ROCK:  
        if(player2==Scissor)
            System.out.println("player1 wins");
        else
            System.out.println("player2 wins");
        break;
        case SCISSORS:
            if(player2 ==Paper)
                System.out.println("player1 wins");
            else
                System.out.println("player2 wins");
            break;
        case PAPER:
            if(player2==Rock)
            System.out.println("player1 wins");
        else
            System.out.println("player2 wins");
        break;
        }
    }
}
}

問題出在 switch case 語句中的 if else 語句中,您在其中將 integer 變量與字符串變量進行比較。 我希望它會有所幫助:-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM