粗糙笔记不喜勿喷
Java 8大基本类型
第一类:逻辑型(boolean)
1.boolean类型只存在true(真),false(假)两种形式
例: boolean a=true; boolean b=false;
第二类:文本型(char)
字符常量通常用单引号括起来(可以是中文)java采用unicode编码,每个字符占两个字节,
例: char a=’a’;char b=’你’;
第三类:整数型(byte、short、int、long)
byte: 8 位,用于表示最小数据单位,如文件中数据,-128~127
short: 16 位,很少用 从-32768到32767
int: 32 位、最常用,-2^31-1~2^31 (21 亿) 从-2147483648,到2147483647共10位
long: 64 位、次常用 从-9223372036854775808到9223372036854775807共19位
第四类:浮点型(float、double)
float: 32 位,后缀 F 或 f,1 位符号位,8 位指数,23 位有效尾数
double: 64 位,最常用,后缀 D 或 d,1 位符号位,11 位指数,52 位有效尾
注意:
在数学中0到1有无数个浮点数;而计算机是离散的,所以表示的时候有误差,计算机用精度(小数点后几位来表示正确),比较浮点数时a==0.1是不合适的,应该a-0.1==0;如果a是0.1,则即使有误差 a-0.1==0因为a和0.1都被表示为一个有误差的计算机二进制
特别说明(String)
String
不是基本类型而是引用类型。
类型转换
自动转换:byte–>short–>int–>long–>float–>double
注意:
小可转大,大转小会失去精度
String转换成int或者double
int 或 Integer num = Integer.parseInt(String str);
double 或 Double num = Double.parseDouble(String str);
整数 int 转换成字串 String
int a=9;
String s = String.valueOf(a);
Java中的变量
类型 变量名称 = 值
可变变量
class TestVeriable{
public static void main(String args[]){
int i1=10;
short s1=2;
int i2 = i1+s1;
float f1=12.5F;
float f2=f1+i2;
long l=12L;
float f3 = l;
char c1= 'a';
char c2= 'A';
int i3 = c1+1;
int i4= c2+1;
//short、byte、char之间的运算结果都被自动转化为int类型
short ss1=12;
byte bb1= 1;
char cc1='a';
int ii1=ss1+bb1+cc1;
}
}
不可变变量(final)
final 类型 变量名称 = 值
public class one {
public static void main(String[] args) {
final int a = 1;
a = 2;//因a不可变所以这里会报错
}
}
类变量(private)
private 类型 变量名称 = 值
public class one {
private int two = 1; //只能在这个类中使用
public static void main(String[] args) {
//此处申明会private报错
}
}
Java循环
while循环
先循环再判断
import java.util.*;
public class test{
public static void main(String[]args){
Scanner a=new Scanner(System.in);//创建扫描器(可输入多个值空格隔开)
System.out.println("请输入你的密码");
while(true){ //<--------while在这里
int password=a.nextInt();//获取扫描器的值(如一次输入多个值再来一个nextInt()获取)
System.out.println("密码不正确请再输入一次");
if(password!=123456){
continue;
}
break;
}
System.out.println("密码正确!");
}
}
常用从Scanner获取数据方法:
public class one {
public static void main(String[] args) {
Scanner a = new Scanner(System.in);
String aa = a.next();//获取字符串类型
int bb = a.nextInt();//获取int类型
double cc = a.nextDouble();//获取double类型
float dd = a.nextFloat();//浮点类型
}
}
do while循环
先执行do
再执行while
(先判断再循环)
import java.util.*;
public class test{
public static void main(String[] args){
Scanner a=new Scanner(System.in);
int b;
Random c=new Random();//随机模块
int d=c.nextInt(101);
do{
System.out.println("猜猜电脑随机生成的数是多少?");
b=a.nextInt();
if(b<d){
System.out.println("小了");
}else if(b>d){
System.out.println("大了");
}
}while(b!=d);
System.out.println("恭喜答对了");
}
}
for 循环
demo1
public static void main(String[] args){
int sum = 0;
//int i= 1;就是计数器的初始化,只初始化一次
//i <= 100就是循环条件,循环多少次就判断多少次
//i++计数器的累加,循环多少次就累加多少次,执行时机是当前这次循环执行完毕
for(int i = 1; i <= 100; i++){
if(i%2 == 0){
sum += i;
}
}
System.out.println("1~100的偶数和是:"+sum);
}
}
demo2
class Demo2{
/*
打印乘法口诀
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
....
1*9=9 2*9=18..... 9*9=81
*/
public static void main(String[] args){
//行的循环
for(int i = 1; i <= 9; i++){
for(int j = 1; j <= i; j++){
System.out.print(j+"*"+i+"="+(i*j)+"\t");
}
System.out.println();
}
}
}
demo3
import java.util.*;
public class test{
public static void main(String[]args){
Scanner a=new Scanner(System.in);
double sum=0;
double ave=0;
for(int i=1;i<=5;i++){//i = 1是申明的变量初始值,i<5循环的条件,i++步长,每次加1
System.out.println("请输入你的第"+i+"门成绩");
int score=a.nextInt();
sum=sum+score;
ave=sum/5;
}
System.out.println("你的平均成绩是:"+ave);
}
}
for each
import java.util.*;
public class one {
public static void main(String[] args) {
String[] a = {"123","456","789"};
for (String i : a ) { //<--------在这里
System.out.println(i);
}
}
}
结果:123 456 789
break continue
下面的代码展示了break、continue 及循环标签(break和continue可以指定标签继续循环或跳出)
public class one {
public static void main(String[] args) {
int count = 0;
loop1 : while (count <= 10) {//loop1是循环标签可以不写,如果写了名字可随意
loop2 :for (int i = 0;i <= 5;i++) {//循环5次
if (i == 1) {//做个演示没什么意义
System.out.println(1);
continue loop2;//结束本次循环继续下次for循环
}
if (i <= 5) {//做个演示没什么意义
System.out.println(2);
break loop1;//指定跳出while
}
}
count += 1;
}
}
}
判断
if else
public class one {
public static void main(String[] args) {
int count = 1;
while (count <= 10) {//循环10次
if (count == 7) {//判断次数是否到7
System.out.println("到" + count + "了");
}
if (count == 8) {//判断次数是否到8
System.out.println("到" + count + "了");
}
if (count == 9) {//判断次数是否到9
System.out.println("到" + count + "了");
}
else {
System.out.println("到" + count + "了");
}
count += 1;
}
}
}
结果:到7了 到8了 到9了 到10了
注意:
为提高效率多个if ..if...if
(可写为if...else if
提高效率)
switch
public class one {
public static void main(String[] args) {
int count = 1;
while (count <= 10) {//循环10次
switch(count) {
case 7: //判断次数是否到7
System.out.println("到" + count + "了");
break;//记得要break不然会出现意想不到的后果如多循环了几次
case 8: //判断次数是否到8
System.out.println("到" + count + "了");
break;
case 9: //判断次数是否到9
System.out.println("到" + count + "了");
break;
case 10: //判断次数是否到10
System.out.println("到" + count + "了");
break;
// default: //同else
// System.out.println("不满足的条件都算我的");
}
count += 1;
}
}
}
结果:到7了 到8了 到9了 到10了
注意:
case完成记得一定要break不然会出现意想不到的后果,如:多循环了几次