Java 安卓随机数组
我想更改代码,这样每次单击“下一个数字”按钮时,它都会生成一个随机数。生成最后一个数字时,应用程序将进行排序,但最后一个数字除外。生成最后一个数字后,单击按钮将没有响应。您需要单击“清除”按钮重新启动。没有重复出现。 这是我的密码 MainActivity.javaJava 安卓随机数组,java,android,random,Java,Android,Random,我想更改代码,这样每次单击“下一个数字”按钮时,它都会生成一个随机数。生成最后一个数字时,应用程序将进行排序,但最后一个数字除外。生成最后一个数字后,单击按钮将没有响应。您需要单击“清除”按钮重新启动。没有重复出现。 这是我的密码 MainActivity.java public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) {
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
int Count=0;
public void generate(View v){
Random myRandom = new Random();
TextView tv_number_one = (TextView)findViewById(R.id.tv_number_one);
TextView tv_number_two = (TextView)findViewById(R.id.tv_number_two);
TextView tv_number_three = (TextView)findViewById(R.id.tv_number_three);
TextView tv_number_four = (TextView)findViewById(R.id.tv_number_four);
TextView tv_number_five = (TextView)findViewById(R.id.tv_number_five);
TextView tv_number_six = (TextView)findViewById(R.id.tv_number_six);
TextView tv_number_seven = (TextView)findViewById(R.id.tv_number_seven);
String ran=String.valueOf(myRandom.nextInt(50));
switch(Count%7){
case 0:
tv_number_one.setText(ran);
break;
case 1:
tv_number_two.setText(ran);
break;
case 2:
tv_number_three.setText(ran);
break;
case 3:
tv_number_four.setText(ran);
break;
case 4:
tv_number_five.setText(ran);
break;
case 5:
tv_number_six.setText(ran);
break;
case 6:
tv_number_seven.setText(ran);
break;
}
Count++;
}
public void clear (View v){
TextView tv_number_one = (TextView)findViewById(R.id.tv_number_one);
TextView tv_number_two = (TextView)findViewById(R.id.tv_number_two);
TextView tv_number_three = (TextView)findViewById(R.id.tv_number_three);
TextView tv_number_four = (TextView)findViewById(R.id.tv_number_four);
TextView tv_number_five = (TextView)findViewById(R.id.tv_number_five);
TextView tv_number_six = (TextView)findViewById(R.id.tv_number_six);
TextView tv_number_seven = (TextView)findViewById(R.id.tv_number_seven);
tv_number_one.setText("?");
tv_number_two.setText("?");
tv_number_three.setText("?");
tv_number_four.setText("?");
tv_number_five.setText("?");
tv_number_six.setText("?");
tv_number_seven.setText("?");
}
}
主要活动
<TextView android:text="\?" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_number_one"
android:textSize="30sp"
android:textIsSelectable="true"
android:layout_marginLeft="10dp"
android:textColor="#67ceff"
android:layout_marginStart="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="\?"
android:id="@+id/tv_number_two"
android:layout_alignBottom="@+id/tv_number_one"
android:layout_toRightOf="@+id/tv_number_one"
android:layout_toEndOf="@+id/tv_number_one"
android:textColor="#67ceff"
android:textSize="30sp"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="\?"
android:id="@+id/tv_number_three"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/tv_number_two"
android:layout_toEndOf="@+id/tv_number_two"
android:layout_marginLeft="20dp"
android:textColor="#67ceff"
android:textSize="30sp"
android:layout_marginStart="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="\?"
android:id="@+id/tv_number_four"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/tv_number_three"
android:layout_toEndOf="@+id/tv_number_three"
android:textColor="#67ceff"
android:textSize="30sp"
android:textIsSelectable="true"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="\?"
android:id="@+id/tv_number_five"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/tv_number_four"
android:layout_toEndOf="@+id/tv_number_four"
android:layout_marginLeft="20dp"
android:textColor="#67ceff"
android:textSize="30sp"
android:layout_marginStart="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="\?"
android:id="@+id/tv_number_six"
android:layout_alignBottom="@+id/tv_number_five"
android:layout_toRightOf="@+id/tv_number_five"
android:layout_toEndOf="@+id/tv_number_five"
android:textColor="#67ceff"
android:textSize="30sp"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="\?"
android:id="@+id/tv_number_seven"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/tv_number_six"
android:layout_toEndOf="@+id/tv_number_six"
android:layout_marginLeft="20dp"
android:textColor="#6198ff"
android:textSize="30sp"
android:layout_marginStart="20dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NEXT NUMBER"
android:id="@+id/button_next_num"
android:layout_below="@+id/tv_number_one"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="41dp"
android:onClick="generate"/>
有人知道怎么做吗?只需在按钮的
onclickListener事件中添加代码,并在计数达到7时在onclickListener中设置计数器整数,然后停止。代码如下-
public class MainActivity extends AppCompatActivity {
int intCount=0;
int[] arr = new int[7];
final Button nextButton;
Random randomGenerator = new Random();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nextButton = (Button)findviewbyid(R.id.button_next_num)
TextView tv_number_one = (TextView)findViewById(R.id.tv_number_one);
TextView tv_number_two = (TextView)findViewById(R.id.tv_number_two);
TextView tv_number_three = (TextView)findViewById(R.id.tv_number_three);
TextView tv_number_four = (TextView)findViewById(R.id.tv_number_four);
TextView tv_number_five = (TextView)findViewById(R.id.tv_number_five);
TextView tv_number_six = (TextView)findViewById(R.id.tv_number_six);
TextView tv_number_seven = (TextView)findViewById(R.id.tv_number_seven);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
intCount++; //when intCount becomes 7 stop execution
arr[intCount]= randomGenerator.nextInt(100) + 1;;
// intCount++; //when intCount becomes 7 stop execution
if(intCount == 1){tv_number_one.setText(arr[intCount]);}
else if(intCount == 2){tv_number_two.setText(arr[intCount]);}
else if(intCount == 3){tv_number_three.setText(arr[intCount]);}
else if(intCount == 4){tv_number_four.setText(arr[intCount]);}
else if(intCount == 5){tv_number_five.setText(arr[intCount]);}
else if(intCount == 6){tv_number_six.setText(arr[intCount]);}
else if(intCount == 7){tv_number_seven.setText(arr[intCount]);}
else{ intCount=0;nextButton.setEnabled(false);}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
是的,您的代码将不会运行,因为您创建了一个大小为2的数组arr,但如果您尝试在该数组中获取7个元素,它将抛出一个名为ArrayIndexOutofBond的异常。因为声明的数组有2个元素,正在尝试检索更多元素。为什么不能直接获取随机值并添加到文本视图
int clickCount = 0;
public void generate(View v) {
Random myRandom = new Random();
if(clickCount >= 7) return;
TextView tv_number_one = (TextView)findViewById(R.id.tv_number_one);
TextView tv_number_two = (TextView)findViewById(R.id.tv_number_two);
TextView tv_number_three = (TextView)findViewById(R.id.tv_number_three);
TextView tv_number_four = (TextView)findViewById(R.id.tv_number_four);
TextView tv_number_five = (TextView)findViewById(R.id.tv_number_five);
TextView tv_number_six = (TextView)findViewById(R.id.tv_number_six);
TextView tv_number_seven = (TextView)findViewById(R.id.tv_number_seven);
String randStr = String.valueOf(myRandom.nextInt(48));
switch(clickCount % 7) {
case 0:
tv_number_one.setText(randStr);
break;
case 1:
tv_number_two.setText(randStr);
break;
case 2:
tv_number_three.setText(randStr);
break;
case 3:
tv_number_four.setText(randStr);
break;
case 4:
tv_number_five.setText(randStr);
break;
case 5:
tv_number_six.setText(randStr);
break;
case 6:
tv_number_seven.setText(randStr);
break;
}
clickCount++;
}
还有一件事,String.valueOf()
方法将返回String
,因此在此之前无需附加空字符串。执行问题是什么?告诉我们你期望什么,得到什么。“为什么它不能运行”??这还不足以回答这个问题。你应该说什么是问题你创建了一个大小为2的数组,然后访问索引7?这是行不通的。@TDG它显示了这样一个会议:E/Surface﹕ getSlotFromBufferLocked:未知缓冲区:0xa1a52cf0@sje397我已经更改了数组大小。它仍然无法工作。从未使用在count++时更改的值。我该怎么办呢?intCount只是一个整数变量,您可以将它添加到类内部以及onCreat()方法as-public class MainActivity扩展AppCompatActivity之前{int intCount=0;嘿,JOEY comment,如果这段代码对你有效…使用这段代码,它肯定有效,只是复制粘贴,因为它担心回复太晚。我在过去几个小时内一直在工作。这很有效,谢谢。但是,当生成7个数字时,如何停止生成?我想知道如何不重复地生成,并在最后一个数字时对它们进行排序当计数变为7时,只需通过-nextButton.setEnabled(false)禁用按钮即可;我已更改了数组大小,谢谢。但是,如果我只删除String.valueOf(),它会工作吗?用你的函数替换我的函数,它会工作的谢谢你的帮助。但我希望用户每次点击“下一个号码"按钮,它会生成一个随机数。我不知道怎么做。是的,它会生成7个随机数,并放置在相应的文本视图中,当用户单击按钮时,将您的代码替换为我的代码并运行一次。让我知道状态。但结果是同时生成7个随机数。我想生成1个单击按钮一次即可进行编号。