Java基础语法笔记

boolean flag = true;
  • System.out.println("hello" + 100 + 3);// 输出hello1003
  • System.out.println(100 + 3 + "hello"); //输出103hello
  • 浮点数=符号位+指数位+尾数位
  • 错误写法:float num2 = 1.1;       正确写法:double num3 = 1.1f;
  • double num = 8.1 / 3 != 2.7 (2.66666667)
if(Math.abs(num1 - num2) < 0.000001) {
    System.out.println("相等");
}
boolean flag = true;

·bool类型只能是true / false 不能是0 或 1(和c语言不同)


基本类型转String

String str = n1 + "";

String 转 int

String s = “123”
int num1 = Integer.parseInt(s5);//字符串转int

程序中+号的使用

System.out.println(100 + 3 + "hello");//103hello
System.out.println("hello" + 100 + 3);//hello1003

键盘输入:

import java.util.Scanner;

public class a {
	public static void main(String[] args) {
		Scanner myScanner = new Scanner(System.in);
		int age = myScanner.nextInt();
		String name = myScanner.next();
		System.out.println(age + name);
	}
}

计算机运算以补码形式实现


取第一个字符:

char c1 = myScanner.next().charAt(0);

switch case

switch( ){

case  _  : ....;

               break;

default : ...;

}


Math.random()随机选取大于0.0小于1.0的double值

System.out.println((int)(Math.random() * 100);

String name = "lc";
System.out.println("lc".equals(name));
int hens[] = new int[5];
for(int i = 0; i < hens.length; i++)
    hens[i] = myScanner.nextInt();

列举A - Z

chars[i] = (char)('A' + i); //需要强制转换

int arr1[] = {1, 2, 3};
int arr2[] = arr1; 

若改变arr1[1]的值,arr2[1]的值也会随之改变


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值