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 && score2 循环控制语句(接Day2)
2.2 continue、break
还是跟C语法相差无几,放个demo了事
continue:跳出本次循环,继续下一次循环
break:跳出离他最近的那层循环
@Test//结果: 1 2 4 5 public void test44() { for (int i = 1; i if (i == 3) { continue; } System.out.println(i); } } @Test//结果: 1 2 public 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