java - 无法访问的声明?

标签 java methods return

这是我的编码: 出于某种原因,最后,当我尝试返回winscounter和losscounter时,它显示无法访问的语句,但不适用于tiescounter。我不明白为什么!如果有人能回答这个问题,将不胜感激!

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

最佳答案

您不能同时从一个方法返回 3 个不同的变量。在这种情况下,return(tiescounter); 始终执行,并立即终止该方法。因此接下来的两行变得无法访问。

defineOutcome() 方法声明为 void,即 public static void recognizeOutcome(),并删除其中的所有 return 语句。您的程序将会运行。

关于java - 无法访问的声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16139376/

相关文章:

java - 从ArrayList中删除学生ID,并显示添加到ArrayList中的所有类(class)

java - 如何从存储过程获取多个结果集到spring boot端

java - 安卓/Java : Is there a way to store a method?

oracle - 将返回从选择查询中检索到的数据的函数 - Oracle

c# - 包含 foreach 语句的方法只有一个返回值

java - Getter 方法返回两个不同的对象identityHashCodes

java - 从 servlet 读取资源文件

java - EntityManager 合并插入新实体

java - while循环Int return方法内部完全死了

java - 调整返回 true/false 的方法