javaSE-day02--循环控制、数组、冒泡排序、初识类和对象

    科技2025-05-05  11

    循环控制

    while循环

    package day02; import java.util.Scanner; public class While { public static void main(String[] args) { int num = 9; boolean aa = true; while (aa) { System.out.println("输入一个数字:"); Scanner a = new Scanner(System.in); String gussnum = a.nextLine(); int gussnumber = Integer.parseInt(gussnum); if (gussnumber > num) { System.out.println("输入的数字大了,重新输入。"); } else if (gussnumber < num) { System.out.println("输入的数字小了,重新输入。"); } else { System.out.println("猜对了。"); aa = false; } } System.out.println("游戏结束"); } }

    总结:

    需要用boolean值来控制while的结束,或者用break

    for循环

    package day02; import java.util.Scanner; public class For { public static void main(String[] args) { int num = 9; for (int i = 0; i < 100; i++) { System.out.println("输入一个数字:"); Scanner a = new Scanner(System.in); String gussnum = a.nextLine(); int gussnumber = Integer.parseInt(gussnum); if (gussnumber > num) { System.out.println("输入的数字大了,重新输入。"); } else if (gussnumber < num) { System.out.println("输入的数字小了,重新输入。"); } else { System.out.println("猜对了。"); i = 100; } } System.out.println("游戏结束。"); } }

    总结:

    可以改变i的值来结束循环 或者用break;

    数组

    package day02; import java.util.Scanner; public class Array { public static void main(String[] args) { Scanner a = new Scanner(System.in); double[] scores= new double[5]; for(int i=0;i<5;i++) { System.out.println("输入一个成绩:"); String num = a.nextLine(); double score = Double.parseDouble(num); scores[i] = score; } //成绩求和 double ab = 0; for(int i=0;i<5;i++) { ab += scores[i]; } System.out.println("总成绩:" + ab); System.out.println("平均成绩:" + ab/5); } }

    总结:

    定义数组:double[] scores= new double[5]; int[] scores = new int[5]; String[] scores = new String[5];字符串到数字的强制类型转换: double score = Double.parseDouble(num); int score = Integer.parseInt(num); float score = Float.parseFloat(num); 遍历数组 package day02; public class ArrayDemo { public static void main(String[] args) { int[] arr = new int[10]; for(int i=0;i<10;i++) { arr[i] = i; } //倒序遍历数组 for(int i=arr.length-1;i>=0;i--) { System.out.println(i); } } }

    总结:

    计算数组长度:arr.length;

    求最大值和最小值

    package day02; public class MaxMin { public static void main(String[] args) { int[] arr = new int[5]; arr[0] = 42; arr[1] = 2; arr[2] = 88; arr[3] = 56; arr[4] = 545; int temp = arr[0]; for (int i = 0; i < arr.length; i++) { if (arr[i] > temp) { temp = arr[i]; } } System.out.println("最大值为:" + temp); for (int i = 0; i < arr.length; i++) { if (arr[i] < temp) { temp = arr[i]; } } System.out.println("最小值为:" + temp); } }

    冒泡排序

    package day02; public class Mao { public static void main(String[] args) { int a[] = new int[5]; a[0] = 12; a[1] = 15; a[2] = 62; a[3] = 58; a[4] = 42; //表示的是从头到尾的次数 for (int j = 0; j < a.length - 1; j++) { //表示的是从头到尾一次比较的次数 for (int i = 0; i < a.length - 1 - j; i++) { if (a[i] > a[i + 1]) { int tem = a[i]; a[i] = a[i + 1]; a[i + 1] = tem; } } } for(int i = 0;i<a.length;i++) { System.out.println(a[i]); } } }

    初识类和对象

    package day02; public class StudentInfo { String name; String ID; int age; double score; } package day02; import java.util.Scanner; public class StudentDemo { public static void main(String[] args) { StudentInfo []students = new StudentInfo[2]; Scanner input = new Scanner(System.in); StudentInfo stu1 = new StudentInfo(); System.out.println("输入第一个学生的信息:"); stu1.name = input.nextLine(); stu1.ID = input.nextLine(); stu1.age = input.nextInt(); stu1.score = input.nextDouble(); Scanner input2 = new Scanner(System.in); StudentInfo stu2 = new StudentInfo(); System.out.println("输入第二个学生的信息:"); stu2.name = input2.nextLine(); stu2.ID = input2.nextLine(); stu2.age = input2.nextInt(); stu2.score = input2.nextDouble(); //排序,将分数高的放到前边 if(stu1.score > stu1.score) { students[0] = stu1; students[1] = stu2; } else { students[0] = stu2; students[1] = stu1; } System.out.println("姓名:"+students[0].name+","+"学号:"+students[0].ID+","+"年龄:"+students[0].age+","+"分数:"+students[0].score); System.out.println("姓名:"+students[1].name+","+"学号:"+students[1].ID+","+"年龄:"+students[1].age+","+"分数:"+students[1].score); } }

    总结:

    类:一个用来封装多个数据的模板。是一种特殊的数组,在使用上类似,可以有不同的数据类型。对象:根据类模板创建出来的一套具体数据(可以用该类型的变量来引用)**
    Processed: 0.016, SQL: 8