Java基础知识复习(一)

一、算术操作符

1.练习-求和:

            要求: 使用Scanner从控制台获取两个数字,然后计算这两个数字的和

import java.util.Scanner;

public class Test1 {

	public static void main(String[] args) {
		
		Scanner s = new Scanner(System.in);
		System.out.print("输入第一个整数:");
		int a = s.nextInt();//读取整数
		System.out.print("输入第二个浮点数:");
		float b = s.nextFloat();//读取浮点数
		float sum = a+b;//结果为float
		System.out.println("结果为:"+sum);
		/**
		 * 需要注意的是,如果在通过nextInt()读取了整数后,再接着读取字符串,读出来的是回车换行:"\r\n",
		 * 因为nextInt仅仅读取数字信息,而不会读取回车换行"\r\n".
		*所以,如果在业务上需要读取了整数后,接着读取字符串,那么就应该连续执行两次nextLine(),
		*第一次是取走回车换行,第二次才是读取真正的字符串
		**/
		
		System.out.print("输入字符串:");
		String kong = s.nextLine();
		String string = s.nextLine();
		System.out.print("读取字符串:"+string);
		
	}

}

2.任意运算单元的长度超过int,小于int

如果有任何运算单元的长度超过int,那么运算结果就按照最长的长度计算 
比如 
int a = 5; 
long b = 6; 
a+b -> 结果类型是long

如果任何运算单元的长度都不超过int,那么运算结果就按照int来计算 
byte a = 1; 
byte b= 2; 
a+b -> int 类型


public class Test2 {

	public static void main(String[] args) {
		int a = 5;
        long b = 6;
        int c = (int) (a+b); //a+b的运算结果是long型,所以要进行强制转换,否则会报错
        long d = a+b;
        
        byte e= 1;
        byte f= 2;
        byte g = (byte) (e+f); //虽然a b都是byte类型,但是运算结果是int类型,需要进行强制转换,否则会报错
        int h = e+f;
        
         
	}

}

二、逻辑操作符

1.长路与 和 短路与


public class Test3 {

	public static void main(String[] args) {
		//长路与  无论第一个表达式的值是true或者false,第二个的值,都会被运算
        int i = 2;
        System.out.println( i== 1 & i++ ==2  ); //无论如何i++都会被执行,所以i的值变成了3
        System.out.println(i);
         
        //短路与 只要第一个表达式的值是false的,第二个表达式的值,就不需要进行运算了
        int j = 2;
        System.out.println( j== 1 && j++ ==2  );  //因为j==1返回false,所以右边的j++就没有执行了,所以j的值,还是2
        System.out.println(j);    

	}

}

2.练习-逻辑操作符


public class Test4 {

	public static void main(String[] args) {
		int i = 1;
		boolean b = !(i++ == 3) ^ (i++ ==2) && (i++==3);
            /** 
              * ^ 优先级高于 &&
              * i++ 值为1 ,!(i++ == 3) 结果为true,i为2;
              * (i++ == 2) 结果为true,i为3;
              * true ^ true 结果为false;
              * &&为短路与,左边为false,右边不执行
              **/
		System.out.println(b);
		System.out.println(i);

	}

}

三、三元操作符

1.练习-判断是否是工作日

 通过Scanner输入一个1-7之间的整数,使用三元操作符判断是工作日还是周末?

import java.util.Scanner;

public class Test5 {

	public static void main(String[] args) {
		System.out.print("今天星期几:");
		Scanner s = new Scanner(System.in);
		String b = null;
		int a = s.nextInt();
		if(a>=1 && a<=7) {
			 b = a < 5 ? "工作日" : "周末";
		}else {
			System.err.println("请输入正确的数字");
		}
		System.out.println("今天是"+b);
	}

}

四、控制流程

1.if 使用过程中可能遇到的坑

在第6行,if后面有一个分号; 而分号也是一个完整的表达式
如果b为true,会执行这个分号,然后打印yes
如果b为false,不会执行这个分号,然后打印yes
这样,看上去无论如何都会打印yes


public class Test6 {
	public static void main(String[] args) {
        boolean b = false;
        
        if (b);
        System.out.println("yes");
	}
}

2.continue


public class Test7 {
   //打印单数
	public static void main(String[] args) {
		for(int i = 0; i < 20; i++) {
			if(i % 2 ==0) continue;//如果是双数,后面的代码不执行,直接进行下一次循环
			System.out.println(i);
		}
	}

}

3.break


public class Test7 {
   //打印双数
	public static void main(String[] args) {
		for(int i = 0; i < 20; i++) {
			if(i % 2 == 1) break;//如果是单数,直接结束循环
			System.out.println(i);
		}
	}

}

4.使用标签结束外部循环


public class Test7 {
	public static void main(String[] args) {
		 //打印单数    
        outloop: //outloop这个标示是可以自定义的比如outloop1,ol2,out5
        for (int i = 0; i < 10; i++) {            
            for (int j = 0; j < 10; j++) {
                System.out.println(i+":"+j);
                if(j%2 == 0) 
                    break outloop; //如果是双数,结束外部循环
            }
             	
        }
	}

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值