首页 > 学院 > 开发设计 > 正文

Java 01[基本知识]

2019-11-14 09:04:33
字体:
来源:转载
供稿:网友

Java三种技术架构

[1] java三种技术架构


命名规范 命名规范

[2] 命名规范


Java数据类型 比较字符

[3] Java数据类型 & 比较字符


变量初始化

[4] 变量初始化


转义字符

[5] 转义字符


栈内存

[6] 栈内存


堆内存

[7] 堆内存 (new所创建的)


new一个对象

[7-1] new一个对象


关键字 关键字

[8]关键字


[9 进制的转换]

public class A {

//有多少位数,就添加多少if语句。public static void main(String[] args) { // 二进制转换 er(5); // 八进制转换 ba(9); // 十六进制转换 shiliu(125);}// 二进制转换PRivate static void er(int i) { if (i > 1) { int a = i % 2; i = i / 2; if (i > 1) { int a1 = i % 2; i = i / 2; if (i > 0) { int a2 = i % 2; i = i / 2; System.out.println(a2 + "" + a1 + "" + a); } } }}// 八进制转换private static void ba(int i) { if (i > 1) { int a = i % 8; i = i / 8; if (i > 0) { int a1 = i % 8; i = i / 8; System.out.println(a1 + "" + a); } }}// 十六进制转换private static void shiliu(int i) { String str = null; if (i > 1) { int a = i % 16; i = i / 16; if (a == 10) { str = "A"; } else if (a == 11) { str = "B"; } else if (a == 12) { str = "C"; } else if (a == 13) { str = "D"; } else if (a == 14) { str = "E"; } if (i > 0) { int a1 = i % 16; i = i / 16; System.out.println(a1 + str); } }}

}


[10 ①大小写转换, ②判断成绩, ③判断日期为一年当中的第几天, ④判断季节, ⑤互换两数位置(不使用中间变量)]

import java.util.Scanner; public class B {

public static void main(String[] args) { /*大小写转换*/ char ch = 'f'; switch (ch) { case 'a': System.out.println((char) (ch - 32)); break; case 'b': System.out.println((char) (ch - 32)); break; case 'c': System.out.println((char) (ch - 32)); break; case 'd': System.out.println((char) (ch - 32)); break; case 'e': System.out.println((char) (ch - 32)); break; default: System.out.println("other"); break; } /*判断成绩 注意:要将分数高的过滤排在前面(同类的switch也是如此,将大数写在前面) */ int chengji = 59; if (chengji >= 60) { System.out.println("及格"); } else { System.out.println("不及格"); } switch (chengji / 60) { case 1: System.out.println("及格"); break; case 0: System.out.println("不及格"); break; default: System.out.println("输入的成绩有误"); break; } /* 判断日期为第几天 */ Scanner sc = new Scanner(System.in); System.out.println("请输入年:"); int nian = sc.nextInt(); System.out.println("请输入月:"); int yuefen = sc.nextInt(); System.out.println("请输入天:"); int tian = sc.nextInt(); int sum = 0; if (yuefen >= 1 && yuefen <= 12){ if (yuefen==2) { if((nian % 4 == 0 && nian % 100 !=0) || nian % 400 == 0){ if(tian > 29 && tian <= 31) { System.out.println("输入的天有误"); }else { switch (yuefen) { case 12: sum += 30; case 11: sum += 31; case 10: sum += 30; case 9: sum += 31; case 8: sum += 31; case 7: sum += 30; case 6: sum += 31; case 5: sum += 30; case 4: sum += 31; case 3: if ((nian % 4 == 0 && nian % 100 !=0) || nian % 400 == 0) { sum += 29; } else { sum += 28; } case 2: sum += 31; case 1: sum += tian; System.out.println("输入的日期为" + nian + "年的第" + sum + "天"); break; default: break; } } }else if(tian > 28 && tian <= 31) { System.out.println("输入的天有误"); }else { switch (yuefen) { case 12: sum += 30; case 11: sum += 31; case 10: sum += 30; case 9: sum +=31; case 8: sum += 31; case 7: sum += 30; case 6: sum += 31; case 5: sum += 30; case 4: sum += 31; case 3: if (nian % 4 == 0) { sum += 29; } else { sum += 28; } case 2: sum += 31; case 1: sum += tian; System.out.println("输入的日期为" + nian + "年的第" + sum + "天"); break; default: break; } } } else if (tian <= 31 && tian > 0) { switch (yuefen) { case 12: sum += 30; case 11: sum += 31; case 10: sum += 30; case 9: sum += 31; case 8: sum += 31; case 7: sum += 30; case 6: sum += 31; case 5: sum += 30; case 4: sum += 31; case 3: if (nian % 4 == 0) { sum += 29; } else { sum += 28; } case 2: sum += 31; case 1: sum += tian; System.out.println("输入的日期为" + nian + "年的第" + sum + "天"); break; default: break; } }else{ System.out.println("输入的天有误"); } }else{ System.out.println("输入的月份有误"); } /*判断季节*/ switch (yuefen) { case 3: case 4: case 5: System.out.println("春季"); break; case 6: case 7: case 8: System.out.println("夏季"); break; case 9: case 10: case 11: System.out.println("秋季"); break; case 12: case 1: case 2: System.out.println("冬季"); break; default: System.out.println("输入的月份有误"); break; } /*互换*/ int m=12; int n=5; System.out.println(m+","+n); m=m^n;//m=12^5=9 n=m^n;//n=9^5=12 m=m^n;//m=9^12=5 System.out.println(m+","+n);}

}


[11 tips:]

当switch循环中的case事件出错时, 需要检查是否遗漏了 “break;” Scanner是一种很宝贵的流, 当不使用时应当关闭.
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表