随机数生成器android

时间:2015-02-25 12:01:17

标签: android random

ActivityMain.java

package com.abhinav.random;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

public int count=0;
public int rand_num;
public int n;
EditText edt_rand;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    rand_num=(int)(Math.random()*100);
    edt_rand=(EditText)findViewById(R.id.edt_rand);
    n = Integer.parseInt(edt_rand.toString());
}

public void onclick(View v){
        if(count!=5){
            if(n>rand_num){
                Toast toast=Toast.makeText(this,"Your number is greater", Toast.LENGTH_SHORT);
                toast.show();
            }
            if(n<rand_num){
                Toast toast=Toast.makeText(this,"Your number is greater", Toast.LENGTH_SHORT);
                toast.show();
            }
            else{
                new AlertDialog.Builder(this)
                .setTitle("Task complete")
                .setMessage("You Win")
                .setNeutralButton("Close", new                              DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub

                    }
                })
                .show();
            }
        }
        else{
            new AlertDialog.Builder(this)
            .setTitle("No Chances Left")
            .setMessage("You Lose")
            .setNeutralButton("Close", new DialogInterface.OnClickListener()       {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub

                }
            })
            .show();
        }

}

}

main_activity.xml

enter code here
 <EditText 
    android:id="@+id/edt_rand"
    android:hint="@string/number"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="number"
    ></EditText>

<Button
    android:id="@+id/btn_rand"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/edt_rand"
    android:layout_below="@+id/edt_rand"
    android:layout_marginTop="23dp"
    android:text="@string/click" 
    android:onClick="onclick"/>

上述代码有什么问题?

2 个答案:

答案 0 :(得分:0)

问题出现在onCreate()方法的下面;

n = Integer.parseInt(edt_rand.toString());

您实际上正在初始化edittext并直接从中获取值。在这种情况下, n 的值将为null或“”

修改您的代码,如下所示,

 public void onclick(View v){
         n = Integer.parseInt(edt_rand.getText().toString());  // write this line here
 ...
 }

答案 1 :(得分:0)

我有一个生成随机数的函数。

    public String getDynamic()
    {
    Random r = new Random();
    int i1 = r.nextInt(99 - 10) + 10;
    String privateString = String.valueOf(i1) ;
    return privateString;

    }