80,349
社区成员
发帖
与我相关
我的任务
分享
public class MainActivity extends Activity implements OnClickListener {
EditText editText;
Button bt;
TextView textView,textView1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=(EditText)findViewById(R.id.edittext);
bt=(Button)findViewById(R.id.button);
textView=(TextView)findViewById(R.id.tv1);
textView1=(TextView)findViewById(R.id.tv2);
bt.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
String str=editText.getText().toString();
textView.setText(str+"以内的质数有:");
textView1.setText("2,3");
int n=Integer.parseInt(str);
int i=5;
int j=2;
while(i<=n)
{
while(j<i)
{
j=2;
if(i%j==0)
break;
if(i-1==j)
textView1.append(","+i);
j++;
}
i++;
}
break;
default:break;
}
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.prime.MainActivity"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="输入一个数:" />
<EditText
android:id="@+id/edittext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确认" />
</LinearLayout>
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
public class MainActivity extends Activity implements OnClickListener {
EditText editText;
Button bt;
TextView textView,textView1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=(EditText)findViewById(R.id.edittext);
bt=(Button)findViewById(R.id.button);
textView=(TextView)findViewById(R.id.tv1);
textView1=(TextView)findViewById(R.id.tv2);
bt.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
String str=editText.getText().toString();
textView.setText(str+"以内的质数有:");
textView1.setText("2,3");
try{
int n=Integer.parseInt(str);
int i=5;
int j;
while(i<=n)
{
j=2;
while(j<i)
{
if(i%j==0)
break;
if(i-1==j)
textView1.append(","+i);
j++;
}
i++;
}
}
catch (Exception e)
{
textView1.setText(e.toString());
}
break;
default:break;
}
}
}