java知识点

(1)final

public class HelloWorld {

    public void method1(final int j) {
        j = 5; //这个能否执行?
    }
}
不可以,因为在调用方法的时候,就一定会第一次赋值了,后面不能再进行多次赋值

(2)异或   ^

public class HelloWorld {
    public static void main(String[] args) {
        boolean a = true;
        boolean b = false;
         
        System.out.println(a^b); //不同返回真
        System.out.println(a^!b); //相同返回假
 
    }
}

(3)三元操作符

public class HelloWorld {
    public static void main(String[] args) {
 
        int i = 5;
        int j = 6;
 
        int k = i < j ? 99 : 88;
 
        // 相当于
        if (i < j) {
            k = 99;
        } else {
            k = 88;
        }
 
        System.out.println(k);
 
    }
}

//////////////////////////////////////////////////////////////

import java.util.Scanner;

public class HelloWorld {
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        System.out.println("今天周几?");
        int day = s.nextInt();
        String status = day>=6?"周末":"工作日";
        System.out.println("今天是"+status);

    }
}

(4)读取了整数后,接着读取字符串

import java.util.Scanner;
   
public class HelloWorld {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int i = s.nextInt();
        System.out.println("读取的整数是"+ i);
        String rn = s.nextLine();
        String a = s.nextLine();
        System.out.println("读取的字符串是:"+a);
    }
}

(5)条件为true时 重复执行,至少会执行一次

public class HelloWorld {
    public static void main(String[] args) {
         
        //打印0到4
        //与while的区别是,无论是否成立,先执行一次,再进行判断
        int i = 0;
        do{
            System.out.println(i);
            i++;           
        } while(i<5);
         
    }
}

(6)n的阶乘

import java.util.Scanner;

public class HelloWorld {
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int f = 1;
        while(n>=1){
            f = f*n;
            n--;
        }
        System.out.println(f);
    }
}

(7)总长度,左对齐,补0,千位分隔符,小数点位数,本地化表达

æ»é¿åº¦ï¼å·¦å¯¹é½ï¼è¡¥0ï¼åä½åé符ï¼å°æ°ç¹ä½æ°ï¼æ¬å°å表达

package digit;
  
import java.util.Locale;
   
public class TestNumber {
   
    public static void main(String[] args) {
        int year = 2020;
        //总长度,左对齐,补0,千位分隔符,小数点位数,本地化表达
          
        //直接打印数字
        System.out.format("%d%n",year);
        //总长度是8,默认右对齐
        System.out.format("%8d%n",year);
        //总长度是8,左对齐
        System.out.format("%-8d%n",year);
        //总长度是8,不够补0
        System.out.format("%08d%n",year);
        //千位分隔符
        System.out.format("%,8d%n",year*10000);
  
        //小数点位数
        System.out.format("%.2f%n",Math.PI);
          
        //不同国家的千位分隔符
        System.out.format(Locale.FRANCE,"%,.2f%n",Math.PI*10000);
        System.out.format(Locale.US,"%,.2f%n",Math.PI*10000);
        System.out.format(Locale.UK,"%,.2f%n",Math.PI*10000);
          
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值