簡體   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