1. java基础(个人小总结)(1)

1.正常输出与错误输出

System.out.println("这是正常输出!");

System.err.println("这是错误输出!");

2.输出重定向

setOut(PrintStream) :输出的重定向

setErr(PrintStream) :错误输出的重定向

setIn(InputStream) :输入的重定向

如:PrintStream out=System.out;

PrintStream ps=new PrintStream("./log.txt");

System.setOut(ps);

System.out.println("输出内容到log.txt");

System.setOut(out);

3.Scanner类

Scanner sc=new Scanner(System.in);

System.out.println("请输入相关信息:");

String info=sc.nextLine();

System.out.println("相关信息为:"+info);

注释:System.in,标准的输入流,用于接收用户的输入信息

nextLine():用于从扫描器封装的输入流中获取一行文本字符串

4.字符串与字符数组的相互转化

String str="abcdefg"; //定义字符串

char[] arr=str.toCharArray(); //转化为字符数组

System.out.println(new String(arr));//把字符数组创建字符串中

5.移位运算

一个数字的移位运算,每左移n位就等于这个数乘以2的次方,没右移n位就等于这个数除以2的n次方。

int a=2;

int b,c;

b=a<<2;//左移两位

System.out.println(b); //8

c=b>>1;//右移一位

System.out.println(c);

6.三元运算符

条件运算?运算结果1:运算结果2;

如果条件运算结果为true,返回值就是运算结果1,否则返回结果2.

如:Scanner sc=new Scanner(System.in);

    System.out.println("请输入一个自然数:");

    int i=sc.nextInt();

    System.out.println(i>=0?"输入正确":"输入错误");//运用三目运算符判断

7.运用“%”运算符可以求余数

int i=10;

System.out.println("10除以3的余数是:"+(10%3)); //1

8.货币运算使用BigDecimal

在进行货币运算时,往往需要精确的数组,但是基本数据类型进行的小数运算可能会有误差,而使用BigDecimal可以帮助我们进行精确的小数运算。

下面是BigDecimal的运算方法:

l  加法:add(BigDecimal argue);

l 减法:subtract(BigDecimal argue);

l 乘法:multiply(BigDecimal argue);

l  除法:divide(BigDecimal argue);

示例如下:

BigDecimal money=new BigDecimal("3.1415926");

BigDecimal price=new BigDecimal("2.0");

//加法

BigDecimal result1=money.add(price);

System.out.println(money+"+"+price+"="+result1);//3.1415926+2.0=5.1415926

//减法

BigDecimal result2=money.subtract(price);

System.out.println(money+"-"+price+"="+result2);//3.1415926-2.0=1.1415926

//乘法

BigDecimal result3=money.multiply(price);

System.out.println(money+"*"+price+"="+result3);//3.1415926*2.0=6.28318520

//除法

BigDecimal result4=money.divide(price);

System.out.println(money+"/"+price+"="+result4);//3.1415926/2.0=1.5707963

 

注释:更多关于BigDecimal的知识,可以参考我的博文:BigDecimal的简单使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值