【问题标题】:Method returns proper value but if loop does not act accordingly方法返回正确的值,但如果循环没有相应地采取行动
【发布时间】:2018-11-23 19:40:13
【问题描述】:

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

计算机的选择是摇滚。用户的选择是纸。纸覆盖岩石。用户获胜!

打印出来:

计算机的选择是摇滚。用户的选择是纸。纸覆盖岩石。

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() 方法上。如果我禁用此方法并为用户设置一个值,那么一切都会正常运行。问题是我不知道为什么它不起作用,因此我无法修复它。

【问题讨论】:

  • 方法名determineWinner不是很准确,其实这个方法更像playOneRound——比看user9849588的回答(或者用一个开关代替if的)
  • 为什么需要for循环

标签: java methods


【解决方案1】:

我认为你对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++;
}

【讨论】:

  • 啊,好吧,我明白我做错了什么。谢谢!这解决了它。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-09
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多