循环结构二
for循环
for( 表达式1; 表达式2; 表示3){
//循环体
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
double sum = 0;//和
double min =Integer.MAX_VALUE;//整形的最大值
double max=Integer.MIN_VALUE;//整形的最小值
double avg = 0;//平均分
double temp = 0;//输入的分数
for (int i = 0; i < 5; i++) {
temp = s.nextFloat();
sum=sum+temp;
if (min > temp) {
min = temp;
}
if(max<temp){
max=temp;
}
break语句的使用
1. break
break语句的使用场合主要是switch语句和循环结构
在循环结构中使用break语句,如果执行了break语句,那么就退出循环,
for(int i =0; i<10; i++){
//跑400米
if(不能坚持){
break;
}
}
continue语句的使用
for(int i =0; i<10; i++){
//跑400米
if(不口渴){
continua;
}
}
import java.util.Scanner;
public class Student {
String name;
double score;
public static void main(String[] args) {
System.out.println(“请输入班级学生人数”);
Scanner sc = new Scanner(System.in);
int totalStudent = sc.nextInt();
Student[] stus = new Student[totalStudent];
for(int i=0;i<totalStudent;i++){
Student s = new Student();
stus[i]=s;
System.out.println(“请输入第”+(i+1)+“个学生的名字”);
s.name = sc.next();
System.out.println(“请输入第”+(i+1)+“个学生的分数”);
s.score = sc.nextDouble();
}
bubbleSort(stus);
printStudents(stus);
sc.close();
}
public static void bubbleSort(Student[] s){
for(int i=0;i<s.length-1;i++){
for(int j=i;j<s.length;j++){
if(s[i].score<s[j].score){
Student temp = s[i];
s[i]=s[j];
s[j]=temp;
}
}
}
}
public static void printStudents(Student[] s){
System.out.println("名次\t名字\t成绩");
for(int i=0;i<s.length;i++){
System.out.println(i+1+"\t"+s[i].name+"\t"+s[i].score);
}
}}