Java 笔记

Java 课堂笔记

  1. 计算3/2的值
  2. 计算3/0的值
  3. 计算3.0/0的值
  4. 计算-3/2的值
  5. 计算-3/-2的值
  6. 就按3/-2的值
  7. 计算0-0.0的值
  8. 就按-3/0.03的值
  9. 回顾一下三目运算符
  10. 定义一个整数变量,判断这个数是一个奇数还是偶数
  11. 定义一个变量表示分数,分数>=90 -A, >=80 -B, >=70 -C, >=60 -D, <60 -E
  12. 定义一个变量表示年份,判断这一年是闰年还是平年
  1 import java.util.*;
  2 public class A
  3 {
  4     public static void main(String[] args)
  5     {
  6         System.out.println("-----------------------------------------");
  7         int a = 3/2;
  8         double b = (double)3/0;
  9         double c = (double)3.0/0;
 10         int d = -3/2;
 11         int e = -3/-2;
 12         int f = 3/-2;
 13         double g = 0/0.0;
 14         double h = -3/0.03;
 15         System.out.println("3/2 is: " + a);
 16         System.out.println("3/0 is: " + b);
 17         System.out.println("3.0/0 is: " + c);
 18         System.out.println("-3/2 is: " + d);
 19         System.out.println("-3/-2 is: " + e);
 20         System.out.println("3/-2 is: " + f);
 21         System.out.println("0/0.0 is: " + g);
 22         System.out.println("-3/0.03 is: " + h);
 23 
 24         
 25         System.out.println("-----------------------------------------");
 26         System.out.println("Use ternary operator:");
 27         int m1 = 1;
 28         int m2 = 2;
 29         int min = (m1 < m2) ? m1:m2;
 30         System.out.println("min is: " + min);
 31     
 32         System.out.println("-----------------------------------------");
 33         System.out.println("Please input a number:");
 34         Scanner sc = new Scanner(System.in);
 35         int s1 = sc.nextInt();
 36         if(s1 % 2 == 0) System.out.println(s1 + " is a even number.");
 37         else System.out.println(s1 + " is a uneven number.");
 38     
 39         
 40         System.out.println("-----------------------------------------");
 41         System.out.println("Please input your score:");
 42         Scanner sc2 = new Scanner(System.in);
 43         int s2 = sc2.nextInt();
 44         int temp = 0;
 45         if(s2 >= 90) temp = 1;
 46         if((s2 >= 80) && (s2 <= 90)) temp = 2;
 47         if((s2 >= 70) && (s2 <= 80)) temp = 3;
 48         if((s2 >= 60) && (s2 <= 70)) temp = 4;
 49         if(s2 < 60) temp = 5;
 50         switch (temp) {
 51             case 1:
 52                 System.out.println("You got A!");
 53                 break;
 54 
 55             case 2:
 56                 System.out.println("You got B!");
 57                 break;
 58 
 59             case 3:
 60                 System.out.println("You got C!");
 61                 break;
 62 
 63             case 4:
 64                 System.out.println("You got D!");
 65                 break;
 66 
 67             case 5:
 68                 System.out.println("You got E!");
 69                 break; 
 70         
 71             default:
 72                 break;
 73         }
 74 
 75         System.out.println("Please input your score:");
 76         Scanner sc3 = new Scanner(System.in);
 77         double score = sc3.nextInt();
 78         char level = score >= 90 ? 'A' :
 79                     (score >= 80) && (score <= 90) ? 'B' :
 80                     (score >= 70) && (score <= 80) ? 'C' :
 81                     (score >= 60) && (score <= 70) ? 'D' : 'E';
 82         System.out.println("You got " + level + "!");
 83 
 84 
 85 
 86 
 87         System.out.println("-----------------------------------------");
 88         System.out.println("Common year and Leap year:");
 89         Scanner input = new Scanner(System.in);
 90         
 91         System.out.println("Please enter year:");
 92         int year = input.nextInt();
 93         if(year % 4 != 0 || year % 100 == 0 && year % 400 != 0) 
 94         {
 95             System.out.println(year + " is common year");
 96         }
 97         else 
 98         {
 99                 System.out.println(year + " is leap year");
100         }
101         
102     }
103 }

 

用三目运算符判断分数级别:

 

运行结果:

 

  1. 练习九九乘法表
  2. 画一个菱形
  3. 设计一个游戏:共21个苹果,两个人轮流取,每人每次只可取1~4个苹果,取最后一个苹果者输。编写人机对弈程序,人先取,计算机最后要赢。

 

 1 import java.util.*;
 2 public class A
 3 {
 4     public static void main(String[] args)
 5     {
 6         System.out.println("Print the multiplication table:");
 7         for(int i = 1;i < 10;i++)
 8         {
 9             for(int j = 1;j <= i;j++)
10             {
11                 System.out.print(j + "*" + i + "=" + i*j + " ");
12             }
13             System.out.println();
14                 
15         }
16 
17         System.out.println("--------------------------------");
18         
19         int a[][] =
20         {
21             {1, 1, 1, 2, 1, 1, 1},
22             {1, 1, 2, 2, 2, 1, 1},
23             {1, 2, 2, 2, 2, 2, 1},
24             {2, 2, 2, 2, 2, 2, 2},
25             {1, 2, 2, 2, 2, 2, 1},
26             {1, 1, 2, 2, 2, 1, 1},
27             {1, 1, 1, 2, 1, 1, 1},
28         }; 
29 
30         
31 
32 
33         
34         for(int m=0;m<a.length;m++){
35             for(int n=0;n<a[m].length;n++){
36                 if(a[m][n] == 1) System.out.print(" ");
37                 else System.out.print("*");
38             }
39             System.out.println();
40         }
41 
42         System.out.println("--------------------------------");
43 
44 
45         
46         for (int i = 1, j = 1; i <= 9; ) {
47         System.out.print(j + "*" + i + "=" + i*j + " ");
48         j++;
49         if (j>i) {
50             System.out.print("\n");
51             i++;
52             j = 1;
53         }
54     }
55 
56     
57 
58         
59 
60 
61             
62         
63     }
64 }

 

 

 

posted @ 2018-09-13 16:52  小骏不抬杠  阅读(161)  评论(0编辑  收藏  举报