1、计算 2+4+6+8+……+100的值
![](https://i-blog.csdnimg.cn/blog_migrate/00f959302218a57f4772b3a36ebb186e.png)
运行结果:
![](https://i-blog.csdnimg.cn/blog_migrate/91a91dbef55ee99c7a02d12d2683fdb4.png)
代码:
package zzz;
public class Yml {
public static void main(String[] args) {
// TODO Auto-generated method stub
int x = 2;
int sum = 0;
while(x<=100) {
sum=sum+x;
x=x+2;
}
System.out.print(sum);
}
}
2、输入十个数,并求这十个数的乘积
package zzz;
import java.util.Scanner;
public class Yml {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.print("输入10个数字,用空格隔开:");
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int d = sc.nextInt();
int e = sc.nextInt();
int f = sc.nextInt();
int g = sc.nextInt();
int h = sc.nextInt();
int i = sc.nextInt();
int j = sc.nextInt();
System.out.print("十个数的积为:"+(a*b*c*d*e*f*g*h*i*j));
sc.close();
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/b811fc7270cf671e3f8b5f4367653bed.png)
结果如下:
![](https://i-blog.csdnimg.cn/blog_migrate/e7b2aa94f5d73340c66f648de84fdfa1.png)
3、要求按序输出 0-9 之间的整数数字,但是不能包含数字 6
package zzz;
public class Yml {
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i = 0;i <= 9;i++) {
if(i==6) {
continue;
}
System.out.println(i);
}
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/0e1cbd7ac77f582d0cada8b21a266516.png)
结果如下:
![](https://i-blog.csdnimg.cn/blog_migrate/35b4ee82530e6267bc47990ae93501e1.png)
4输入一个正整数 n,然后打印出1到n 中的所有素数
package zzz;
import java.util.Scanner;
public class Yml {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.print("输入一个数:");
int n = sc.nextInt();
int i,j;
for(i=1;i<(n+1);i++) {
for(j=2;j<i;j++) {
if(i%j==0) {
break;
}
}
if(j==i) {
System.out.println(i);
}
}
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/b6c84f19c3326e91670dda82bf5c42a8.png)
结果如下:
![](https://i-blog.csdnimg.cn/blog_migrate/eb95a8533a3e75f351b573282285704f.png)