2021-03-16

Java学习(三)

第三章 Java进阶

3.1 流程控制

3.1.1 if-else

if(判断条件){

//条件成立代码

}else{

//条件不成立代码

}

3.1.2 多重if

if后面必须跟条件,else后面不能跟条件,可以跟if

3.1.3 if嵌套

例:if(判断条件){

​ if(判断条件){

​ //条件成立

​ }else{

​ //条件不成立

​ }

​ }

三目运算符:条件?成立:不成立

3.1.4 switch-case

switch(变量){

case 值1:

代码1;

break;

case 值2:

代码2;

break;

default:

代码n;

}

switch-case只能完成等值判断,支持类型有int、short、byte、char、枚举、String,不支持boolen。break用于将代码块分区,条件成立后,如无break,后续代码块执行。

3.2 循环

3.2.1 while循环

例:

package com.company;
import java.util.Scanner;

public class Test {
    public static void main(String[] args){
        System.out.println("学生成绩查询系统");

        Scanner scanner = new Scanner(System.in);
        int num;

        String str = "yes";

        while (str.equals("yes")){
            System.out.println("请输入学生编号:");
            num = scanner.nextInt();

            switch(num){
                case 1:
                    System.out.println("张三的成绩为90");
                    break;
                case 2:
                    System.out.println("李四的成绩为88");
                    break;
                default:
                    System.out.println("查无此人!");
                    break;
            }
            System.out.println("是否继续?yes/no:");
            str = scanner.next();
        }
        System.out.println("查询结束");
    }
}
3.2.2 do-while循环

例:

package com.company;
import java.util.Scanner;

public class Test {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        String str;
        do {
            System.out.println("张三参加体能测试,跑1000米......");
            System.out.println("是否合格?yes/no:");
            str = scanner.next();
        }while (str.equals("no"));
        System.out.println("合格,通过测试!");
    }
}
3.2.3 for循环

for(循环变量;循环条件;更新循环变量){/执行代码}

package com.company;
import java.util.Scanner;

public class Test {
    public static void main(String[] args){
        for(int i = 0;i<10;i++){
            System.out.println("Hello World!" + i);
        }
    }
}

3.2.4 while、do-while和for这3种循环的区别

相同点:循环四要素,即循环变量、循环条件、循环体、更新循环变量

不同点:while和do-while适用于循环次数不确定的场景,for适用于循环次数确定的场景。

while和for先执行循环条件,再执行循环体,do-while先执行循环体,再执行循环条件。

(System.out.print()表示不换行输出)

例:

package com.company;
import java.util.Scanner;

public class Test {
    public static void main(String[] args){
        int num = 0;
        //while
        while (num <= 10){
            if (num%2!=0){
                System.out.print(num+",");
            }
            num++;
        }
        System.out.println("");
//        do while
        int num1 = 0;
        do {
            if (num1%2!=0){
                System.out.print(num1+",");
            }
            num1++;
        }while (num1<=10);
        System.out.println("");
//        for
        for(int i = 0; i <= 10; i++){
            if (i%2!=0) {
                System.out.print(i+",");
            }
        }
    }
}

3.2.5 双重循环

例:

打印

0

1 2

2 3 4

3 4 5 6

2 3 4

1 2

0

public class Test {
    public static void main(String[] args){
        for(int j = 0; j <= 3; j++){
            for(int k = 0; k < 3-j; k++){
                System.out.print(" ");
            }
            for(int i = j; i < 2*j+1; i++){
                System.out.print(i+" ");
            }
            System.out.println("");
        }
        for(int j = 0; j < 3; j++){
            for(int k = 0; k < j+1; k++){
                System.out.print(" ");
            }
            for(int i = 2-j; i < 5-2*j; i++){
                System.out.print(i + " ");
            }
            System.out.println("");
        }
    }
}

例:

打印九九乘法表

public class Test {
    public static void main(String[] args){
        for(int i = 1; i <= 9; i++){
            for(int j = 1; j <= i; j++){
                System.out.print(i + "*" + j + "=" + i*j + "\t");
            }
            System.out.println("");
        }
    }
}
3.2.6 终止循环

break跳出整个循环,continue跳出本次循环。两者均适用于所用循环结构。

例:

public class Test {
    public static void main(String[] args){
        int sum = 0;
        for(int i = 1; i <= 100; i++){
            sum += i;
            System.out.println(sum);
            if(sum > 50){
                break;
            }
        }
    }
}

例:

public class Test {
    public static void main(String[] args){
        int sum = 0;
        for(int i = 1; i <= 200; i++){
            if(i%2!=0){
                sum += i;
            }else {
                continue;
            }
        }
        System.out.println(sum);
    }
}

3.3 数组

3.3.1 什么是数组

数组是一种可以存放大量数据类型相同的变量的数据结构。

3.3.2 数组的基本要素

构成:数组名称,数组元素,数组下标,数组类型

3.3.3 如何使用数组
  1. 声明数组:数据类型[] 数组名 或 数据类型 数组名[]
  2. 分配内存空间:数组名=new数据类型[数组长度]
  3. 赋值:数组名[数组序列号]=值
  4. 使用数组

例:

public class Test {
    public static void main(String[] args){
        int[] height;
        height = new int[4];
        //int[] height = new int[4];
        //int[] height = {179, 182, 167, 176};
        //int[] height = new int[]{179, 182, 167, 176};

        height[0] = 179;
        height[1] = 182;
        height[2] = 167;
        height[3] = 176;
        double sum = 0;
        for(int i = 1; i <= height.length; i++){
            sum += height[i - 1];
        }
        double avg = sum/height.length;
        System.out.println("四人的身高平均值为:"+avg);
    }
}

内存可以分为栈内存与堆内存,基本数据类型的变量和值都保存在栈内存中,引用数据类型的变量保存在栈内存中,变量的值保存在堆内存中,栈内存保存的是堆内存的地址。

常见错误:

  1. 数组声明是数据类型不匹配;
  2. 边声明边赋值必须写在同一行;
  3. 数组下标越界。
3.3.4 数组的常用操做及方法
  1. 求数组中的最大值;

  2. 求数组中的最小值;

    例:

public class Test {
    public static void main(String[] args){
        int [] nums = {455, 56, 57, 4687, 48};
        int max = nums[0];
        for(int i = 1; i < nums.length; i++){
            if(nums[i]>max){
                max = nums[i];
            }
        }
        System.out.println("最大值是"+max);
    }
}

3.在数组的指定位置插入一个数据;

例:

import java.util.Arrays;

public class Test {
    public static void main(String[] args){
        int[] nums = {455, 56, 57, 4687, 48};
        int score = 12;
        int[] nums2 = new int[nums.length+1];
        for(int i = 0; i<3; i++){
            nums2[i] = nums[i];
        }
        nums2[3] = score;
        for(int i = 4; i<nums2.length; i++){
            nums2[i] = nums[i-1];
        }
//        Arrays工具类的toString方法可以将数组的元素依次取出拼接成一个字符串
        System.out.println("添加新元素之前的数组:"+Arrays.toString(nums));
        System.out.println("添加新元素之后的数组:"+Arrays.toString(nums2));
    }
}

4.数组排序。

冒泡排序

import java.util.Arrays;

public class Test {
    public static void main(String[] args){
        int[] nums = {455, 56, 57, 4687, 48};
//        升序排列
        for(int j = 0; j<nums.length-1;j++){
            for(int i = 0; i < nums.length-1-j; i++){
                if(nums[i] >nums[i+1]){
                    int temp = nums[i];
                    nums[i] = nums[i+1];
                    nums[i+1] = temp;
                }
            }
        }
        System.out.println("升序排列:"+Arrays.toString(nums));
    }
}

import java.util.Arrays;

public class Test {
    public static void main(String[] args){
        int[] nums = {455, 56, 57, 4687, 48};
        int[] nums2 = {455, 56, 57, 4687, 48};
        int[] nums3 = {455, 56, 57, 4687, 48};

        System.out.println(Arrays.equals(nums, nums2));

        Arrays.sort(nums);
        System.out.println(Arrays.toString(nums));

        Arrays.fill(nums2, 56);
        System.out.println(Arrays.toString(nums2));

        int[] copyNums = Arrays.copyOf(nums3, 12);
        System.out.println(Arrays.toString(copyNums));

        int index = Arrays.binarySearch(nums, 57);
        System.out.println(index);
    }
}
3.3.5 二维数组
  1. 声明
  2. 开辟内存空间
  3. 赋值

public class Test {
    public static void main(String[] args){
        int[][] array = new int[3][6];
        int sum = 0;
        for(int i = 0; i<array.length; i++){
            for(int j = 0; j<array[i].length; j++){
                int num = (i+1)*(j+1);
                sum += num;
            }
        }
        System.out.println(sum);
    }
}

3.4 综合练习

例:用户管理系统

package com.company;
import java.util.Scanner;

public class Test {
    public static void main(String[] args){
//        初始化用户信息
        String[] nameArray = {"李四","张三","王五","小明"};
        int[] ageArray = {22, 23, 20, 19};
        String[] stateArray = {"正常","正常","正常","正常"};

        Scanner scanner = new Scanner(System.in);
        int num;

        do{
            System.out.println("欢迎使用用户管理系统");
            System.out.println("1.查询用户");
            System.out.println("2.添加用户");
            System.out.println("3.删除用户");
            System.out.println("4.账号冻结");
            System.out.println("5.账号解封");
            System.out.println("6.退出系统");
            System.out.print("请选择:");

            num = scanner.nextInt();

            switch (num){
                case 1:
                    System.out.println("——————查询用户——————");
                    System.out.println("编号\t\t名称\t\t年龄\t\t状态");
                    for(int i = 0; i < nameArray.length; i++){
                        if(nameArray[i] != null){
                            System.out.println((i+1) + "\t\t" + nameArray[i] +
                                    "\t\t" + ageArray[i] + "\t\t" + stateArray[i]);
                        }
                    }
                    System.out.print("输入0返回:");
                    num = scanner.nextInt();
                    break;

                case 2:
                    System.out.println("——————添加用户——————");
                    if(nameArray[nameArray.length-1] != null){
                        String[] newNameArray = new String[nameArray.length + 1];
                        int[] newAgeArray = new int[ageArray.length + 1];
                        String[] newStateArray = new String[stateArray.length + 1];
                        for(int i = 0; i < nameArray.length; i++){
                            newNameArray[i] = nameArray[i];
                            newAgeArray[i] = ageArray[i];
                            newStateArray[i] = stateArray[i];
                        }
                        nameArray = newNameArray;
                        ageArray = newAgeArray;
                        stateArray = newStateArray;
                    }
                    System.out.print("请输入用户名称:");
                    String name = scanner.next();
                    boolean flag = false;

//                    判断用户是否存在
                    for(int i = 0; i < nameArray.length; i++){
                        if(nameArray[i] != null && nameArray[i].equals(name)){
                            System.out.println(name + "已存在!");
                            flag = true;
                            break;
                        }
                    }
                    if(!flag){
                        System.out.println("请输入用户年龄:");
                        int age = scanner.nextInt();
                        nameArray[nameArray.length-1] = name;
                        ageArray[ageArray.length-1] = age;
                        stateArray[stateArray.length-1] = "正常";
                        System.out.println(nameArray.length + "\t\t" +
                                nameArray[nameArray.length-1] + "\t\t" + ageArray[ageArray.length-1] +
                                "\t\t" + stateArray[stateArray.length-1]);
                        System.out.println(name + "添加成功!");
                    }
                    System.out.print("输入0返回:");
                    num = scanner.nextInt();
                    break;

                case 3:
                    System.out.println("——————添加用户——————");
                    System.out.print("请输入用户名称:");
                    name = scanner.next();
                    boolean flag2 = false;

//                    判断用户是否存在
                    for (int i = 0; i < nameArray.length; i++){
                        if(nameArray[i] != null && nameArray[i].equals(name)){
                            flag2 = true;
                            if(i == nameArray.length-1){
                                nameArray[i] = null;
                                ageArray[i] = 0;
                                stateArray[i] = null;
                            }else {
                                for(int j = i; j < nameArray.length-1; j++){
                                    nameArray[j] = nameArray[j+1];
                                    ageArray[j] = ageArray[j+1];
                                    stateArray[j] = stateArray[j+1];

                                    nameArray[j+1] = null;
                                    ageArray[j+1] = 0;
                                    stateArray[j+1] = null;
                                }
                            }
                        }
                    }
                    if(!flag2){
                        System.out.println(name + "不存在,请重新输入!");
                    }else {
                        System.out.println(name + "删除成功!");
                    }
                    System.out.print("输入0返回:");
                    num = scanner.nextInt();
                    break;

                case 4:
                    System.out.println("——————账号冻结——————");
                    System.out.print("请输入用户名称:");
                    name = scanner.next();
                    boolean flag3 = false;
                    //                    判断用户是否存在
                    for (int i = 0; i < nameArray.length; i++){
                        if(nameArray[i] != null && nameArray[i].equals(name)){
                            flag3 = true;
                            if(stateArray[i].equals("冻结")){
                                System.out.println(name + "已冻结!");
                            }else {
                                stateArray[i] = "冻结";
                                System.out.println(name + "冻结成功!");
                            }
                            break;
                        }
                    }
                    if(!flag3){
                        System.out.println(name + "不存在,请重新输入!");
                    }
                    System.out.print("输入0返回:");
                    num = scanner.nextInt();
                    break;

                case 5:
                    System.out.println("——————账号解封——————");
                    System.out.print("请输入用户名称:");
                    name = scanner.next();
                    boolean flag4 = false;
                    //                    判断用户是否存在
                    for (int i = 0; i < nameArray.length; i++){
                        if(nameArray[i] != null && nameArray[i].equals(name)){
                            flag4 = true;
                            if(stateArray[i].equals("正常")){
                                System.out.println(name + "状态正常!");
                            }else {
                                stateArray[i] = "正常";
                                System.out.println(name + "解封成功!");
                            }
                            break;
                        }
                    }
                    if(!flag4){
                        System.out.println(name + "不存在,请重新输入!");
                    }
                    System.out.print("输入0返回:");
                    num = scanner.nextInt();
                    break;

                case 6:
                    System.out.println("感谢使用用户管理系统!");
                    return;
                default:
                    System.out.println("输入错误!请重新输入!");
                    System.out.print("输入0返回:");
                    num = scanner.nextInt();
                    break;
            }
        }while (num == 0);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
2021-03-26 20:54:33,596 - Model - INFO - Epoch 1 (1/200): 2021-03-26 20:57:40,380 - Model - INFO - Train Instance Accuracy: 0.571037 2021-03-26 20:58:16,623 - Model - INFO - Test Instance Accuracy: 0.718528, Class Accuracy: 0.627357 2021-03-26 20:58:16,623 - Model - INFO - Best Instance Accuracy: 0.718528, Class Accuracy: 0.627357 2021-03-26 20:58:16,623 - Model - INFO - Save model... 2021-03-26 20:58:16,623 - Model - INFO - Saving at log/classification/pointnet2_msg_normals/checkpoints/best_model.pth 2021-03-26 20:58:16,698 - Model - INFO - Epoch 2 (2/200): 2021-03-26 21:01:26,685 - Model - INFO - Train Instance Accuracy: 0.727947 2021-03-26 21:02:03,642 - Model - INFO - Test Instance Accuracy: 0.790858, Class Accuracy: 0.702316 2021-03-26 21:02:03,642 - Model - INFO - Best Instance Accuracy: 0.790858, Class Accuracy: 0.702316 2021-03-26 21:02:03,642 - Model - INFO - Save model... 2021-03-26 21:02:03,643 - Model - INFO - Saving at log/classification/pointnet2_msg_normals/checkpoints/best_model.pth 2021-03-26 21:02:03,746 - Model - INFO - Epoch 3 (3/200): 2021-03-26 21:05:15,349 - Model - INFO - Train Instance Accuracy: 0.781606 2021-03-26 21:05:51,538 - Model - INFO - Test Instance Accuracy: 0.803641, Class Accuracy: 0.738575 2021-03-26 21:05:51,538 - Model - INFO - Best Instance Accuracy: 0.803641, Class Accuracy: 0.738575 2021-03-26 21:05:51,539 - Model - INFO - Save model... 2021-03-26 21:05:51,539 - Model - INFO - Saving at log/classification/pointnet2_msg_normals/checkpoints/best_model.pth 我有类似于这样的一段txt文件,请你帮我写一段代码来可视化这些训练结果
最新发布
02-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HLY虎狼鹰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值