1.实验2 循环结构 、选择结构
题目:
- 输出1-100的所有偶数之和。
- 编程实现求三个数中的最大值。
- 编写程序,将百分制成绩转换为四级分制成绩。如果成绩的取值范围是[0,60),则打印输出“不及格”;如果取值范围是[60,75),则打印输出“及格”;如果取值范围是[75,90),则打印输出“良好”;如果取值范围是[90,100],则打印输出“优秀”。其他的取值范围均打印“该成绩不合法”。
- 编程求(结果是:0.688)
- 利用公式求π的近似值,直到最后一项的绝对值小于10-6为止。
四、程序源代码(核心)与实验结果
第1题:
int i=0;int sum=0
while (i<100){
i++;
if(i%2==1)
continue;sum=sum+i;
}System.out.print(sum);
第2题:
int []a=new int [3];
Scanner sc=new Scanner(System.in);
for(int i=0;i<3;i++){
System.out.print("请输入第"+(i+1)+"个数:");
a[i]=sc.nextInt();
}
if (a[0]>=a[1] && a[0]>=a[2]){
System.out.print(a[0]);
}
else if(a[1]>=a[2]){
System.out.print(a[1]);
}
else{
System.out.print(a[2]);
}
第3题:
Scanner sc=new Scanner(System.in);
System.out.print("请输入成绩:");
float cj=sc.nextFloat();
if(cj>=0 && cj<60){
System.out.print("不及格");
}
else if (cj>=60 && cj<75){
System.out.print("及格");
}
else if (cj>=75 && cj<90){
System.out.print("良好");
}
else if (cj>=90 && cj<=100){
System.out.print("优秀");
}
else{
System.out.print("该成绩不合法");
}
第4题:
double sum=0;
double a=0;
for (int i=1;i<=100;i++){
a=Math.pow(-1,i+1);
sum=sum+(a*(1.0/i));
}
System.out.print(String.format("%.3f",sum));
第5题:
double sum=0;
int i=1,f=-1;
while (1.0/i>=Math.pow(10,-6)){
f=-f;
sum=sum+f*(1.0/i);
i=i+2;
}
System.out.print(4*sum);