【问题标题】:Control random numbers控制随机数
【发布时间】:2013-12-17 12:38:23
【问题描述】:

您如何控制获得的随机数。例如,您将如何控制数字 10 - 20 的随机数生成器。我正在使用 eclipse,这是我的代码:

public class FrontPage extends Activity {
Random rndNumbers = new Random(); 
int d20    = rndNumbers.nextInt(20) +1; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_front_page);



            Button add = (Button) findViewById(R.id.button1);
            add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    int d20    = rndNumbers.nextInt(20) +1; 
                    EditText comment = (EditText) findViewById(R.id.editText1); 
                    Editable commy = comment.getText() ;
                    String  str =   commy.toString(); 
        TextView TV = (TextView) findViewById(R.id.textView1);
        TV.setText("" + str + d20);
        TV.setTextSize(d20);

                }
            });



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.front_page, menu);
    return true;
}

}

请帮忙。

【问题讨论】:

    标签: java android eclipse random textview


    【解决方案1】:

    你将如何控制数字 10 - 20 的随机数生成器

    创建一个介于 0 到 10 之间的随机数并将其加 10。

    int d20 = rndNumbers.nextInt(11)+10; 
    

    【讨论】:

      猜你喜欢
      • 2013-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-09
      • 2016-01-03
      • 1970-01-01
      相关资源
      最近更新 更多