1. Experiment 2 Cycle structure, selection structure
Topic:
- Output the sum of all even numbers from 1 to 100.
- Program to find the maximum of three numbers.
- Write a program to convert percentile grades into four-point grades. If the value range of the grade is [0,60), then print out "Fail"; if the value range is [60,75), then print out "Pass"; if the value range is [75,90), Then the printout is "good"; if the value range is [90,100], the printout is "excellent". For other value ranges, "The result is invalid" is printed.
- Programmatically seek (result: 0.688)
- Use the formula to find the approximate value of π until the absolute value of the last term is less than 10-6.
4. Program source code (core) and experimental results
Question 1:
int i=0;int sum=0
while (i<100){
i++;
if(i%2==1)
continue;sum=sum+i;
}System.out.print(sum);
Question 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]);
}
Question 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("该成绩不合法");
}
Question 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));
Question 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);