2013-01-23 95 views
-1

我正在嘗試創建一個測驗遊戲,該測驗遊戲將在活動結束時顯示用戶的分數。我有50個問題,但每次用戶播放時只會顯示10個問題。我遇到了麻煩,因爲無法保存用戶的分數。如何在用戶每次玩時隨機化問題

,以下是我的java 代碼假設Q1.javaQ2.java是我的java類Whoami.java是用戶開始測驗的代碼,score.java顯示得分。請幫助我們!提前致謝!

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_question1); 

    TextView q1 = (TextView) findViewById(R.id.q1); 
    q1.setText("I am an amphibian who has undergone metamorphosis. I have slippery skin, and spend most of my time in or near water. I breathe with lungs now, but when I was a tadpole I had gills."); 
} 

@Override 
public void onClick(View arg0) 
{ 
    // TODO Auto-generated method stub 
    int cnt = 0; 

    final Animation animScale = AnimationUtils.loadAnimation(this, R.animator.anim_scale); 
    final Animation animAlpha = AnimationUtils.loadAnimation(this, R.animator.anim_alphas); 

    Button btn1 = (Button) findViewById(R.id.btnopt1_a); 
    Button btn2 = (Button) findViewById(R.id.btnopt1_b); 
    Button btn3 = (Button) findViewById(R.id.btnopt1_c); 

    ImageView img1 = (ImageView) findViewById(R.id.imageView1); 
    ImageView img2 = (ImageView) findViewById(R.id.imageView2); 
    ImageView img3 = (ImageView) findViewById(R.id.imageView3); 

    img1.setOnClickListener(new ImageView.OnClickListener() 
    { 
      @Override 
      public void onClick(View arg0) { 
       arg0.startAnimation(animScale); 
      }}); 

    img2.setOnClickListener(new ImageView.OnClickListener() 
    { 
      @Override 
      public void onClick(View arg0) { 
       arg0.startAnimation(animAlpha); 
      }}); 

    img3.setOnClickListener(new ImageView.OnClickListener() 
    { 
      @Override 
      public void onClick(View arg0) { 
       arg0.startAnimation(animAlpha); 
      }}); 

    if(btn1.isClickable())  
    { 
     img1.startAnimation(animScale); 
     cnt++; 
     Intent i = new Intent(this,Q2.class); 
     i.putExtra("score",cnt); 
     startActivity(i); 
    } 

    else if (btn2.isClickable()) 
    { 
     img2.startAnimation(animAlpha); 
    } 

    else if (btn3.isClickable()) 
    { 
     img3.startAnimation(animAlpha); 
    } 
} 
    @Override 
    protected void onPause() { 
     // TODO Auto-generated method stub 
     super.onPause(); 
     finish(); 
    } 
} 

那麼這是我的其他類名Q2.java

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_question2); 

    TextView q2 = (TextView) findViewById(R.id.q2); 
    q2.setText("I am a bird that has wings, but does not fly. I spend most of my time in the water. I am known to live in colder regions like Antarctica."); 
} 

@Override 
public void onClick(View arg0) 
{ 
    // TODO Auto-generated method stub 
    int cnt = 0; 

    final Animation animScale = AnimationUtils.loadAnimation(this, R.animator.anim_scale); 
    final Animation animAlpha = AnimationUtils.loadAnimation(this, R.animator.anim_alphas); 

    Button btn1 = (Button) findViewById(R.id.btnopt2_a); 
    Button btn2 = (Button) findViewById(R.id.btnopt2_b); 
    Button btn3 = (Button) findViewById(R.id.btnopt2_c); 

    ImageView img1 = (ImageView) findViewById(R.id.imageView1); 
    ImageView img2 = (ImageView) findViewById(R.id.imageView2); 
    ImageView img3 = (ImageView) findViewById(R.id.imageView3); 

    img1.setOnClickListener(new ImageView.OnClickListener() 
    { 
      @Override 
      public void onClick(View arg0) { 
       arg0.startAnimation(animScale); 
      }}); 

    img2.setOnClickListener(new ImageView.OnClickListener() 
    { 
      @Override 
      public void onClick(View arg0) { 
       arg0.startAnimation(animAlpha); 
      }}); 

    img3.setOnClickListener(new ImageView.OnClickListener() 
    { 
      @Override 
      public void onClick(View arg0) { 
       arg0.startAnimation(animAlpha); 
      }}); 

    if(btn1.isClickable())  
    { 
     img1.startAnimation(animScale); 
     cnt++; 
     Intent i = new Intent(this,Q3.class); 
     i.putExtra("score",cnt); 
     startActivity(i); 
    } 

    else if (btn2.isClickable()) 
    { 
     img2.startAnimation(animAlpha); 
    } 

    else if (btn3.isClickable()) 
    { 
     img3.startAnimation(animAlpha); 
    } 
} 
    @Override 
    protected void onPause() { 
     // TODO Auto-generated method stub 
     super.onPause(); 
     finish(); 
    } 
} 

這裏是我的Score.java

@Override 
    public void onCreate(Bundle savedInstanceState) 
{ 
    try 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_score); 

     Intent intent = getIntent(); 
     int score = intent.getIntExtra("score",0); 

     Button btnok = (Button)findViewById(R.id.btnok); 
     btnok.setOnClickListener(this); 

     TextView txtscore = (TextView) findViewById(R.id.txtscore); 
     txtscore.setText("Your score is : " + score +"/10"); 
    } 
    catch(Exception e) 
    { 
     try { 
      throw e; 
     } catch (Exception e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
    } 
} 

@Override 
public void onClick(View arg0) { 
    // TODO Auto-generated method stub 
    switch (arg0.getId()) 
    { 

case R.id.btnok: 
    Intent i = new Intent (this,MainMenu.class); 
    startActivity(i);  
default: 
    break; 
    } 

} 

@Override 
protected void onPause() { 
    // TODO Auto-generated method stub 
    super.onPause(); 
    finish(); 
} 

}

,最後我Whoami.java類

public class Whoami extends Activity{ 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 

setContentView(R.layout.activity_whoami); 


Button bplay =(Button) findViewById(R.id.bplay); 
bplay.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View arg0) { 

     Random random = new Random(); 

     switch(random.nextInt(50)) 
     { 

      case 0: 
      Intent newActivity1 = new Intent(Whoami.this,Q1.class);  
      startActivity (newActivity1); 
      break; 
      case 1: 
      Intent newActivity2 = new Intent(Whoami.this,Q2.class);  
      startActivity(newActivity2); 
      break; 
      case 2: 
      Intent newActivity3 = new Intent(Whoami.this,Q3.class);  
      startActivity(newActivity3); 
      break; 
      case 3: 
      Intent newActivity4 = new Intent(Whoami.this,Q4.class);  
      startActivity(newActivity4); 
      break; 
      case 4: 
      Intent newActivity5 = new Intent(Whoami.this,Q5.class);  
      startActivity(newActivity5); 
      break; 
      case 5: 
      Intent newActivity6 = new Intent(Whoami.this,Q6.class);  
      startActivity(newActivity6); 
      break; 
      case 6: 
      Intent newActivity7 = new Intent(Whoami.this,Q7.class);  
      startActivity(newActivity7); 
      break; 
      case 7: 
      Intent newActivity8 = new Intent(Whoami.this,Q8.class);  
      startActivity(newActivity8); 
      break; 
      case 8: 
      Intent newActivity9 = new Intent(Whoami.this,Q9.class);  
      startActivity(newActivity9); 
      break; 
      case 9: 
      Intent newActivity10 = new Intent(Whoami.this,Q10.class);  
      startActivity(newActivity10); 
      break; 
      case 10: 
      Intent newActivity11 = new Intent(Whoami.this,Q11.class);  
      startActivity(newActivity11); 
      break; 
      case 11: 
      Intent newActivity12 = new Intent(Whoami.this,Q12.class);  
      startActivity(newActivity12); 
      break; 
      case 12: 
      Intent newActivity13 = new Intent(Whoami.this,Q13.class);  
      startActivity(newActivity13); 
      break; 
      case 13: 
       Intent newActivity14 = new Intent(Whoami.this,Q14.class); 
       startActivity(newActivity14); 
       break; 
      case 14: 
       Intent newActivity15 = new Intent(Whoami.this,Q15.class); 
       startActivity(newActivity15); 
       break; 
      case 15: 
       Intent newActivity16 = new Intent(Whoami.this,Q16.class); 
       startActivity(newActivity16); 
       break; 
      case 16: 
       Intent newActivity17 = new Intent(Whoami.this,Q17.class);  
       startActivity (newActivity17); 
       break; 
      case 17: 
       Intent newActivity18 = new Intent(Whoami.this,Q18.class);  
       startActivity(newActivity18); 
       break; 
      case 18: 
       Intent newActivity19 = new Intent(Whoami.this,Q19.class);  
       startActivity(newActivity19); 
       break; 
      case 19: 
       Intent newActivity20 = new Intent(Whoami.this,Q20.class);  
       startActivity(newActivity20); 
       break; 
      case 20: 
       Intent newActivity21 = new Intent(Whoami.this,Q21.class);  
       startActivity(newActivity21); 
       break; 
      case 21: 
       Intent newActivity22 = new Intent(Whoami.this,Q22.class);  
       startActivity(newActivity22); 
       break; 
      case 22: 
       Intent newActivity23 = new Intent(Whoami.this,Q23.class);  
       startActivity(newActivity23); 
       break; 
      case 23: 
       Intent newActivity24 = new Intent(Whoami.this,Q24.class);  
       startActivity(newActivity24); 
       break; 
      case 24: 
       Intent newActivity25 = new Intent(Whoami.this,Q25.class);  
       startActivity(newActivity25); 
       break; 
      case 25: 
       Intent newActivity26 = new Intent(Whoami.this,Q26.class);  
       startActivity(newActivity26); 
       break; 
      case 26: 
       Intent newActivity27 = new Intent(Whoami.this,Q27.class);  
       startActivity(newActivity27); 
       break; 
      case 27: 
       Intent newActivity28 = new Intent(Whoami.this,Q28.class);  
       startActivity(newActivity28); 
       break; 
      case 28: 
       Intent newActivity29 = new Intent(Whoami.this,Q29.class);  
       startActivity(newActivity29); 
       break; 
      case 29: 
        Intent newActivity30 = new Intent(Whoami.this,Q30.class); 
        startActivity(newActivity30); 
        break; 
      case 30: 
        Intent newActivity31 = new Intent(Whoami.this,Q31.class); 
        startActivity(newActivity31); 
        break; 
      case 31: 
        Intent newActivity32 = new Intent(Whoami.this,Q32.class); 
        startActivity(newActivity32); 
        break; 
      case 32: 
        Intent newActivity33 = new Intent(Whoami.this,Q33.class);  
        startActivity(newActivity33); 
        break; 
      case 33: 
        Intent newActivity34 = new Intent(Whoami.this,Q34.class);  
        startActivity(newActivity34); 
        break; 
      case 34: 
        Intent newActivity35 = new Intent(Whoami.this,Q35.class);  
        startActivity(newActivity35); 
        break; 
      case 35: 
        Intent newActivity36 = new Intent(Whoami.this,Q36.class);  
        startActivity(newActivity36); 
        break; 
      case 36: 
        Intent newActivity37 = new Intent(Whoami.this,Q37.class);  
        startActivity(newActivity37); 
        break; 
      case 37: 
        Intent newActivity38 = new Intent(Whoami.this,Q38.class);  
        startActivity(newActivity38); 
        break; 
      case 38: 
        Intent newActivity39 = new Intent(Whoami.this,Q39.class);  
        startActivity(newActivity39); 
        break; 
      case 39: 
        Intent newActivity40 = new Intent(Whoami.this,Q40.class);  
        startActivity(newActivity40); 
        break; 
      case 40: 
        Intent newActivity41 = new Intent(Whoami.this,Q41.class);  
        startActivity(newActivity41); 
        break; 
      case 41: 
        Intent newActivity42 = new Intent(Whoami.this,Q42.class);  
        startActivity(newActivity42); 
        break; 
      case 42: 
        Intent newActivity43 = new Intent(Whoami.this,Q43.class);  
        startActivity(newActivity43); 
        break; 
      case 43: 
        Intent newActivity44 = new Intent(Whoami.this,Q44.class);  
        startActivity(newActivity44); 
        break; 
      case 44: 
        Intent newActivity45 = new Intent(Whoami.this,Q45.class); 
        startActivity(newActivity45); 
        break; 
      case 45: 
        Intent newActivity46 = new Intent(Whoami.this,Q46.class); 
        startActivity(newActivity46); 
        break; 
      case 46: 
        Intent newActivity47 = new Intent(Whoami.this,Q47.class); 
        startActivity(newActivity47); 
        break; 
      case 47: 
        Intent newActivity48 = new Intent(Whoami.this,Q48.class);  
        startActivity (newActivity48); 
        break; 
      case 48: 
        Intent newActivity49 = new Intent(Whoami.this,Q49.class);  
        startActivity(newActivity49); 
        break; 
      case 49: 
        Intent newActivity50 = new Intent(Whoami.this,Q50.class);  
        startActivity(newActivity50); 
        break; 

     } 
    } 
    }); 
} 

@Override 
protected void onPause() { 
    // TODO Auto-generated method stub 
    super.onPause(); 
    finish(); 
} 

}

+4

您的標題問如何隨機化問題,在你寫這個問題的文字是你無法保存用戶評分。請只問一個具體問題(每個問題),因爲這個網站不是_here是我的代碼,請修復它的地方。如果您解釋您的問題並向我們展示您的代碼的相關部分,我們將很樂意提供幫助。 – jlordo

+3

你爲什麼要爲每個問題創建1個活動?嘗試保留一項活動,只更新該活動的問題。 – R9J

+3

如果您有1000個問題,該怎麼辦?你會寫1000分支的交換機嗎? –

回答

0

與它一起創建QuestionActivity類和xml佈局。使用

Intent intent = getIntent(); 
String question = intent.getStringExtra("question"); 

然後將問題變成一個TextView通過

intentInstance.putExtra("question", yourRandomQuestion); 

和字符串到活動 通的問題得到了QuestionActivity類傳遞價值。

希望它有幫助:)

+0

你是什麼意思?我會把我的問題放在這個圓括號裏面嗎?(「問題」) –

+0

只需要在strings.xml中設置所有的問題,只需要用它的名字來調用任何問題:) – R9J

+0

好的@Raj ..真的很棒!!非常感謝 –

相關問題