方法返回正确的值,但是如果循环没有相应地起作用

时间:2018-11-23 19:40:14

标签: java methods

我已经为我的编程课写出了所有代码。这项工作需要我们创建一个程序,该程序允许用户在计算机上玩剪刀石头布。我们需要有单独的方法来获取计算机的选择,用户的选择,检查用户的选择是否有效以及确定比赛的获胜者。如果一场比赛没有平局,我们需要打印出比赛获胜的原因,例如。 “剪刀剪纸”,并打印出获胜者。一切正常,除非用户获胜,否则永远不会计算或宣布。例如,而不是打印:

计算机的选择是艰难的。用户的选择是纸张。纸覆盖岩石。用户赢了!

它打印:

计算机的选择是艰难的。用户的选择是纸张。纸覆盖了岩石。

import java.util.Scanner;
import java.util.Random;

public class FinalRockPaperScissors {

//Computer's Choice
public static int computersChoice (int options) {
    Random randGen = new Random();
    int computerValue = randGen.nextInt(options)+1;
    System.out.println(computerValue); //FOR TESTING ONLY
    return computerValue;
}

//Player's Choice
public static int usersChoice () {
    Scanner scnr = new Scanner(System.in);
    System.out.print("Enter 1 for rock, 2 for paper, or 3 for scissors: ");
    int userValue = scnr.nextInt();

    if (isValid(userValue) == true) {
        return userValue;
    }

    else {
        userValue = 0;
        return userValue;
    }


}

//Check for valid user input
public static boolean isValid (int userInput){
    if (userInput == 1 || userInput == 2 || userInput == 3) {
        return true;
    }

    else {
        return false;
    }
}

//Checking winner
public static char determineWinner () {
    char win;
    int computerValue = computersChoice(3);
    int userValue = usersChoice();

    //print computer choices
    if (computerValue == 1) {
        System.out.println("The computer's choice was rock.");
    }

    else if (computerValue == 2) {
        System.out.println("The computer's choice was paper.");
    }

    else if (computerValue == 3){
        System.out.println("The computer's choice was scissors.");
    }

    //print user choices
            if (userValue == 1) {
                System.out.println("The user's choice was rock.");
            }

            else if (userValue == 2) {
                System.out.println("The user's choice was paper.");
            }

            else if (userValue == 3){
                System.out.println("The user's choice was scissors.");
            }


    //check who won
    if (computerValue == 1) { //rock vs
        if (userValue == 2) {           //paper
            System.out.println("Paper wraps Rock.");
            return win = 'b'; 
        }

        else if (userValue == 3) {      //scissors
            System.out.println("Rock smashes Scissors.");
            return win = 'a'; 
        }

        else if (userValue == 1){                           //rock
            return win = 'c'; 
        }

        else {
            System.out.println("The user chose an invalid number. This round will be ignored.");
            return win = 'd';
        }

    }

    else if (computerValue == 2) { //paper vs
        if (userValue == 2) {           //paper
            return win = 'c'; 
        }

        else if (userValue == 3) {      //scissors
            System.out.println("Scissors cuts Paper.");
            return win = 'b'; 
        }

        else if (userValue == 1){                           //rock
            System.out.println("Paper wraps Rock.");
            return win = 'a'; 
        }

        else {
            System.out.println("The user chose an invalid number. This round will be ignored.");
            return win = 'd';
        }

    }

    else { //scissors vs
        if (userValue == 2) { //paper
            System.out.println("Scissors cuts Paper.");
            return win = 'a';
        }

        else if (userValue == 3) { //scissors
            return win = 'c';
        }

        else if (userValue == 1){ //rock
            System.out.println("Rock smashes Scissors.");
            return win = 'b';
        }

        else {
            System.out.println("The user chose an invalid number. This round will be ignored.");
            return win = 'd';
        }

    }

}

public static void main(String[] args) {
    int userWins = 0;
    int computerWins = 0;
    int ties = 0;
    int error = 0;

    //for (int i = 0; i < 1; i++) { //5 for testing purposes
        if (determineWinner() == 'a') {
            System.out.println("The computer wins!");
            System.out.println("");
            computerWins++;
        }

        else if (determineWinner() == 'b') {
            System.out.println("The user wins!");
            System.out.println("");
            userWins++;
        }

        else if (determineWinner() == 'c'){
            System.out.println("The game is tied!");
            System.out.println("");
            ties++;
        }

        else {
            error++;
        }
    //}

    System.out.println("The number of ties is " + ties);
    System.out.println("The number of user wins is " + userWins);
    System.out.println("The number of computer wins is " + computerWins);

    //output final winner
    if (computerWins > userWins) {
        System.out.println("Computer is the winner.");
    }

    else if (userWins > computerWins) {
        System.out.println("User is the winner.");
    }

    else {
        if (userWins == computerWins) {
            System.out.println("User is the winner.");
        }
        else if (computerWins == ties) {
            System.out.println("Computer is the winner.");
        }
    }


}

}

经过一些测试,我发现问题可能出在我的userchoice()方法上。如果我禁用此方法并为用户设置一个值,那么一切都会正常进行。问题是我不知道为什么它不起作用,结果我无法修复它。

2 个答案:

答案 0 :(得分:0)

main函数中,您每次测试if条件时都会调用determineWinner()。正确的方法是将返回的值存储在变量中,然后检查该变量是否为“ a”,“ b”或“ c”。例如:

char result = determineWinner();
if (result == 'a') {
    System.out.println("The computer wins!");
    System.out.println("");
    computerWins++;
}

答案 1 :(得分:0)

我认为您是Java的新手,并且让我们开始向您展示您在此代码中的错误,您多次致电determineWinner(),但这并不是那样,因为您重复了游戏四次确定一次结果,因此您应该调用一次并获取返回值并检查该值,例如:-

char result = determineWinner(); //Play the game and get the result

// use it in conditions

if (result == 'a') {  
    System.out.println("The computer wins!");
    System.out.println("");
    computerWins++;
}

else if (result == 'b') {
    System.out.println("The user wins!");
    System.out.println("");
    userWins++;
}

else if (result == 'c'){
    System.out.println("The game is tied!");
    System.out.println("");
    ties++;
}

else {
    error++;
}