顺序结构
java按代码顺序一句一句往下执行
if 单选择结构
- package code_learn_day5;
- import java.util.Scanner;
- // if 单选择结构
- public class If_01 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner((System.in));
- System.out.println(“请输入内容:”);
- String s = scanner.nextLine();
- // euals判断字符是否相等
- if (s.equals(“Hello”)){
- System.out.println(s);
- }
- System.out.println(“End”);
- scanner.close();
- }
- }
if 双选择结构
- package code_learn_day5;
- import java.util.Scanner;
- public class if_02 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- System.out.println(“请输入成绩:”);
- int score = scanner.nextInt();
- if (score>60) {
- System.out.println(“及格”);
- }else{
- System.out.println(“不及格”);
- }
- }
- }
还有多选择结构 elseif
每个if语句都必须在最后输入else。
switch选择结构
判断一个变量与某个特征值是否相等
变量可以为byte\shot\int\string或者char
可以用break语句来中止switch语句
- package code_learn_day5;
- public class switch_01 {
- public static void main(String[] args) {
- //
- char grade = ‘c’;
- switch (grade){
- case ‘a’:
- System.out.println(“Great”);
- break;
- case ‘b’:
- System.out.println(“good”);
- break;
- case ‘c’:
- System.out.println(“well done”);
- break
- default:
- System.out.println(“unknown”);
- }
- }
- }
switch语句可能会发生case穿透现象,请注意break语句
掌握编译与反编译
循环结构
while \ do…while \ for
注意do…while的特点,程序会先进入循环,再进行判断
for循环,代码1,简单的顺序显示数字
- package code_learn_day5;
- public class for_01 {
- public static void main(String[] args) {
- int a = 1;// 初始化条件
- while (a <= 100) {// 条件判断
- System.out.println(a);// 循环体
- a += 2;// 迭代
- }
- System.out.println(“end”);
- int i;
- for (i=1;i<=100;i++);{
- System.out.println(i);// 为什么在for循环里定义i的话,这行的i是红色的???
- }
- System.out.println(“end”);
- }
- }
for循环,code2,计算0-100奇数、偶数的和
- package code_learn_day5;
- public class for_03 {
- public static void main(String[] args) {
- // 0-100奇数、偶数的和
- int oddSum = 0;
- int evenSum = 0;
- for (int i = 0; i <= 100; i ++){
- if (i%2 != 0) { // %是mod运算,奇数
- oddSum += i;
- }else{
- evenSum += i;
- }
- }
- System.out.println(“odd” + oddSum);
- System.out.println(“even” + evenSum);
- }
- }
for循环,code3、4,输出1000内能被5整除的数,并且每行输出3个
我的代码
- package code_learn_day5;
- public class for_02 {
- public static void main(String[] args){
- for (int i = 1; i <= 1000; i ++){
- if (i%5 == 0){
- if (i%3 != 0) {
- System.out.print(i + “ “);
- }else{
- System.out.println(i);
- }
- }
- }
- }
- }
up代码
- package code_learn_day5;
- public class for_04 {
- public static void main(String[] args) {
- for (int i = 1; i <= 1000; i ++){
- if (i%5 == 0){
- System.out.println(i + “\t”);
- }
- if(i%(5*3) == 0){
- System.out.println();
- //same as System.out.println(“/n”)
- }
- }
- }
- }
问题:为什么数据结果是竖着3个在一起,\t有问题??
打印九九乘法表
我的代码:
- package code_learn_day5;
- public class for_05 {
- public static void main(String[] args) {
- for (int i = 1; i < 10; i++) {
- for (int i1 = 1; i1 <= i; i1++) {
- if (i1 != i) {
- System.out.print(i + “X” + i1 + “=” + i*i1 + “\t”);
- } else {
- System.out.println(i + “X” + i1 + “=” + i*i1 );
- }
- }
- }
- }
- }
up的代码跟我的代码差不多
增强型的for循环,意思就是定义一个数组,i的值在里面取值
- package code_learn_day5;
- public class for_06_power {
- public static void main(String[] args) {
- int[] numbers = {10,20,30,40,50};// 定义一个数组
- for (int i = 0; i < 5; i++){
- System.out.println(numbers[i]);
- }
- //遍历数组的元素
- for (int x:numbers){
- System.out.println(x);
- }
- }
- }
break和continue
break会中止代码块的执行
- package code_learn_day5;
- public class break_continue_01 {
- public static void main(String[] args) {
- int i = 0;
- while (i<100){
- i++;
- if (i == 30) {
- break;
- }
- }
- }
- }
这个循环输出到30就不继续运行了,但是整个程序不会结束
continue会中止当前循环,但是不会中止整个循环,会继续下一个循环
- package code_learn_day5;
- public class break_continue_01 {
- public static void main(String[] args) {
- int i = 0;
- while (i < 100) {
- System.out.println(i);
- i++;
- if (i == 30) {
- System.out.println(i);
- continue;
- }
- }
- }
- }
这个代码会输出两次30
画三角形
- package code_learn_day5;
- public class triangle_01 {
- public static void main(String[] args) {
- // 打印三角形
- for (int i = 1; i <= 5; i++) {
- for (int j = 5; j >= i; j—) {
- System.out.print(“ “);
- }
- for (int j = 1; j <= i; j++) {
- System.out.print(“*”);
- }
- for (int j = 1; j <= i - 1; j++) {
- System.out.print(“*”);
- }
- System.out.println();
- }
- }
- }
一行一行更新,要理清楚这个逻辑
ended at No.44
to be continued…