2017-08-12 41 views
0

我想設置一個問答對話框,它會詢問幾個問題並存儲同樣多的答案。當我通過QuestionsAnswer.java傳遞一個int時,它將設置@string/questions[i]@string/answers[i]。我試圖通過在這裏搜索和java的文檔找到我的答案,沒有運氣。單個java switch語句返回2個變量

僞代碼:

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; 
    } 
} 

下面是編輯後的漢化版。它完美的工作! 公共類問題答案{

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; 
    } 
} 
+1

你的僞代碼應工作,只是用smicolon替換逗號('')(';')... –

+0

把它全部扔掉,並使用某種表格。 – EJP

+0

我討厭語法錯誤哈哈......但它現在很好用!謝謝! –

回答

3

您可以使用;

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

謝謝你的幫助!我有大約3周的Java。 –

+0

@MichaelViox歡迎您。我建議你與Java的基本語法相處,然後閱讀OOPS。您試圖解決的問題有許多更好的實施方式。 –