简体   繁体   中英

intro java coding. don't understand what i'm doing wrong

i'm really not sure what's wrong with my code. It's supposed to do rock paper scissors against the computer by taking in the user choice, comparing it to the random computer choice, and displaying the results.

I get two errors that i have no return statements for the 3rd and 4th methods. Also, when i run it without fixing the errors, the nested if statements starting at line 60 only print out one of the two println statements, which really makes zero sense to me.

import java.util.Random;
import java.util.Scanner;
public class Chapter5ProjectPart2 {

    public static void main(String[] args) {

    Random generator = new Random();
    Scanner keyboard = new Scanner(System.in);

    int userNum;
    int compNum;
    String userChoice = "";
    String compChoice = "";
    int rnum;
    int result = 0;
    boolean keepPlaying;
    int input = 1;


    do
    {   
        compNum = generator.nextInt(2)+1;
        compChoice = numToChoice(compNum);

        menu();

        userNum = keyboard.nextInt();
        userChoice = numToChoice(userNum);
        keyboard.nextInt();

        System.out.println();
        System.out.println("you chose " + userChoice);
        System.out.println("the computer chose " + compChoice);

        result = resultCheck(userNum, compNum);


        if (result == 1) // user wins
        {
            if (userNum == 1) //user won choosing rock
            {
                System.out.println("rock beats scissors");
                System.out.println("you win");
            }
            else if (userNum == 2) //user won choosing paper
            {
                System.out.println("paper beats rock");
                System.out.println("you win");
            }
            else if (userNum == 3)  //user won choosing scissors
            {
                System.out.println("scissors beats paper");
                System.out.println("you win");
            }
        }
        else if (result == 3) //user loses
        {
            if (userNum == 1)  //user lost choosing rock
            {
                System.out.println("paper beats rock");
                System.out.println("you lose");
            }
            else if (userNum == 2)  //user lost choosing paper
            {
                System.out.println("scissors beats paper");
                System.out.println("you lose");
            }
            else if (userNum == 3)  //user lost choosing scissors
            {
                System.out.println("rock beats scissors");
                System.out.println("you lose");
            }
        else if (result == 2) //draw
            System.out.println("draw");
        }

        System.out.println("would you like to play again?");
        System.out.println("1 = yes");
        System.out.println("2 = no");
        input = keyboard.nextInt();
        keepPlaying = play(input);

    } while (keepPlaying == true);

}


// method 1 (menu)
public static void menu()
{
System.out.println("Enter your choice of rock, paper, or scissors\n" + "1 = rock\n" + "2 = paper\n" + "3 = scissors");
}

// method 2 (result check)
public static int resultCheck(int userNum, int compNum)
{
    if (userNum == 2 && compNum == 1)
        return 1;
    else if (userNum == 1 && compNum == 3)
        return 1;
    else if (userNum == 3 && compNum == 2)
        return 1;
    else if (userNum == compNum)
        return 2;
    else
        return 3;
}

// method 3 (converting number choice to rock/paper/scissors
public static String numToChoice(int num)
{
    if (num == 1)
        return "rock";
    else if (num == 2)
        return "paper";
    else if (num == 3)
        return "scissors";
}

//method 4 (play again)
public static boolean play(int input)
{
    if (input == 1)
        return true;
    else if (input == 2)
        return false;
}


}

I get two errors that i have no return statements for the 3rd and 4th methods.

Right. Let's look at the third:

public static String numToChoice(int num)
{
    if (num == 1)
        return "rock";
    else if (num == 2)
        return "paper";
    else if (num == 3)
        return "scissors";
}

Suppose num isn't 1, 2, or 3? Then what should the return value of the method be? That's why you're getting the error, you need a final else (with no if ) saying what that return value should be when none of the earlier branches has returned a value. Without it, the method causes a compile-time error.

Also, when i run it without fixing the errors, the nested if statements starting at line 60 only print out one of the two println statements, which really makes zero sense to me.

You can't run it without fixing the errors, because these are compile-time errors. If you try to compile this source code with those errors in place, it fails, and you don't get an updated class file. So if you then try to run, and it seems to work, you're running an earlier copy of the class file you compiled before those errors were there. That class file doesn't relate to the current source code, and so it's understandable that it would make no sense to you. You're not looking at what the JVM is running.

If you correct the methods so that things compile (by adding the final else with no if on it), then run the compiled result, things should make more sense. Meanwhile, you might want to delete the previous Chapter5ProjectPart2.class file, since it's out of date.

public static String numToChoice(int num)
{
    if (num == 1)
        return "rock";
    else if (num == 2)
        return "paper";
    else if (num == 3)
        return "scissors";
}

//method 4 (play again)
public static boolean play(int input)
{
    if (input == 1)
        return true;
    else if (input == 2)
        return false;
}

These methods should always return something. Method 3 for example, if int num is 4 it won't return anything. Solve it by adding:

else return "";

那是因为您没有以else子句结束方法3和4中的else-if语句。

You indeed lack return statements. Java expects a non-void method to always return a value or throw an exception. Having a non-void method end without returning or throwing an exception is an error.

public static String numToChoice(int num)
{
    if (num == 1)
        return "rock";
    else if (num == 2)
        return "paper";
    else if (num == 3)
        return "scissors";
}

Here, your method doesn't return anything if num isn't 1, 2 or 3. Likewise:

public static boolean play(int input)
{
    if (input == 1)
        return true;
    else if (input == 2)
        return false;
}

Here inputs that aren't 1 or 2 lack their return statements.

First of all, never run code that doesn't compile.

Second: examine this method:

public static boolean play(int input)
{
    if (input == 1)
        return true;
    else if (input == 2)
        return false;
}

What would the method return if input is 6, or -67, or 789? You didn't tell, and the compiler can't guess it. So it refuses to compile until you tell it what to return in these cases.

If the other cases should never happen, then throw an exception:

public static boolean play(int input)
{
    if (input == 1)
        return true;
    else if (input == 2)
        return false;
    else {
        throw new IllegalStateException("input is " + input + ". Something's really wrong");
    }
}

Java method MUST return a value unless it states void as return.

In your play method, if input is not 1 or 2, Java won't return any value. This does not allow to compile in Java.

In your numToChoice method, if num is not 1, 2 or 3, Java won't return any value. This does not allow to compile in Java.

Add an else close to return a value in "unexpected" cases, and allow Java to compile.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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