Unreachable statement?(无法到达的声明?)
问题描述
这是我的编码:出于某种原因,最后,当我试图返回 winscounter 和 losscounter 时,它说的是无法访问的语句,但不是针对 tiescounter 的.我不知道为什么!如果有人能回答这个问题,将不胜感激!
Here is my coding: For some reason, at the very end, when I'm trying to return winscounter and losscounter, it says unreachable statement but not for the tiescounter. I can't figure out why! If anyone can answer this, it would be greatly appreciated!!
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"+
                    "
===================");
            System.out.println("
1=Rock" +
                    "
2=Paper" +
                    "
3=Scissors" +
                    "
===========" +
                    "
Choose:");
            choice = Integer.parseInt(br.readLine());
            if (choice !=1 && choice !=2 && choice !=3) {
                do{
                    System.out.println("
Error. 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("
YOU'VE TIED");
                do{
                    tiescounter+=1;
                }
                while (tiescounter != tiescounter);
            }
            else if (value == 1){ //Rock
                if (choice == 2){ //Paper
                    System.out.println("
YOU'VE WON");               
                    do{
                        winscounter +=1;
                    }
                    while (winscounter != winscounter);
                }
                else if (choice == 3){ //Scissors
                    System.out.println("
YOU'VE LOST");
                    do{
                        losscounter+=1;
                    }
                    while(losscounter!=losscounter);
                }
            }
            else if (value == 2){ //Paper
                if (choice == 1){ //Rock
                    System.out.println("
YOU'VE LOST");
                    do{
                        losscounter+=1;
                    }
                    while(losscounter!=losscounter);
                }
                else if (choice == 3){ //Scissors
                    System.out.println("
YOU'VE WON");
                    do{
                        winscounter +=1;
                    }
                    while (winscounter != winscounter);
                }
            }
            else if (value == 3){ //Scissors
                if (choice == 1){ //Rock
                    System.out.println("
YOU'VE WON");
                    do{
                        winscounter +=1;
                    }
                    while (winscounter != winscounter);                    
                }
                else if (choice == 2){ //Paper
                    System.out.println("
YOU'VE LOST");
                    do{
                        losscounter+=1;
                    }
                    while(losscounter!=losscounter);
                }
            }
            return(tiescounter);
            return(winscounter);
            return(losscounter);
    }
}
推荐答案
一个函数只能返回一个值.一旦 return(tiescounter); 被执行,函数就会退出.如果要返回所有三个值,则必须将它们包装在一个类中.
A function can only return one value.  As soon as return(tiescounter); is executed the function exits.  If you want to return all three values you will have to wrap them in a class.
顺便说一下,return(tiescounter); 可以写成 return tiescounter; 不需要在返回值周围加上括号.两个语句的结果相同.
By the way return(tiescounter); can be written as return tiescounter;  Parenthesis around return values is not required.  Both statements will have the same result.
这篇关于无法到达的声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无法到达的声明?
				
        
 
            
        - C++ 和 Java 进程之间的共享内存 2022-01-01
 - Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
 - value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
 - Java包名称中单词分隔符的约定是什么? 2022-01-01
 - Jersey REST 客户端:发布多部分数据 2022-01-01
 - 将log4j 1.2配置转换为log4j 2配置 2022-01-01
 - Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
 - 如何使用WebFilter实现授权头检查 2022-01-01
 - Eclipse 插件更新错误日志在哪里? 2022-01-01
 - 从 finally 块返回时 Java 的奇怪行为 2022-01-01
 
						
						
						
						
						