java - 方法未返回正确的值 (Java)

标签 java methods return

public static char getChoice(char choice) {
    System.out.println("\nTake or stop? (t/s): ");
    choice = sc.next()
        .charAt(0);
    if (choice == 't') {
        System.out.print("Player Took");
        choice = 't';

    } else if (choice == 's') {
        System.out.println("Player didnt take");
        choice = 's';

    }
    return choice;
    //return answer;
}

这是主体中调用它的部分

        //Checking if players score is 21 when the game begins
    if(ptotal == 21)
    {
        System.out.print("\nPlayer wins!");
    }
    //Checking if the computers score is 21 when the game
    else if(ctotal == 21)
    {
        System.out.print("\nComputer wins!");
    }
    //Checking if both scores are 21 which would be a tie
    else if(ctotal == 21 && ptotal == 21)
    {
        System.out.print("\nTie!");
    }
    //If none above is true it will proceed to the loop
    else
    {
        //getChoice(answer);
        while(ptotal < 21 && answer == 't')//Loop conditions
        {
            getChoice(answer);
            //Prompting user if they would like to take or stop
            /*System.out.println();
            System.out.print("Take or stop? (t/s): ");
            answer = sc.next().charAt(0);
            System.out.println("");*/


            //Checking if the input is 't'
            if(answer == 't')
            {
                //If true another card will be generated and added to the total value
                System.out.print("\nPlayer draws: ");
                pCard = getCard();
                showCard(pCard);
                ptotal += cardValue(pCard);

            }
            //Checking if input is 's'
            else if(answer == 's')
            {
                System.out.println("Player Stops");
            } 
        }

这应该要求用户输入字符,然后根据 if 语句返回一个特定的字符,但是每次我从主函数调用它时它都会返回它,它不会显示这些字符串,但不会将选择设置为正确的字符。请帮忙。

最佳答案

//always post an MCVE
//see http://stackoverflow.com/help/mcve
import java.util.Scanner;

public class Test{

    public static void main(String[] args){

        char c = getChoice();
        System.out.println("returned value is "+ c);
    }

    //why pass a value choise if you read it from user ?
    public static char getChoice(/*char choice*/)
    {
        System.out.println("\nTake or stop? (t/s): ");

        Scanner sc = new Scanner( System.in );
        char choice = sc.next().charAt(0);
        if(choice == 't')
        {
            System.out.println("Player Took");
            // choice is already equal to `t`. This is not needed
            // choice = 't';

        }

        else if(choice == 's')
        {
            System.out.println("Player didnt take");
            // choice is already equal to `s`. This is not needed
            choice = 's';

        }
        return choice;
    }
}

关于java - 方法未返回正确的值 (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39737482/

相关文章:

C# 编译器错误 : "not all code paths return a value"

java - 如何从命令行导入随机数量的参数?

java - 使用 OpenGL ES 2.0 在 Android 中高效绘制许多纹理

java - 多个线程读取原始类型

java - 如何在java中过滤对象的列表变量?

java - 如何在方法声明上使用 @around spring aop 注释?

PHP静态方法递归

java - 在 Java 中比较两个 ArrayList

java - 数组列表 - 级别的大小?

c - C 中的返回值