#java笔记
##常量
例如
System.out.println("hello world");
##变量
例如
int i = 10;
int j = 20;
int k = 30;
System.out.println(i);
##字符串
例如
System.out.println("fghjk"+ "fghj" + 666);
System.out.println("fghjk"+ "fghj" + 666 + 2);
System.out.println(666 + 2 + "fghjk"+ "fghj"
仔细看他们的结果,是不一样的
这三个字符串的结果分别是:
fghjkfghj666,
fghjkfghj6662,
668fghjkfghj
&&和& | 和 ||
System.out.println((i++ > 100) & (j++ > 100)); //F & F
System.out.println((i++ > 100) && (j++ > 100)); //F && F
- &无论左边真假 右边都要执行
- && 如果左边为真,则右边执行,如果左边为假则右边不执行
- | 无论左边真假 右边都要执行
- || 如果左边为真,则右边执行,如果左边为假则右边不执行
##三元运算符
格式:关系表达式 ? 表达式1 :表达式2
a>b ? a:b
- 首先计算表达式的值
- 如果为T则表达式1的值为运算结果
- 如果为F则表达式2的值为运算结果
案例:两个数&三个数比较
public class example {
public static void main(String[] args) {
// 两个数
int a = 180;
int b = 200;
boolean c = a == b ? true : false;
System.out.println("c:"+ c);
// 三个数比较
int h1 = 150;
int h2 = 210;
int h3 = 165;
int max1 = h1 > h2 ? h1 : h2;
int max2 = max1 >h3 ? max1 :h3;
System.out.println("max" + max2);
}
}
这段代码的结果是:
c:false max210
##数据输入
输入函数包Scanner
创建对象
Scanner sc = new Scanner(System.in);
接收数据
int x = sc.nextInt();
System.out.println("x=" +x);
注意的是使用此功能要添加Scanner包(idea一般都会自动添加)
例子:
import java.util.Scanner;
public class Scan {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入H1");
int h1 = sc.nextInt();
System.out.println("输入H2");
int h2 = sc.nextInt();
System.out.println("输入H3");
int h3 = sc.nextInt();
int max1 = h1 > h2 ? h1 : h2;
int max = max1 > h3 ? max1 : h3;
System.out.println("MAX=" + max);
}
}
##流程控制
###分支
IF Switch
###循环
for
while
do…while
IF Switch
先是If
import java.util.Scanner;
public class IFswitch {
public static void main(String[] args) {
int i = 1;
int j = 2;
// if
if (i > j) {
System.out.println("为 T 执行内部语句 接着执行下边的");
}
System.out.println("为F执行外部语句");
然后是IF…else
if (i < j) {
System.out.println("为T执行1表达式");
} else {
System.out.println("为F执行表达式2");
}
案例:奇偶数
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个数来判断是否是奇偶数");
int number = sc.nextInt();
if (number % 2 == 0) {// number 除2取模
System.out.println("number:" + number + "为偶数");
} else {
System.out.println("number:" + number + "为奇数");
}
else if
Scanner EL = new Scanner(System.in);
System.out.println("输入一个数");
int num2 = EL.nextInt();
if(num2 == 1){
System.out.println("此时num2 的值为1");
}else if(num2 == 2){
System.out.println("此时num2 的值为2 执行了elseif语句");
}else {
System.out.println("此时num2的值既不是1也不是2");
}
案例
Scanner re = new Scanner(System.in);
System.out.println("请输入你的成绩");
Float score = re.nextFloat();
if (score > 100){
System.out.println("输入的成绩有误");
}else {
if(score>=95 && score<=100){
System.out.println("牛逼");
}else if(score>=80){
System.out.println("可以");
}else if(score>=60){
System.out.println("还行");
}else {
System.out.println("菜狗");
}
}
- 首先判断score是否为T 如果为T 则执行右边 如果右边同样为T 则执行下面的语句
- 此流程仅作为学习使用 实际&& 后边可以省略
- 因为当使用 score >100 判断错误时 score <=100永远为T
###Switch
! ! 在使用Switch时case只允许使用常量
Scanner SW = new Scanner(System.in);
System.out.println("输入一个数(Switch)");
int SWI = SW.nextInt();
switch (SWI){
case 1:
System.out.println("此时输入的是1");
break;
case 2:
System.out.println("此时输入的是2");
break;
default:
System.out.println("此时输入的既不是1也不是2");
break;
}
案例:
Scanner mo = new Scanner(System.in);
System.out.println("请输入季节");
int month = mo.nextInt();
switch (month){
case 2:
case 3:
case 4:
// 当输出结果一样时可以使用代码合并 将几个case共同使用一个print
System.out.println("春季");
break;
case 5:
case 6:
case 7:
System.out.println("夏季");
break;
case 8:
case 9:
case 10:
System.out.println("秋季");
// 当case后不加brake时 输入此之后会接着执行下面的与语句不会结束 break;
default:
System.out.println("我不想写了");
}
}
}
###For 和 while
####For 循环
判断为T时 循环才会继续 F退出
定义初始条件->定义判断标准->定义执行方式
案例
public class Forwhile {
public static void main(String[] args) {
for (int i = 1; i <= 3; i++) {
System.out.println("这是for循环");
}
案例1 输出1-5
for (int j = 1; j <= 5; j++) {
System.out.println(j);
}
案例2 求和
int sum = 0;
for (int l = 1; l <= 5; l++) {
sum += l;
}
System.out.println(sum);
案例3 求偶数和
int su = 0;
for (int h = 1; h <= 100; h++) {
if (h % 2 == 0) {
su += h;
}
}
System.out.println(su);
案例4 求水仙花数
371 3的3次 +7的3次 +1的3次 =371
int y = 371;
// 个位
System.out.println(y % 10);
// 百位
System.out.println(y / 100);
// 十位
System.out.println(y / 10 % 10);
int count = 0; //统计变量
for (int u = 100; u <= 999; u++) {
int a = u % 10; //个位
int b = u / 100; //十位
int c = u / 10 % 10; //百位
if (Math.pow(a, 3) + Math.pow(b, 3) + Math.pow(c, 3) == u) {
// 引用Math函数
count++;
System.out.println(u);
}
}
System.out.println(count); //输出统计count值
####While循环
- while 会判断是否为T/F
- 如果为F则循环结束
- 如果为T则继续执行
- while 和 for 可以互相转换
- while 一般用于不知道多少次循环的循环
int q = 1;
while(q<=5){
System.out.println("Hello world (while)");
q++;
}
案例
double w =0.1;
int coun = 0;
while (w<=8844430){
w = w * 2;
coun++;
}
System.out.println(coun);
####do…while
do…while先写循环在写判断
int e =0;
do{
System.out.println("Hello,world (do...while)");
e++;
}while (e<=3);
}
}
- 三种循环的区别 for & while & do…while
- for 和 while 是先做条件判断 在做循环 当条件不成立时 一次循环都不执行
- 定义初始值时 [i] for循环的[i]是被定义在循环结构中
- 而while的[i]则是可以反复使用的[int i = 1]
- 当出现死循环时for 和 while 和 do…while都会无限执行
- do…while 至少会执行一次
循环嵌套
public class timefor {
public static void main(String[] args) {
for (int hour = 0;hour<3;hour++){
for (int min =0 ;min<3;min++){
System.out.println(hour + "时" + min + "分");
}
}
}
这段代码的结果是
0时0分
0时1分
0时2分
1时0分
1时1分
1时2分
2时0分
2时1分
2时2分
##随机数
需要的包:import java.util.Random;
用法:
Random r = new Random();
int number = r.next.Int(10)
这段代码的取值范围是[0,10)
案例:
import java.util.Random;
public class rando {
public static void main(String[] args) {
for (int i = 0; i < 11; i++) {
Random r = new Random();
int num = r.nextInt(10);
System.out.println(num);
}
}
}
##数组
数组是用来存储大量相同类型数组的
创建数组的格式是
int [] arr
这段代码定义了一个int型的数组,数据名是arr
#####动态初始化
指定数组长度
格式:
数据类型[]变量名 = new 数据类型 [长度]
int[] arr = new int [3]
- 数组元素访问
- 数组变量访问方式
格式:数组名 - 数组内部保存吗的数据的访问方式
- 格式:数组名[索引]
- 索引是数组中的数据编号方式
- 索引用于访问数组中的数据使用,数组名[索引]等同于变量名,是一种特殊的变量名
索引是由0开始 - 是连续的
- 逐渐增加每次加一
代码示例
System.out.println(arr);
System.out.println(arr[0]);
System.out.println(arr[1]);
System.out.println(arr[2]);
arr[0] = 100;
arr[2] = 200;
System.out.println(arr[0]);
System.out.println(arr[1]);
System.out.println(arr[2]);
此段代码要结合上边的创建数组一起使用
#####静态初始化
就是指定数据在数组里
int [] name = {1,2,3,4};
System.out.println(name);
System.out.println(name[1]);
#####关于数组的常见问题
- 索引越界 访问了数组中不存在的索引对应元素,造成索引越界问题
- 空指针异常 访问的是这样已经不再指向堆内存的数据,造成空指针异常
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。