一般问题:我会在注释的哪里放置for循环?将范围内的所有素数加到数组中
深入问题:在Android Dev Studio的程序中,我设置了基本的“Hello World”程序(在文本框中键入消息,单击“发送”按钮,消息出现在下一个活动中)。
但是,我想将该程序调整为一个人可以在文本框中键入数字的程序,程序将找到1和输入数字之间的所有素数。
以下是我的代码到目前为止,与语法部分的评论我不知道如何格式化。
public class Main2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
int limit = Integer.parseInt(message);
int a[limit];
for(int i = 1; i <= limit; i++)
{
if(i%2 = 0 || i%3 = 0 || i%5 = 0 || i%7 = 0)
{
//I want the program to move on to the next number
}
else
{
//I want this number to be added into the array
}
}
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(a);
//a being the name of the array created
另一个问题我有将是程序将不承认1,2,3,5,和图7为素数,由于它们是由本身分割,因此具有余量是0.
有没有办法让在输入其余条款之前设置数组的一部分?如果没有,有没有一种方法可以编辑程序,以便这些数字看起来像素数?