[关闭]
@Sarah 2015-11-05T17:04:51.000000Z 字数 4701 阅读 983

JAVA1 if 循环 case


001


保留字
标识符
JAVA代码写完以后存到 .class 文件里
main主函数 程序执行main后面的内容,如果不想执行就加上数字

  1. main01()

打主函数的时候,先打main,然后找快捷方式里第二个即可


JAVA结构

1函数:方法 method
2变量:存放数据

成员变量:针对整体的
局部变量:写在代码块里的

整数 int long
浮点数 float double
字符串 char
布尔 boolean

如果超出范围会变成负的“溢出”

3语句:执行

逻辑关系

1分支

1.if
形式1

  1. if(逻辑表达式){代码块}

形式2

  1. if () {}
  2. else{}

形式3

  1. if (){}
  2. else if(){}
  3. else if(){}
  4. ...
  5. else{}

2.switch

  1. switch(整形变量){
  2. case1:
  3. 代码
  4. break;
  5. case2:
  6. 代码
  7. break;
  8. }

2循环

1while
形式1

  1. while (条件){
  2. }

形式2

  1. do{
  2. }
  3. while(条件)

2for

  1. for(初始化;循环;增量){}

eg

  1. for(i=0;i<=100;i++)

阶乘代码
是否构成三角形代码

这两个结果是布尔值

and :&&
or: ||

这两个结果是个数

&
|

问号?如何使用

  1. i=条件? AB
  2. 如果真执行A
  3. 如果假执行B

上课所有范例

  1. //1熟悉变量
  2. public static void main1(String[] args) {
  3. int ageOfMine = 100;
  4. int height;
  5. height = 200;
  6. long g = 123456789876543l;
  7. float f1 = 5678.567f;
  8. double f2 = 3456.5678 + 1;
  9. boolean b = 1 == 0;
  10. height = -height;
  11. System.out.println(height);
  12. }
  13. //2用两种方式判断数字是星期几
  14. public static void main2(String[] args) {
  15. int i = 4;
  16. // if (i==1){
  17. // System.out.println("Mondy");
  18. // }
  19. // else if( i ==2){
  20. // System.out.println("Tuesday");
  21. //
  22. // }
  23. switch (i) {
  24. case 1:
  25. System.out.println("monday");
  26. break;
  27. case 2:
  28. System.out.println("");
  29. break;
  30. case 3:
  31. System.out.println("Wendsday");
  32. break;
  33. // default:
  34. // System.out.println("default");
  35. //如果都不符合,输出default
  36. }
  37. }
  38. //求阶乘
  39. public static void main3(String[] args) {
  40. long k = 1;
  41. for (int i = 1; i <= 26; i++) {
  42. k = k * i;
  43. System.out.println(k);
  44. }
  45. // System.out.println(k);
  46. }
  47. //判断三边是否构成三角形
  48. public static void main4(String[] args) {
  49. int a = 3, b = 5;
  50. int c = 7;
  51. if (a + b > c && b + c > a && a + c > b) {
  52. System.out.println("YES");
  53. } else {
  54. System.out.println("NO");
  55. }
  56. }
  57. //大括号的作用
  58. public static void main5(String[] args) {
  59. int i = 67, j = 45;
  60. if (i == j) {
  61. System.out.println("1");
  62. }
  63. System.out.println("2");
  64. System.out.println("ok");
  65. }
  66. //累加到100 方法1
  67. public static void main6(String[] args) {
  68. int k = 0, i = 1;
  69. while (i < 101) {
  70. k = k + i;
  71. i++;
  72. }
  73. System.out.println(k);
  74. //方法2
  75. k = 0;
  76. i = 1;
  77. while (true) {
  78. // 这个true能让循环继续下去
  79. if (i > 100)
  80. break;
  81. else
  82. k = k + i;
  83. i++;
  84. }
  85. System.out.println(k);
  86. }
  87. //判断成绩优良中差
  88. public static void main(String[] args) {
  89. int testscore = 76;
  90. char grade;
  91. if (testscore >= 90) {
  92. grade = '优';
  93. } else if (testscore >= 80) {
  94. grade = '良';
  95. } else if (testscore >= 70) {
  96. grade = '中';
  97. } else if (testscore >= 60) {
  98. grade = 'D';
  99. } else {
  100. grade = 'F';
  101. }
  102. System.out.println("Grade = " + grade);
  103. }
  104. //左右对称的三角形
  105. public static void main0(String[] args) {
  106. for (int i = 1; i <=10; i++) {
  107. for (int k = 10; k>i; k--) {
  108. System.out.print(" ");
  109. }
  110. for (int j = 0; j < i; j++)
  111. System.out.print("*");
  112. for (int j = 0; j < i; j++)
  113. System.out.print("*");
  114. System.out.println();
  115. }
  116. }
  117. }
  118. //打印出星号组成的左对齐三角形
  119. //[右对齐三角形
  120. public static void main(String[] args) {
  121. for (int i = 1; i <10; i++) {
  122. for (int k= 10; k> i; k--) {
  123. System.out.print(" ");
  124. }
  125. for (int j = 0; j < i; j++)
  126. System.out.print("*");
  127. System.out.println();
  128. }
  129. }
  130. }
  131. //左对齐的三角形
  132. public class main01 {
  133. public static void main(String[] args) {
  134. for (int i=0;i<=10;i++){
  135. for (int j=0;j<i;j++)
  136. System.out.print("*");
  137. //右对齐倒三角形
  138. public static void main(String[] args) {
  139. for (int i=0;i<=10;i++){
  140. for (int j=0;j<i;j++)
  141. System.out.print(" ");
  142. for (int k=10;k>i;k--){
  143. System.out.print("*");
  144. }
  145. //左对齐倒三角形
  146. public static void main(String[] args) {
  147. for (int i=0;i<=10;i++){
  148. for (int k=10;k>i;k--){
  149. System.out.print("*");
  150. }
  151. //从上往下递减=倒三角形
  152. //从上往下递增=正三角形
  153. //先有空格三角形=右对齐
  154. //直接星号=左对齐

注意大括号的位置:
如果是在大括号里面,那每循环一次就执行一次;
如果是在大括号外面,那括号里面循环结束才会执行

  1. //判断月份
  2. class SwitchDemo {
  3. public static void main(String[] args) {
  4. int month = 8;
  5. switch (month) {
  6. case 1: System.out.println("January"); break;
  7. case 2: System.out.println("February"); break;
  8. case 3: System.out.println("March"); break;
  9. case 4: System.out.println("April"); break;
  10. case 5: System.out.println("May"); break;
  11. case 6: System.out.println("June"); break;
  12. case 7: System.out.println("July"); break;
  13. case 8: System.out.println("August"); break;
  14. case 9: System.out.println("September"); break;
  15. case 10: System.out.println("October"); break;
  16. case 11: System.out.println("November"); break;
  17. case 12: System.out.println("December"); break;
  18. default: System.out.println("Invalid month.");break;
  19. }
  20. }
  21. }
  1. //一个月多少天
  2. class SwitchDemo2 {
  3. public static void main(String[] args) {
  4. int month = 2;
  5. int year = 2000;
  6. int numDays = 0;
  7. switch (month) {
  8. case 1:
  9. case 3:
  10. case 5:
  11. case 7:
  12. case 8:
  13. case 10:
  14. case 12:
  15. numDays = 31;
  16. break;
  17. case 4:
  18. case 6:
  19. case 9:
  20. case 11:
  21. numDays = 30;
  22. break;
  23. case 2:
  24. if ( ((year % 4 == 0) && !(year % 100 == 0))
  25. || (year % 400 == 0) )
  26. numDays = 29;
  27. else
  28. numDays = 28;
  29. break;
  30. default:
  31. System.out.println("Invalid month.");
  32. break;
  33. }
  34. System.out.println("Number of Days = " + numDays);
  35. }
  36. }
  1. class WhileDemo {
  2. public static void main(String[] args){
  3. int count = 1;
  4. while (count < 11) {
  5. System.out.println("Count is: " + count);
  6. count++;
  7. }
  8. }
  9. }
  1. class DoWhileDemo {
  2. public static void main(String[] args){
  3. int count = 1;
  4. do {
  5. System.out.println("Count is: " + count);
  6. count++;
  7. } while (count <= 11);
  8. }
  9. }
  1. class ForDemo {
  2. public static void main(String[] args){
  3. for(int i=1; i<11; i++){
  4. System.out.println("Count is: " + i);
  5. }
  6. }
  7. }
  1. class EnhancedForDemo {
  2. public static void main(String[] args){
  3. int[] numbers = {1,2,3,4,5,6,7,8,9,10};
  4. for (int item : numbers) {
  5. System.out.println("Count is: " + item);
  6. }
  7. }
  8. }
  1. class BreakDemo {
  2. public static void main(String[] args) {
  3. int[] arrayOfInts = { 32, 87, 3, 589, 12, 1076,
  4. 2000, 8, 622, 127 };
  5. int searchfor = 12;
  6. int i;
  7. boolean foundIt = false;
  8. for (i = 0; i < arrayOfInts.length; i++) {
  9. if (arrayOfInts[i] == searchfor) {
  10. foundIt = true;
  11. break;
  12. }
  13. }
  14. if (foundIt) {
  15. System.out.println("Found " + searchfor
  16. + " at index " + i);
  17. } else {
  18. System.out.println(searchfor
  19. + " not in the array");
  20. }
  21. }
  22. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注