0
package veda.software.game;
import java.util.ArrayList;
import java.util.Iterator;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class RandomNumGenActivity extends Activity  {
    View v1 = new View(getApplicationContext());
    TextView txtName;
    Button submitbutton;
    int choice;
    Integer level = 1;
    String toRem = "";
    int life = 0;
    int score;
    boolean watsUp1 = true;
    Handler myHandler =  new Handler();
    int noOfGames = 0;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen2);
        txtName = (TextView) findViewById(R.id.editText1);
        submitbutton = (Button) findViewById(R.id.button1);
        Intent i = getIntent();
        choice = Integer.parseInt(i.getStringExtra("choice"));
        this.scoreLife(); 
    }
    public void scoreLife(){
        while(true){
            // TODO Auto-generated method stub
            if(watsUp1==true){
                Context context = getApplicationContext();
                CharSequence text = "you have to remember the !!"+"The"+level.toString()+"from last";
                int duration = Toast.LENGTH_SHORT;
                Toast toast = Toast.makeText(context, text, duration);
                toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 400);
                toast.show();
                watsUp1=false;
                noOfGames++;
                if(noOfGames==5){
                    noOfGames=0;
                    level++;
                }
                if(level==9){
                    break;
                }
                else{
                    RandomNumGenActivity.this.startTheGame();
                }
            }
        }
    }
    public int getArraySize(){
        int arraySize = 0 + (int)(Math.random() * ((9 - 0) + 1));
        return arraySize;
    }
    public void startTheGame(){
        ArrayList<Integer> arrayOfNum = new ArrayList<Integer>();
        int arraySize = this.getArraySize();
        if(arraySize<level&&arraySize<4){
            arraySize = this.getArraySize();
        }
        else{
            for (int idx = 1; idx <= arraySize; ++idx){
                int randomInt = choice*1 + (int)(Math.random() * ((choice*9 - choice*1) + 1));
                arrayOfNum.add(randomInt);
                Log.d("randomInt are:",""+randomInt);
            }
            this.theGame(arrayOfNum);
        }
    }
    public void theGame(ArrayList<Integer> theArray){
        Iterator<Integer> ite = theArray.iterator();
        int counter = 0;
        while(ite.hasNext()){
            counter++;
            String temp = ite.next().toString();
            if(counter == theArray.size()-level){
                toRem = temp.toString();
            }

            Log.d("random array elements are:",""+temp);
            showToast(temp);
        }
    }   
    public void showToast(String txtToDisplay){
        Log.d("inside showToast",""+txtToDisplay);
        Context context = getApplicationContext();
        int duration = Toast.LENGTH_LONG;
        Toast toast = Toast.makeText(context, txtToDisplay, duration);
        toast.show();
    }
    public void buttonClick(View v1){
        final Handler handler = new Handler();
        final Runnable updater = new Runnable() {
            public void run() {
                String req = txtName.getText().toString();
                System.out.println("req is"+req );
                if(req.equals(toRem)){
                    Context context = getApplicationContext();
                    CharSequence text = "yo!! you got it right buddy";
                    int duration = Toast.LENGTH_SHORT;
                    Toast toast = Toast.makeText(context, text, duration);
                    toast.show();
                    watsUp1=true;
                }
                else{
                    Context context = getApplicationContext();
                    CharSequence text = "you are fired!!";
                    int duration = Toast.LENGTH_SHORT;
                    Toast toast = Toast.makeText(context, text, duration);
                    toast.show();
                    watsUp1=true;
                }
            }
        };
        Thread x = new Thread() {
            public void run() {
                handler.post(updater);
            }
        };
    }   
}

*

此代码不起作用。我是 android 的初学者,我认为这应该有效。请帮助..我没有收到错误。我的屏幕只是变黑了。这段代码到底哪里出了问题???

4

2 回答 2

0

你那里有一些非常糟糕的结构......

导致黑屏的原因是这个无限循环while(true),你设置的永远不会是真的,它会永远循环,没有执行任何操作。watsUp1=falsewatsUp1==true

于 2012-08-02T22:28:28.317 回答
0

您使用无限循环的事实可能是问题所在。事实上,我想不出很多情况下无限循环不是问题。无论如何,您的循环似乎遍历了第一个 if 块

如果(watsUp1==真){

然后在该块中设置 watsUp1 = false,除非您处于无限循环中,否则这不会是问题。

此外,我将致力于让您的代码在未来更美观。

于 2012-08-14T21:17:09.167 回答