繁体   English   中英

无法回复的声明?

[英]Unreachable statement?

这是我的编码:出于某种原因,在最后,当我试图返回winscounter和losscounter时,它表示无法接受的声明,但不适用于tiecounter。 我无法弄清楚为什么! 如果有人能回答这个问题,我们将不胜感激!

public class RockPaperScissors {

    /**
     * @param args the command line arguments
     */

    static int value;  //computer's choice
    static int choice; //user choice
    static int tiescounter = 0;
    static int winscounter = 0;
    static int losscounter = 0;

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader (new InputStreamReader (System.in));// user input

        int repeat;

        do {
            System.out.println("ROCK PAPER SCISSORS"+
                    "\n===================");
            System.out.println("\n1=Rock" +
                    "\n2=Paper" +
                    "\n3=Scissors" +
                    "\n===========" +
                    "\nChoose:");

            choice = Integer.parseInt(br.readLine());

            if (choice !=1 && choice !=2 && choice !=3) {

                do{
                    System.out.println("\nError. Please choose Rock, Paper or Scissors.");
                    choice = Integer.parseInt(br.readLine());
                }
                while (choice !=1 && choice !=2 && choice !=3);
            }

            System.out.println();

            if (choice == 1){
                System.out.println("You have chosen Rock.");
            }
            else if (choice ==2){
                System.out.println("You have chosen Paper.");
            }
            else if(choice == 3){
                System.out.println("You have chosen Scissors.");
            }

            randomWholeNumber();

            if (value == 1){
                System.out.println("The computer has chosen Rock." );
            }
            else if (value == 2){
                System.out.println("The computer has chosen Paper." );
            }
            else if (value == 3){
                System.out.println("The computer has chosen Scissors." );
            }

            determineOutcome();
            System.out.println("Ties:"+ tiescounter);
            System.out.println("Wins: " + winscounter);
            System.out.println("Losses: " + losscounter);

            repeat = Integer.parseInt(br.readLine());

        }
        while (repeat==1);
    }

    public static int randomWholeNumber(){

        do{
            value=0;//resets random number
            //generates and returns a random number within user's range 
            value = (int) ((Math.random()*3)+1);
        } 
        while((value>3)||(value<1));
        return (value);
    }

    public static int determineOutcome(){

            if (value == choice){
                System.out.println("\nYOU'VE TIED");

                do{
                    tiescounter+=1;
                }
                while (tiescounter != tiescounter);
            }

            else if (value == 1){ //Rock
                if (choice == 2){ //Paper
                    System.out.println("\nYOU'VE WON");               
                    do{
                        winscounter +=1;
                    }
                    while (winscounter != winscounter);
                }
                else if (choice == 3){ //Scissors
                    System.out.println("\nYOU'VE LOST");
                    do{
                        losscounter+=1;
                    }
                    while(losscounter!=losscounter);
                }
            }

            else if (value == 2){ //Paper
                if (choice == 1){ //Rock
                    System.out.println("\nYOU'VE LOST");
                    do{
                        losscounter+=1;
                    }
                    while(losscounter!=losscounter);
                }
                else if (choice == 3){ //Scissors
                    System.out.println("\nYOU'VE WON");

                    do{
                        winscounter +=1;
                    }
                    while (winscounter != winscounter);
                }
            }

            else if (value == 3){ //Scissors
                if (choice == 1){ //Rock
                    System.out.println("\nYOU'VE WON");
                    do{
                        winscounter +=1;
                    }
                    while (winscounter != winscounter);                    
                }
                else if (choice == 2){ //Paper
                    System.out.println("\nYOU'VE LOST");
                    do{
                        losscounter+=1;
                    }
                    while(losscounter!=losscounter);
                }
            }

            return(tiescounter);
            return(winscounter);
            return(losscounter);
    }
}

函数只能返回一个值。 return(tiescounter); 执行函数退出。 如果要返回所有三个值,则必须将它们包装在一个类中。

顺便return(tiescounter); 可以写为return tiescounter; 不需要围绕返回值的括号。 两个语句都会有相同的结果。

您不能同时从一个方法返回3个不同的变量。 在这种情况下, return(tiescounter); 总是执行,然后终止方法。 因此接下来的2行无法访问。

determineOutcome()方法声明为void即public static void determineOutcome() ,并删除其中的所有return语句。 你的程序会运作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM