Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用方法的决策 任务: 包括决策和上面的所有构造 定义并使用方法,包括接受参数并返回结果的方法_Java - Fatal编程技术网

Java 使用方法的决策 任务: 包括决策和上面的所有构造 定义并使用方法,包括接受参数并返回结果的方法

Java 使用方法的决策 任务: 包括决策和上面的所有构造 定义并使用方法,包括接受参数并返回结果的方法,java,Java,示例:如上所述,但该程序现在在情书中还有一系列可能的模板行,除上述内容外,并随机选择要包含的模板行。例如,它可能会在一行“你是我的”和一行“我会永远认为你是我的”之间做出选择。当被要求完成句子时,运行程序的人会输入一个名词(例如“snugglebunny”)(例如,在这种情况下,它可能会选择“你是我的snugglebunny”)。对于每个模板,定义了一个单独的方法,该方法返回作为参数提供的单词的完整句子 这是我设法完成的代码,但是它显示错误“String[]无法转换为printmessage(a

示例:如上所述,但该程序现在在情书中还有一系列可能的模板行,除上述内容外,并随机选择要包含的模板行。例如,它可能会在一行“你是我的”和一行“我会永远认为你是我的”之间做出选择。当被要求完成句子时,运行程序的人会输入一个名词(例如“snugglebunny”)(例如,在这种情况下,它可能会选择“你是我的snugglebunny”)。对于每个模板,定义了一个单独的方法,该方法返回作为参数提供的单词的完整句子

这是我设法完成的代码,但是它显示错误“String[]无法转换为printmessage(ans1,input)的String”。我不知道我在这个问题上哪里出了问题,如果有人能帮忙,我会很高兴的

public static void decisions()
    {
        String input; 
        String phrase = "";

        input = askquestion ();  // defining the method for asking user the question to input a noun word.

        String [] ans1 = {"You remind me of my","I always think of you as my","I wish You could always be my","you are my one and only"};   // this will randomly select one phrase out of 4 given

        int rand = (int) (Math.random() * 4);  //this will do a random math calculation and select one phrase

        switch (rand)
                {
                case 0:
                printmessage(ans1, input);
                break;

                case 1:
                printmessage(ans1, input);
                break;

                case 2:
                printmessage(ans1, input);
                break;

                case 3:
                printmessage(ans1, input);
                break;
                }   

         // this defines the method for printing the message, taking the argument strings inside the bracket to the message defined later on.

    } // END decisions


    public static String askquestion ()

    {
        String result = "";

        result = JOptionPane.showInputDialog("Enter a noun word to describe your partner"); //asks users input

        return result;
    }

    public static void printmessage (String ans1, String input) // this will receive the argument from the method defined above and then be printed below as shown. The argument have been declared as x and y.

    {
        JOptionPane.showInputDialog( ans1 + input ); //this will combine the two variables and execute the message.

    }

String[]是字符串数组
printmessage
需要单个字符串,而不是它们的数组。给它一根绳子

aka,在switch语句中传入
rand
参数,顺便说一下,这是多余的,因为您调用的是相同的方法

switch (rand) {
    case 0:
    printmessage(ans1[rand], input);
    break;

    case 1:
    printmessage(ans1[rand], input);
    break;

    case 2:
    printmessage(ans1[rand], input);
    break;

    case 3:
    printmessage(ans1[rand], input);
    break;
}  
可以简单地变成

printmessage(ans1[rand], input);

您对字符串[]的理解是什么?无法将其转换为字符串?什么是
字符串[]
?什么是
字符串
?错误提到的方法涉及哪些类型?我是java新手,如果您能给我解释一下,我会很高兴的@Sotiriosdelimanolis只需扔掉整个
开关
并使用
打印消息(ans1[rand],输入)取而代之。我的课程练习要求我使用决策,所以我不想把整个开关都去掉。有没有办法让切换方法正常工作@您可以调用
printmessage(ans1[rand],输入)在任何情况下。。。