java - 单个 java switch 语句返回 2 个变量

标签 java switch-statement

我正在尝试设置一个问答对话框,该对话框将提出几个问题并存储同样多的答案。当我通过 QuestionsAnswer.java 传递一个 int 时,它将设置 @string/questions[i]@string/answers[i]。我试图通过搜索这里和java的文档来找到我的答案,但没有成功。

伪代码:

public class QuestionAnswer {

    public static void QuestionAnswer(int QuestionNum) {

        String question;
        String answer;
        switch (QuestionNum) {
            case 1:  question = "What are you doing?", answer = "activity";
                break;
            case 2:  question = "Where have you been?", answer = "Location";
                break;
            case 3:  question = "Are you looking at me?", answer = "Boolean";
                break;
            case 4:  question = "What do you think about when I'm not around?", answer = "Crazy";
                break;
            case 5:  question = "Do you want to play a game?", answer = "MovieQuote";
                break;
            case 6:  question = "Does a cat have nine lives?", answer = "CanCatsFly";
                break;
        }
          //question is a string variable that will change the question text for the dialog
        R.string.Questions = question;
          //answer is a string variable that will change what column name the answer will be stored into
        R.string.answers = answer;
    }
}

Here is the finished version after the edit. It works perfectly! public class QuestionAnswer {

    public static void QuestionAnswer(int QuestionNum) {


        String question;
        String answer;
        switch (QuestionNum) {
            case 1:  question = "What are you doing?";
                answer = "activity";
                break;
            case 2:  question = "Where have you been?";
                answer = "Location";
                break;
            case 3:  question = "Are you looking at me?";
                answer = "Boolean";
                break;
            case 4:  question = "What do you think about when I'm not around?";
                answer = "Crazy";
                break;
            case 5:  question = "Do you want to play a game?";
                answer = "MovieQuote";
                break;
            case 6:  question = "Does a cat have nine lives?";
                answer = "CanCatsFly";
                break;
        }
          //question is a string variable that will change the question text for the dialog
        R.string.Questions = question;
          //answer is a string variable that will change what column name the answer will be stored into
        R.string.answers = answer;
    }
}

最佳答案

您可以使用;

switch (QuestionNum) {
   case 1:  question = "What are you doing?";
            answer = "activity";
            break;
            //continue with next cases
 }

关于java - 单个 java switch 语句返回 2 个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45645940/

相关文章:

java - 允许多种预定义类型的集合

perl - 为什么我的 continue 在 Perl 5.10 中不执行下一个 when block ?

python - 使用 Python 字典触发 Break 关键字

c++ - switch 语句的奇怪行为 c++

java - 是否可以从列表中删除项目而不引用其索引?

java - 我创建了 RecyclerView,当屏幕方向发生变化时,重复的项目将添加到顶部

java - Freemarker:去除换行符但不压缩空格?

java - 在不使用 protected 的情况下处理抽象类的正确方法是什么?

C# switch 语句中每个 case/default block 都带有大括号的 switch 语句?

jQuery Switch Case 插件?