day02知识点总结

1.算术运算符:+,-,*,/,%

  1. String的format来转换
  2. DecimalFormat转换,注意他的占位符号
  3. 因为二进制的不精确,浮点类型的值计算会有误差(video),用BigDecimal可以计算 传递参数不一样 可以用API直接转换为相应的基本类型 也可以
public class Demo {

	public static void main(String[] args) {
		//第一种方法:使用String的format
		double d = 123.452;
		System.out.println(Double.parseDouble(String.format("%.3f", d)));; //保留三位小数
		//第二种方法:使用DecimalFormat
		double e = 123.7;
		DecimalFormat df = new DecimalFormat("0.00"); //后面小数点不够自动补0
		//看API接收的是什么参数
		System.out.println(df.format(e));
	}

}
public class Demo2 {

	public static void main(String[] args) {
		double d1 = 2; //字面量int自动提升到double
		double d2 = 1.9;
		
		//看到传入字符串和基本数据类型是不同的效果
		BigDecimal bd1 = new BigDecimal(2);
		BigDecimal bd2 = new BigDecimal(1.9);
		System.out.println(bd1.subtract(bd1.subtract(bd2)));; //重写了toString 不会输出@
		
		BigDecimal bd3 = new BigDecimal("2");
		BigDecimal bd4 = new BigDecimal("1.9");
		System.out.println(bd1.subtract(bd3.subtract(bd4)));;
		
		//直接转换成double类型的数
		double bd5 = bd1.subtract(bd3.subtract(bd4)).doubleValue();
		System.out.println(bd5);
		
		//保留小数 三个参数 
		System.out.println(new BigDecimal("1").divide(new BigDecimal("3"),2,BigDecimal.ROUND_HALF_UP));
	}

}

在这里插入图片描述

2. 连接符 +

public static void main(String[] args) {
		System.out.println(3+5+""); 
		System.out.println(""+3+5); 
		System.out.println(3+5+""+3+5); 
	}

3. 赋值运算符(左边必须是变量)

在这里插入图片描述
在这里插入图片描述
注意byte的强制转换 详见:https://blog.csdn.net/weixin_42512488/article/details/95373256

4. 自增自减运算符++ –

  • 前置:
    int x=1;
    int r = ++x;
    x=2;r=2;
  • 后置:
    x=1;
    r=x++;
    x=2; r=1;

5. 比较运算符

6. 逻辑运算符

  • &&(&): 短路 &:两边的表达式或者变量(字面量)必须为boolean类型
    boolean b = 2>8 & true;
    区别:&无短路
  • ||(|): 短路:
    同理
  • || &&优先级高,书上是这么写的,但证明了半天没有证明出来
  • ^(相同为0,不同为1)
  • !>,&&>,||:特殊情况 ||在&&前,如果有短路,那么&&直接被短路掉,如下:
public class Demo5 {
	public static void main(String[] args) {
		int x = 1;
		int y = 1;
		//证明了&&的优先级没有||高
		boolean b1 = x++>0 || true && y++>0;
		System.out.println(b1+","+x+","+y);
		
		int a = 0;
		boolean b2 = x++>3 || 1>a && y++>3;  //加括号  ~>&&>||
		System.out.println(b2+","+x+","+y);
		
		
	}
}

7. 二进制运算符 & | ^ ~

    2&3                  2|3                  2^3               ~2    

    010                   010                  010               ~ 0010
& 011                 | 011                 ^ 011                   1101   1000 0011
    010                   011                   001

8. 两个数交换的解决方案

public class Demo6 {
	
	public static void swap(int a, int b){
		a = a ^ b;
		b = a ^ b;
		a = a ^ b;
		System.out.println(a+" "+b);
	}
	public static void swap1(int a, int b){
		a = a + b;
		b = a - b;
		a = a - b;
		System.out.println(a+" "+b);
	}
	public static void swap2(int a,int b){
		int c = a;
		a = b;
		b = c;
		System.out.println(a+" "+b);
	}
	public static void main(String[] args) {
		
		swap(4,5);
		swap1(1,2);
		swap2(7,8);
	}
}

9. 三目运算符

两边不兼容,object

public class Demo7 {
	
	public static void main(String[] args) {
	
		int a = 1;
		String b = "hello";
		//三目运算符取决于变量中最大的哪一种类型
		double e = true?1.0:1;
		Object c  = true ? a:b;
		System.out.println(c);
		//int a,b,c返回最大值
		int t = 2;
		int t1 = 3;
		int t3 = 4;
		int tt = ((t>t1?t:t1)>t3)?(t>t1?t:t1):t3;
		System.out.println(tt);
	}
}

10. 赋值运算符

在这里插入图片描述

11. 语句种类

赋值语句:y=9
方法调用语句:System.out.println(); 静态变量 输出流
import导入类 package
条件语句
循环语句
break,continue

12. package语句

  • 作用:
  • 定义格式:package a.b.c;
  • 位置:类的第一条有效语句,有且只能有一条。但上面可以放注释。可以没有,放在默认包下default package;

13. import语句

  • 作用:导入类 java.util.*; import java.util.Date;
  • 格式:
  • 位置:package下,class上
  • 个数:用多少引入多少

14. if语句

  • if(逻辑值){}
  • if(逻辑值){}else{}
  • if(逻辑值){}else if(l逻辑值){}…else{}
  • 只走一个,其余即使再符合也不走
  • if(b){}if(b){}else{}
  • 注意:if和else中只有一条语句可以省略大括号

15. switch语句(case后的值不能相同)

  • switch(值){
    case n1:
    语句体;
    break;

    default:
    语句体;
    }
  • 值类型:byte,short,int,char,enum,String
  • 执行:值和n相同,执行语句体
  • n1 n2 可以相同,default可以放前面
  • 如果没有break,那么后面的case不管用,直接输出。default不会输出
    问:如何用Scanner接受char类型
    在这里插入图片描述
    在这里插入图片描述
    或者用charAt(index);
    问:用switch输出春夏秋冬
    问:next和nextLine有什么区别和注意事项
    https://blog.csdn.net/Zhiyuan_Ma/article/details/51592730
    问:String和char之间如何转换
    在这里插入图片描述
    问: if(true) break; 合法吗?
    在这里插入图片描述

16. 循环

  • for(表达式初始值;判断条件;步长){} 都可以不写,死循环
  • 循环原理:

练习题:
1.指出编译错误,并改正

int balance;
balance = 218.50;

218.50字面量为double类型,赋值给int类型需要强制转换
int balance;
balance = (int)218.50;

2.指出编译错误或运行结果,并解释原因
int i = 128;
i = 10000000008;
System.out.println(i);

编译错误 10000000008字面量默认类型为int 超过int的范围

3.打印结果
double x = 2.0;
double y = 1.9;
System.out.println(x-y);
结果:0.10000000009

4.收银台收款程序
编写一个收银台的收款程序,根据输入的商品大家,购买数量及
收款金额,计算出应收金额和找零;
当总价大于或等于500时,打8折;
如果收款金额小于应收金额时,输出提示信息

public class Demo9 {
	
	public static void shouYin(){
		HashMap<String,Double> h = new HashMap<String, Double>();
		h.put("苹果", 20.0);
		h.put("梨", 30.0);
		h.put("香蕉", 40.0);
		
		Scanner s1 = new Scanner(System.in);
		System.out.println("请输入你要购买的商品:");
		String s = s1.nextLine();
		System.out.println("请输入你要购买的商品数量:");
		int num = s1.nextInt();
		if(h.containsKey(s)){
			System.out.println("输入您的付款金额:");
			double price = s1.nextDouble();
			if(price>h.get(s)*num)
				System.out.println("找零:"+(price-h.get(s)*num));
			else
				System.err.println("钱没给够!");
			
		}else{
			System.out.println("没有你要购买的商品");
		}
	}
	public static void main(String[] args) {
		int a,b,c;
		a = b =c=100;
		System.out.println(a+""+b+""+c);
//		shouYin();
	}
}

5.输出结果
int a,b,c;
a = b =c=100;
System.out.println(a+""+b+""+c);

100 100 100

6.编写个人所得税计算程序
b得税的计算公式是:资薪金所得-扣除数)*适用税率-速算扣除数。 其中,扣除数为3500,使用税率以及速算扣除数如下表所示。

全月应纳税所得额 税率 速算扣除数(元)
全月应纳税额不超过1500元 3% 0
全月应纳税额超过1500至4500 10% 105
全月应纳税额超过4500至9000 20% 555
全月应纳税额超过9000至35000 25% 1005
全月应纳税额超过35000至55000 30% 2755
全月应纳税额超过55000至80000 35% 5505

  •       45%	13505
    

上表中的全月应纳税所得额 = 工资薪金所得 - 扣除数。
本案例要求计算个人所得税的缴纳额度:用户从控制台输入税前工资的金额,
程序计算所需要缴纳的个人所得税的金额,并将计算结果输出到控制台。

public class Demo9 {
	
	public static void main(String[] args) {
		Scanner s1 = new Scanner(System.in);
		double b = s1.nextDouble();
		if(b<=5000){
			System.out.println("不交税");
		}else if((b-5000)<=1500){
			System.out.println((b-5000)*0.03);
		}else if((b-5000)>1500&&(b-5000)<=4500){
			System.out.println((b-5000)*0.1-105);
		}else if((b-5000)>4500&&(b-5000)<=9000){
			System.out.println((b-5000)*0.2-555);
		}else if((b-5000)>9000&&(b-5000)<=35000){
			System.out.println((b-5000)*0.25-1005);
		}else if((b-5000)>35000&&(b-5000)<=55000){
			System.out.println((b-5000)*0.3-2775);
		}else if((b-5000)>55000&&(b-5000)<=80000){
			System.out.println((b-5000)*0.35-5505);
		}else{
			System.out.println((b-5000)*0.45-13505);
		}
	}
}

7.控制台输入两个int数,输出最大值

8.控制台输入三个int数,输出最大值

public class Demo10 {
	public static void main(String[] args) {
		Scanner s1 = new Scanner(System.in);
		int a = s1.nextInt();
		int b = s1.nextInt();
		System.out.println(a>b?a:b);
		int c = s1.nextInt();
		System.out.println((a>b?a:b)>c?(a>b?a:b):c);
	}
}

9.控制台输入三个int数,升序排序

public class Demo16 {
	public static void bubble(int[] arr) {
		for(int j=0;j<arr.length-1;j++)
			for (int i = 0; i < arr.length-1-j; i++) {
				if(arr[i]>arr[i+1]){
					swap(arr,i,i+1);
				}
			}
	}

	private static void swap(int[] arr, int i, int j) {
		arr[i] = arr[i] ^ arr[j];
		arr[j] = arr[i] ^ arr[j];
		arr[i] = arr[i] ^ arr[j];
	}

	public static void main(String[] args) {
		int[] arr = {2,5,6,9,1};
		bubble(arr);
		for(int i:arr)
			System.out.println(i);
	}
}

10.接收 控制台输入的年和月份,输出该年该月有多少天

比如 2000年2月,共29天
public class Demo12 {

	public static void test(){
		Scanner s1 = new Scanner(System.in);
		int year = s1.nextInt();
		int month = s1.nextInt();
		switch(month){
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			System.out.println(31);
			break;
		case 2:
			if(year%4==0&&year%100!=0||year%400==0){
				System.out.println(29);
			}else{
				System.out.println(28);
			}
			break;
		default:
			System.out.println(30);
		}
	}
	public static void main(String[] args) {
		test();
	}
}
package cn.tedu.day02.test;

import java.util.Scanner;

public class Demo11 {
	public static void test1(){
		Scanner scanner =  new Scanner(System.in);
		System.out.println(666);
		int a = scanner.nextInt();
		int b = scanner.nextInt();
		System.out.println(777);
		//不能用nextLine
		String c = scanner.next();
		switch(c){
		case "+":
			System.out.println(a+b);
			break;
		case "-":
			System.out.println(a-b);
		case "*":
			System.out.println(a*b);
			break;
		case "/":
			System.out.println(a/b);
			break;
		default:
			System.out.println("没有此运算符");
		}
	}
	
	public static void main(String[] args) {
		
		test1();
	}
	
	public static void test2(){
		int x = 9;
		switch(x){
		default:
			System.out.println("2");
		case 2:
			System.out.println("3");
		case 52:
			System.out.println("1");
		}
	}
	
	public static void test3(){
		System.out.println("请输入月份:\n");
		Scanner in=new Scanner(System.in);
		int month;
		month=in.nextInt();
		switch(month)
		{
			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;
		}


	}
	
}

11.猜数游戏:
while,do…while

public class Demo14 {
	
	public static void main(String[] args) {
		int num = (int)(Math.random()*10)+1;
//		System.out.println(num);
		Scanner s1 = new Scanner(System.in);
		while(true){
			System.out.println("请输入你猜的数字");
			int a = s1.nextInt();
			if(a==11)
				break;
			else if(a<num)
				System.out.println("小了");
			else if(a>num)
				System.out.println("大了");
			else{
				System.out.println("对了");
				break;
			}
		}
	}
}

12.百钱百鸡
3文钱1只公鸡,2文钱1只母鸡,1文钱3只小鸡;
100文钱如何买恰好100只鸡?
分析: 先定义一个循环表示公鸡,嵌套一个循环表示母鸡,
计算小鸡的个数,根据各种鸡的个数来计算钱数是否为10012.

public class Demo15 {
	
	public static void main(String[] args) {
		for(int a = 0; a < 100; a++ ){
			for(int b = 0; b < 100; b++){
				for(int c = 0; c < 100; c++){
					if((9*a+6*b+c)==300 && (a+b+c)==100)
						System.out.println(a+" "+b+" "+c);
				}
			}
		}
	}
}
发布了84 篇原创文章 · 获赞 90 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/weixin_42512488/article/details/95190945