2.利用Java的多线程技术,编写一个即可以求20以内偶数和又可以求20以内偶数积 的程序。
1个回答
展开全部
我写了个,你参考下:
class changing{
class jishuhe extends Thread{
public void run() {
Integer sum = 0;
for (int i = 0; i < 20; i++) {
if( i%2 == 0 ){
sum = sum + i ;
}
}
System.out.println("20以内偶数和:"+ sum);
}
}
class jishuji extends Thread{
public void run() {
Integer sum = 1;
for (int i = 1; i < 20; i++) {
if( i%2 == 0 ){
sum = sum * i ;
}
}
System.out.println("20以内偶数积:"+ sum);
}
}
public static void main(String[] args) {
jishuhe he = new changing().new jishuhe();
he.start();
jishuji ji = new changing().new jishuji();
ji.start();
}
}
class changing{
class jishuhe extends Thread{
public void run() {
Integer sum = 0;
for (int i = 0; i < 20; i++) {
if( i%2 == 0 ){
sum = sum + i ;
}
}
System.out.println("20以内偶数和:"+ sum);
}
}
class jishuji extends Thread{
public void run() {
Integer sum = 1;
for (int i = 1; i < 20; i++) {
if( i%2 == 0 ){
sum = sum * i ;
}
}
System.out.println("20以内偶数积:"+ sum);
}
}
public static void main(String[] args) {
jishuhe he = new changing().new jishuhe();
he.start();
jishuji ji = new changing().new jishuji();
ji.start();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询