JAVA入门(二)—基本语法
一、运算符
1.1、算术运算符
练习:
package Package01; public class ArithmeticoperatorDemo1 { public static void main(String[] args) { System.out.println(3+2); System.out.println(5-3); System.out.println(7*9); System.out.println(10 / 2); System.out.println(10 / 3); //整数参与运算结果是整数 System.out.println(10.0 / 3);//小数运算结果又肯呢个不精确 System.out.println(10 % 2); System.out.println(10 % 3); } }
输入一个三位数,求他的个位,百位,十位
import java.util.Scanner; public class ArithmeticoperatorDemo02 { public static void main(String[] args){ System.out.println("请输入一个数"); Scanner sc = new Scanner(System.in); int number = sc.nextInt(); int ge = number % 10; int shi = number /10 % 10; int bai = number /100 % 10; System.out.println(ge); System.out.println(shi); System.out.println(bai); } }
1.2、数据的转换
隐式转换:取值范围小的转换成取值范围大的;(自动类型提升)
强制转换:取值范围大的转换成取值范围小的;
byte、short计算的时候先提升为int;
强制转换
package Package01; public class ArithmeticoperatorDemo03 { public static void main(String[] args) { byte a = 10; byte b = 20; int c = (byte)(a + b); System.out.println(c); } }
1.3、字符串连接
System.out.println("个位是"+ge); System.out.println("十位是"+shi); System.out.println("百位是"+bai);
1.4、自增自减运算符
++ 变量加一
-- 变量减一
a++ 先用后加
++a 先加后用
public class ArithemticoperatorDemo4 { public static void main(String[] args){ int x = 10; int y = x++; int z = ++x; System.out.println("x的值是" + x); System.out.println("y的值是" + y); System.out.println("z的值是" + z); } } x的值是12 y的值是10 z的值是12
1.5、赋值运算符
1.6、关系运算符
public class Compare { public static void main(String[] args) { int a = 10; int b = 10; int c = 30; System.out.println(a==b); System.out.println(a==c); } }
练习
mport java.util.Scanner; public class Appointment { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入第一个整数:"); int a = sc.nextInt(); System.out.println("请输入第二个整数"); int b = sc.nextInt(); boolean c = a>b; System.out.println(c); } }
1.7、逻辑运算符
短路逻辑运算符
public class LogicCompare02 { public static void main(String[] args) { int a = 10; int b = 20; boolean c = ++a < 5 && ++b < 5; System.out.println(a); System.out.println(b); } }
练习
import java.util.Scanner; public class Test6 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入第一个整数"); int a = sc.nextInt(); System.out.println("请输入第二个整数"); int b = sc.nextInt(); boolean c = a == 6 || b == 6 || (a + b) % 6 == 0; System.out.println(c); } }
1.8、三元运算符
格式 a>b ? a : b 如果a>b 成立,则结果是a,如果a>b 不成立,则结果就是b;
import java.util.Scanner; public class Weight { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入第一个数:"); int weight1 = sc.nextInt(); System.out.println("请输入第二个数:"); int wehght2 = sc.nextInt(); String result = weight1 == wehght2 ? "相同" : "不同"; System.out.println(result); } }
1.9、原码、反码、补码
原码:十进制数据的二进制表现,最左边是符号位0为正,1为负;
反码:为了解决原码不能解决负数的问题,正数的反码不变,负数的反码,在原码的基础上,符号位不变,数值取反,0变1,1变0;
补码:解决负数计算跨零的问题,正数的补码不变,负数的补码在反码的基础上加1;-128没有反码和原码;
计算机中数据的存储和计算都是以补码的形式进行的。
二进制的一个0或者一个1称为一个bit,8个bit称为一个字节byte,
二、流程控制
2.1、IF控制
import java.util.Scanner; public class IFTes03 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入你的分数:"); int a = sc.nextInt(); if(a >= 0 && a <= 100){ if(a >= 95 && a <= 100){ System.out.println("奖品是自行车!"); }else if(a >= 80 && a < 95){ System.out.println("奖品是游乐场!"); }else if(a >= 75 && a <= 80){ System.out.println("奖品是蛋糕!"); }else{ System.out.println("good luck!"); } }else{ System.out.println("请输入正确的分数:"); } } }
2.2、SWITCH语句
import java.util.Scanner; public class SwitchTest { public static void main(String[] args) { System.out.println("请输入星期数"); Scanner sc = new Scanner(System.in); int a = sc.nextInt(); switch(a){ case 1: System.out.println("星期一"); break; case 2: System.out.println("星期二"); break; case 3: System.out.println("星期三"); break; case 4: System.out.println("星期四"); break; case 5: System.out.println("星期五"); break; default: System.out.println("NO"); break; } } }
case穿透
import java.util.Scanner; public class CaseTest { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入星期数:"); int a = sc.nextInt(); switch(a){ case 1: case 2: case 3: case 4: case 5: System.out.println("工作日"); break; case 6: case 7: System.out.println("休息日"); break; default: System.out.println("NO"); break; } } }
简化代码(JDK12)
import java.util.Scanner; public class CaseTest { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入星期数:"); int a = sc.nextInt(); switch(a){ case 1,2,3,4,5, -> System.out.println("工作日"); case 6,7 -> System.out.println("休息日"); default -> System.out.println("NO"); } } }
2.3、IF循环结构
求100以内偶数的和
public class SumTest { public static void main(String[] args) { int sum = 0; for(int i = 1 ; i <= 100 ; i++){ if( i % 2 == 0){ sum = sum + i ; } } System.out.println(sum); } }
2.4、WHILE循环结构
public class WhileTest { public static void main(String[] args) { int i = 1; while(i <= 100){ System.out.println(i); i = ++ i; } } }
2.5、For循环和While循环的区别
public class WhileTest3 { public static void main(String[] args) { int bcs = 10; int cs = 3; int ys = 0; int shang = 0; while(bcs >= cs){ bcs = bcs - cs; shang++; } System.out.println(shang); System.out.println(bcs); } }
2.6、跳出循环
continut跳出本次循环,镜像下一次循环;
public class Test0415 { public static void main(String[] args) { for ( int i = 1;i <= 5;i++ ){ if(i == 3){ continue; } System.out.println("这是第" + i + "循环"); } } }
break结束循环;
public class Test0415 { public static void main(String[] args) { for ( int i = 1;i <= 5;i++ ){ if(i == 3){ break; } System.out.println("这是第" + i + "循环"); } } }
逢7 过
public class Test041502 { public static void main(String[] args) { for(int i =1 ; i <= 100 ; i++){ //个位是7,十位是7,7的倍数 if(i % 10 == 7 || i / 10 % 10 == 7 || i % 7 ==0){ System.out.println("过"); continue; } System.out.println(i); } } }
平方根
import java.util.Scanner; public class Testping { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入一个大于2的整数"); int number = sc.nextInt(); for(int i = 1;i <= number; i++){ if(i * i == number){ System.out.println(i + "就是" + number + "平方根"); break; }else if(i * i > number){ System.out.println((i - 1) + "就是" + number + "平方根的整数部分"); break; } } } }
循环中的标记
import java.util.Scanner; public class TestZhiShu { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入一个整数"); boolean flage = true; int number = sc.nextInt(); for(int i = 2 ; i < number; i++){ if( number % i == 0){ flage = false; break; } } if(flage){ System.out.println(number + "是一个质数"); }else{ System.out.println(number + "是一个合数"); } } }
随机生成一个数,猜这个数字
mport javax.swing.*; import java.util.Random; import java.util.Scanner; public class RandomTest { public static void main(String[] args) { Random r = new Random(); int number = r.nextInt(100)+1;//100表示0 到 99,后面加1,表示1-100之间 while(true) { System.out.println("请输入你要猜的数字:"); Scanner sc = new Scanner(System.in); int i = sc.nextInt(); if (i > number) { System.out.println("大了"); } else if (i < number) { System.out.println("小了"); } else { System.out.println("合适"); break; } } } }
感谢视频作者 https://www.bilibili.com/video/BV17F411T7Ao/?p=47&spm_id_from=pageDriver&vd_source=a152087dca674715707126502292f55b