小工具知识网

当前位置: 首页 > 行业知识 > 考研失败, 学点Java打小工——Day3

行业知识

考研失败, 学点Java打小工——Day3

2026-12-01 05:51:00 来源:互联网转载

1 编码规范——卫语句

  表���异常分支时,少用if-else方式。

  比如成绩判断中对于非法输入的处理:

    /*    >=90   =80    =70    =60            //int score = 78;        //通过Scanner可以实现从控制台输入信息        Scanner scanner = new Scanner(System.in);        System.out.println("请输入成绩:");        int score = scanner.nextInt();        if(score = 70 && score = 60 && score  

  修改后:

    @Test    public void test2() {        //通过Scanner可以实现从控制台输入信息        Scanner scanner = new Scanner(System.in);        System.out.println("请输入成绩:");        int score = scanner.nextInt();        //卫语句1        if (score  0) {//异常和正常 要分开            System.out.println("Invalid input!");            //后面的代码不再执行            return;        }        //卫语句2...        //合法输入        if (score >= 90 && score             System.out.println("优秀");        } else if (score = 80 && score = 70 && score = 60 && score  

2 循环控制语句(接Day2)

 2.2 continue、break

   还是跟C语法相差无几,放个demo了事

   continue:跳出本次循环,继续下一次循环

   break:跳出离他最近的那层循环

@Test//结果: 1 2 4 5public void test44() {    for (int i = 1; i         if (i == 3) {            continue;        }        System.out.println(i);    }}@Test//结果: 1 2public void test45() {    for (int i = 1; i         if (i == 3) {            break;        }        System.out.println(i);    }}@Test//public void test46() {    //i,j,k    for (int i = 1; i         System.out.println("i: " + i);        for (int j = 1; j             if (j == 3) {                break;            }            System.out.println("j: " + j);        }    }}        Scanner scanner = new Scanner(System.in);        Homework day2 = new Homework();        System.out.println("please input month: ");        int month = scanner.nextInt();        if (month 

上一篇:怎么登录内置服务器端口?

下一篇:服务器+vs+云