如果嵌套错误导致冗余代码循环,如何找出如何停止

问题描述 投票:0回答:1

我为猫与狗性格测验设置了代码,但 if 代码一直是多余的。

import java.util.Scanner;


public class xxxxBranchingInteractions {
    
    public static void main(String[] args) {

        Scanner s = new Scanner(System.in);

        // Welcome message
        System.out.println("Welcome to Dog or Cat");
        
        //Introduction of point system for dog and cat quiz
                int dog = 0;
                int cat = 0;
        
// #1   //Prompt user to print name
        System.out.println("What's your name?");
        //Allows user input
        String input = s.nextLine();

//#2    //Prompt user to answer dog or cat
        System.out.println("Are you a dog or cat person?");
        //Allow input
        String answerTwo = "";
        answerTwo = s.nextLine();

        //boolean variable
        boolean continueInteraction = true;
        //while loop
        while (continueInteraction) {
//#3    
        //Introduction of major if loop
        //If user inputs dog
        if (answerTwo.equalsIgnoreCase("Dog")) {
    
            System.out.println("Are you extroverted?");

            input = s.nextLine();
            //If user answers yes to being extroverted
                if (input.equalsIgnoreCase("Yes")) {
                    dog++;
                    
                    System.out.println("So you enjoy being with others?");
                    
                    input = s.nextLine();
                    //If user inputs yes to enjoying being with others
                        if (input.equalsIgnoreCase("Yes")) {
                            dog++;
                            
                            System.out.println("Cool.");

                        //If user inputs no to enjoying being with others
                            } else if (input.equalsIgnoreCase("No")) {
                                cat++;
                                
                                System.out.println("That's okay.");

                            }
            //If user answers no to being extroverted
                    } else if (input.equalsIgnoreCase("No")) {
                        cat++;
                        
                        System.out.println("But do you like being outside?");
                        
                        input = s.nextLine();
                        //If user answers yes to enjoying outdoors
                            if (input.equalsIgnoreCase("Yes")) {
                                dog++;
                                
                                System.out.println("Fun!");
                                
                        //If user answers no to enjoying outdoors
                            } else if (input.equalsIgnoreCase("No")) { 
                                cat++;
                                System.out.println("Me too.");
                            }
                        
                    }
            // Dog person route convenes to new question
            System.out.println("Do you enjoy loud environments?");
            
            input = s.nextLine();
            
                //If user enjoys loud environments
                if (input.equalsIgnoreCase("Yes")) {
                    dog++;
                    
                    System.out.println("Do you prefer it to be loud with people or music?");
                    
                    input = s.nextLine();
                    
                        //If user enjoys people more
                        if (input.equalsIgnoreCase("People")) {
                            dog++;
                            
                            System.out.println("You seem socially active!");
                            
                        // If user enjoys music more
                        } else if (input.equalsIgnoreCase("Music")) {
                            cat++;
                            
                            System.out.println("You seem chill.");
                        }
                //If user does not enjoy loud environments
                } else if (input.equalsIgnoreCase("No")) {
                    cat++;
                    
                    System.out.println("Do you find talking tiring?");
                    
                    input = s.nextLine();
                    
                    //If user does find talking tiring
                    if (input.equalsIgnoreCase("Yes")) {
                        cat++;
                        
                        System.out.println("You seem chill.");
                    //If user does not find talking tiring
                    } else if (input.equalsIgnoreCase("No")) {
                        dog++;
                        
                        System.out.println("You seem socially active!");
                    }
                ///////// END OF DOG ROUTE
                
                //If user inputs cat
                }
                } else if (answerTwo.equalsIgnoreCase("Cat")){
                    cat++;
    
                    System.out.println("Are you introverted?");
                    
                    input = s.nextLine();
                    
                    //If user is introverted
                    if (input.equalsIgnoreCase("Yes")) {
                        cat++;
                        
                        System.out.println("Do you like being alone?");
                        
                        input = s.nextLine();
                        
                        //If user enjoys being alone
                        if (input.equalsIgnoreCase("Yes")) {
                            cat++;
                            
                            System.out.println("Alone time is important!");
                        
                        //If user does not enjoy being alone
                        } else if (input.equalsIgnoreCase("No")) {
                            dog++;
                            
                            System.out.println("Connection is great!");
                        }
                    //If user is not introverted
                    } else if (input.equalsIgnoreCase("No")) {
                        dog++;
                        
                        System.out.println("Do you like being inside?");
                        
                        input = s.nextLine();
                        
                        //If user enjoys indoors
                        if (input.equalsIgnoreCase("Yes")) {
                            cat++;
                            
                            System.out.println("Your home must be cozy.");
                            
                        //If user does not enjoy indoors
                        } else if (input.equalsIgnoreCase("No")) {
                            dog++;
                            System.out.println("Sunlight is good!");
                        }
                    //Needs an else statement
                    
                    System.out.println("Do you enjoy a quiet environment?");
                    
                    input = s.nextLine();
                    
                    //If user enjoys quiet
                    if (input.equalsIgnoreCase("Yes")) {
                        cat++;
                        
                        System.out.println("Quiet by using headphones or quiet by leaving a loud space?");
                        
                        input = s.nextLine();
                        
                        //If user likes using headphones
                        if (input.equalsIgnoreCase("Headphones")) {
                            dog++;
                            
                            System.out.println("You seem peaceful.");
                        
                        //If user likes leaving
                        } else if (input.equalsIgnoreCase("Leaving")) {
                            cat++;
                            
                            System.out.println("Oh, you've gone away.");
                        }
                    
                    //If user does not enjoy quiet
                    } else if (input.equalsIgnoreCase("No")) {
                        dog++;
                        
                        System.out.println("Do you find silence awkward?");
                        
                        input = s.nextLine();
                        
                        //If user does not enjoy silence
                        if (input.equalsIgnoreCase("Yes")) {
                            dog++;
                            
                            System.out.println("Oh, you've gone away.");
                        
                        //If user enjoys silence
                        } else if (input.equalsIgnoreCase("No")) {
                            cat++;
                            
                            System.out.println("You seem peaceful.");
                        }
                    }
                    
                    
                    
                    }
                    ///////// END OF CAT ROUTE

                //If user cannot spell correctly
                } else {
    
                    System.out.println("Please type dog or cat");
    
                    answerTwo = s.nextLine();
    
                }


            }
        if ((answerTwo.equalsIgnoreCase("Dog")) || (answerTwo.equalsIgnoreCase("Cat"))) {
            //Convergence of initial dog and cat branches with last question
            System.out.println("Do you prefer the night or day?");
            
            input = s.nextLine();
            
            //If user prefers the night
            if (input.equalsIgnoreCase("Night")) {
                cat++;
            
            //If user prefers the day
            } else if (input.equalsIgnoreCase("Day")) {
                dog++;
            }
            
            //Beginning of the end
            System.out.println("Hope you have a good one!");
            
            //Ending and results
            //if cat > dog
                   // Haven't gotten to this part because I test ran it and it started acting up

        
        }
        }
    }



        

我知道它有点长,所以我有点希望这个错误能够让那些更熟悉 Java 的人感到不舒服。

我想让代码继续下去,但它却不断循环回到之前的问题。

java if-statement nested
1个回答
0
投票

你的代码是如此兰格,你只使用 if 和 else if ,为什么你不使用 else ?并且您没有结束代码或中断,答案会让您回来。你必须尝试使用 else 并在方法中编写你的代码,并尝试在第 92 行 ypu habe if 中进行中断,也进行 else ,并在 124 statert cat 中,ansd 在该方法中使用 else 。你的代码已交叉。

© www.soinside.com 2019 - 2024. All rights reserved.