hooyantsing's Blog

第5次课程

字数统计: 1.4k阅读时长: 7 min
2019/04/19

源辰74班

第5次课程

04.19.2019

内容

1.回顾

  1. 运算符

     赋值   =   +=  -=….     从右向左

     关系   > >=   < <=  !=    计算出boolean值

     逻辑   &&   ||   !    

             &&   ||   短路

             &&只要有一个操作数为 false ,则表达式的结果为false

             ||只要有一个操作数为true,则表达式结果为true

     表达式1?表达式2:表达式3   相当  if…else

     位:   ^   异或   

     ()   []   .   

  运算符优先级….

  1. 控制流语句

    条件 :   if … else

          switch…case

    循环:

            for    while    do..while

    中断:

            break    continue

  1. 输入输出,随机数:

   System.out.println();

   System.out.print()

   import java.util.Scanner;

   Scanner sc=new Scanner(System.in);    System.in: 系统标准输入流

   sc.nextLine();  //以换行符为结束

   sc.nextInt();   //以空格为结束

   sc.nextDouble();

   …

   随机数:

   import java.util.Random;

   Random r=new Random();

   r.nextInt();   //生成整数

   r.nextInt( range );    //  [0, range)

  1. 案例:

    生成牌: (  如何抽取数据,每次抽取的不同,且算法复杂度:  N )

    闰平年:

    每个月多少天.

2.eclipse软件

public static void main(String[] args) {}

快捷键:main    alt+/

System.out.println(“”);

快捷键:syso    alt+/

调入包

快捷键:(将光标放在类名上)    ctrl+1

快速复制一行

快捷键:ctrl+alt+向下键

快速删除一行

快捷键:ctrl+D

代码自动对齐

快捷键:ctrl+shift+f

3.eclipse外观设置

视图框意外关闭,恢复方法:窗口->perspective->复位透视图

字体及外观设置:窗口->首选项->常规->外观

4.[项目]税收问题(if用法)

实验题:

1
2
3
常量的定义
private static final double  SINGLE_BRACKET1 = 21450;
私有    静态          数据类型 常量名(大写字母)= 值 ;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package Test0419;
import java.util.*;
public class tax {
    private static final double RATE1 =  0.15;
    private static final double RATE2 =  0.28;
    private static final double RATE3 =  0.31;
    private static final double  SINGLE_BRACKET1 = 21450;
    private static final double  SINGLE_BRACKET2 = 51900;
    private static final double  MARRIED_BRACKET1 = 35800;
    private static final double  MARRIED_BRACKET2 = 86500;
    public static void main(String[] args) {
       Scanner in = new Scanner(System.in);
       System.out.println("请输入您的收入:");
       double income = in.nextDouble();
       System.out.println("请输入婚姻状态(S  单身/M 已婚):");
       String input = in.next();
       double tax = 0;
       if (input.equals("S")) {
           if (income <= SINGLE_BRACKET1) {
               tax = RATE1 * income;
           } else if (income <=  SINGLE_BRACKET2) {
               tax = RATE1 * SINGLE_BRACKET1  + RATE2 * (income - SINGLE_BRACKET1);
           } else
               tax = RATE1 * SINGLE_BRACKET1  + RATE2 * (SINGLE_BRACKET2 -  SINGLE_BRACKET1)
                      + RATE3 * (income -  SINGLE_BRACKET2);
       } else if (input.equals("M")) {
           if (income <= MARRIED_BRACKET1) {
               tax = RATE1 * income;
           } else if (income <=  MARRIED_BRACKET2) {
               tax = RATE1 *  MARRIED_BRACKET1 + RATE2 * (income -  MARRIED_BRACKET1);
           } else
               tax = RATE1 *  MARRIED_BRACKET1 + RATE2 * (MARRIED_BRACKET2  - MARRIED_BRACKET1)
                      + RATE3 * (income -  MARRIED_BRACKET2);
       }
       System.out.println("所应缴的税款为:"+tax);
    }
}

5.do…while循环用法

菜单:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package Test0419;
import java.util.*;
public class dowhile {
    public static void main(String[] args) {
       Scanner sc=new Scanner(System.in);
       
       boolean flag = true;
       do {
           System.out.println("===========学生管理系统============");
           System.out.println("1.登陆");
           System.out.println("2.退出");
            System.out.println("================================");
           int choice = sc.nextInt();
           sc.nextLine();
           if(choice==1) {
               System.out.println("登陆服务器繁忙,请稍后......");
               System.out.println("按回车键继续......");
               sc.nextLine();
           }else
               if(choice==2) {
                  System.out.println("已安全退出程序");
                  flag = false;
               }
           
       /*while内
        *flag==true时,满足循环。
        *flag==false时,跳出循环。*/  
       }while( flag );
       
    }
}

九九乘法表:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package Test0419;
public class jiujiuchengfabiao {
    public static void main(String[] args) {
       int i = 1;
       do {
           int j = 1;
           do {
               System.out.print(i+"*"+j+"="+i*j);
               j++;
           }while(j<=i);
           System.out.println();
           i++;
       }while(i<=9);
    }
}

6.for循环用法

九九乘法表:

1
2
3
4
5
6
7
8
9
10
11
12
package Test0419;
public class jiujiuchengfabiaofor {
    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);
           }
           System.out.println();
       }
       
    }
}

转存html文件

找到该源码文件(java)所生成的字节码文件(class),然后通过”>.html”转存为网页文件(html)。// 通过”>.txt”转存为文档文件(txt)。

48592b2240a5f5fc3cdc34022657b1fe.png

九九乘法表(加入html换行标签):

1
2
3
4
5
6
7
8
9
10
11
//package Test0419;
public class jiujiuchengfabiaofor {
    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);
           }
           System.out.println("<br/>");
       }
    }
}

效果图如下:

7fe14aa1d780923b988841ec743a8171.png

7.for循环例题

星号三角例题

**2019-04-20_132730.png

**

解体源码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package Test0419;
import java.util.Scanner;
public class xingxingta1 {
    public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("请输入行数:");
       int n = sc.nextInt();

       System.out.println("例题一");
       for (int i = 1; i <= n; i++) {
           for (int j = 1; j <= i; j++) {
               System.out.print("*");
           }
           System.out.println();
       }
       // System.out.println();

       System.out.println("例题二");
       /*
        * 图解:         图 星 空 行i
        *    *      1 4 1
        *   ***     3 3 2
        *  *****    5 2 3
        * *******   7 1 4
        **********  9 0 5
        *
        * 规则:
        * 空j:n-行 -> n-i
        * 星j:行*2-1 -> i*2-1
        */
       for (int i = 1; i <= n; i++) {
           // 打印空格
           for (int j = 1; j <= n - i; j++)  {
               System.out.print(" ");
           }
           // 打印星号
           for (int j =1; j <=i*2-1 ; j++) {
               System.out.print("*");
           }
           // 换行
           System.out.println();
       }

       System.out.println("例题三");
       for (int i = 1; i <= n; i++) {
           // 打印空格
           for (int j = 1; j <= n - i; j++)  {
               System.out.print(" ");
           }
           // 打印星号
           for (int j = 1; j <= i * 2 - 1;  j++) {
               if (j == 1 || j == i * 2 - 1  || i == n) {
                  System.out.print("*");
               } else {
                  System.out.print(" ");
               }
           }
           // 换行
           System.out.println();
       }
       
       System.out.println("例题四");
       for (int i = 1; i <= n; i++) {
           for (int j = 1; j <= n - i; j++)  {
               System.out.print(" ");
           }
           for (int k = 1; k <= i * 2 - 1;  k++) {
               System.out.print("*");
           }
           System.out.println();
       }
       for (int i = 1; i <= n; i++) {
           for (int k = 1; k <= i; k++) {
                  System.out.print(" ");
           }
           for (int j =1; j <=2*(n-i)-1 ;  j++) {
                  System.out.print("*");
           }
           System.out.println();
       }
    }
}
CATALOG