|和||区别
区别在于,执行到一半就可以判断结果了,因此双写可能只执行一半
因此被称为短路与,短路或
例如:
以下情况只执行括号内即可确定结果
0&&1==(0&1)&1
1 || 1 ==(1|1)|1
三元运算符:两数相等问题
import java.util.Scanner;
public class b {
public static void main(String[] args) {
Scanner scanf = new Scanner(System.in);
System.out.println("请输入两只老虎体重");
System.out.println(scanf.nextInt() == scanf.nextInt() ? "相等" : "不相等");
}
}
三元运算符:三数最大问题
import java.util.Scanner;
public class b {
public static void main(String[] args) {
Scanner scanf = new Scanner(System.in);
System.out.println("请输入三只老虎体重");
int a = scanf.nextInt();
int b = scanf.nextInt();
int c = scanf.nextInt();
System.out.println("最重的老虎重达:" + (a > b ? a > c ? a : c : b > c ? b : c));
}
}
分支结构:奇数偶数?
import java.util.Scanner;
import javax.swing.plaf.basic.BasicTreeUI.TreeCancelEditingAction;
public class b {
public static void main(String[] args) {
Scanner scanf = new Scanner(System.in);
while (true) {
System.out.println("请输入数值:");
System.out.println((scanf.nextInt() % 2) == 0 ? "是偶数n" : "是奇数n");
}
}
}
分支语句:考试奖励
import java.util.Scanner;
import javax.swing.plaf.basic.BasicTreeUI.TreeCancelEditingAction;
public class b {
public static void main(String[] args) {
Scanner scanf = new Scanner(System.in);
while (true) {
System.out.println("请输入分数:");
int socre = scanf.nextInt();
if (socre >= 80 & socre < 90) {
System.out.println("奖励玩具");
} else if (socre >= 90 && socre < 95) {
System.out.println("奖励swich游戏机");
} else if (socre >= 95 && socre <= 100) {
System.out.println("奖励大奔");
} else {
System.out.println("奖励一皮带");
}
}
}
}
分支switch语句:春夏秋冬
import java.util.Scanner;
import javax.swing.plaf.basic.BasicTreeUI.TreeCancelEditingAction;
public class b {
public static void main(String[] args) {
Scanner scanf = new Scanner(System.in);
while (true) {
System.out.println("请输入月份:");
int moon = scanf.nextInt();
moon = moon > 0 & moon < 4 ? 1
: moon > 3 && moon < 7 ? 2
: moon > 6 & moon < 10 ? 3
: moon > 9 & moon < 13 ? 4 : 0;
switch (moon) {
case 1:
System.out.println("春季");
break;
case 2:
System.out.println("夏季");
break;
case 3:
System.out.println("秋季");
break;
case 4:
System.out.println("冬季");
break;
default:
System.out.println("输错了月份");
break;
}
}
}
}
循环语句for语句:电风扇循环
import java.util.Scanner;
import javax.sql.rowset.CachedRowSet;
import javax.swing.plaf.basic.BasicTreeUI.TreeCancelEditingAction;
public class b {
public static void main(String[] args) {
Scanner scanf = new Scanner(System.in);
for (;;) {
int level = scanf.nextInt();
System.out.println("请输入电风扇级数");
switch (level) {
case 0:
System.out.println("关闭电风扇");
break;
case 1:
System.out.println("现在电风扇风俗一档");
break;
case 2:
System.out.println("现在电风扇风俗二档");
break;
case 3:
System.out.println("现在电风扇风俗三档");
break;
default:
System.out.println("现在电风扇风俗四档");
break;
}
}
}
}
累加计算器
import java.util.Scanner;
import javax.sql.rowset.CachedRowSet;
import javax.swing.plaf.basic.BasicTreeUI.TreeCancelEditingAction;
public class b {
public static void main(String[] args) {
Scanner scanf = new Scanner(System.in);
int num = 0;
for (;;) {
int sum = scanf.nextInt();
num = num + sum;
System.out.println("当前数值" + num);
}
}
}
水仙花
// 水仙花
public class b {
public static void main(String[] args) {
int sum=0;
for (int i = 100; i <= 999; i++) {
int a, b, c;
a = i / 100;
b = i % 100 / 10;
c = i % 10;
if (a * a * a + b * b * b + c * c * c == i) {
sum++;
System.out.println(i);
}
}
System.out.println("总计"+sum+"个水仙花数");
}
}
循环语句:while
珠穆朗玛峰
public class b {
public static void main(String[] args) {
// 珠穆朗玛峰高8844.43m
// 一张纸薄0.1mm,一张纸对折多少次后能超过珠穆朗玛峰?
int a = 1;
int mouton = 88444300;
int count = 0;
while (a < mouton) {
a = a * 2;
count++;
}
System.out.println("经过" + count + "次对折可以超过珠穆朗玛峰");
double num = a / 10000.00;
System.out.println("高度达到:" + num + "米");
}
}
do…while()
public class b {
public static void main(String[] args) {
do {
System.out.println("只执行一次");
} while (false);
}
}
break,continue
public class b {
public static void main(String[] args) throws Exception {
for (int i = 0; i < 10; i++) {
if (i == 5) {
System.out.println("终止循环");
break;
}
if (i == 3) {
System.out.println("本次循环不执行");
continue;
}
System.out.println("循环第:" + i + "次");
}
}
}
java中的随机数
import java.util.Random;
public class b {
public static void main(String[] args) throws Exception {
Random r = new Random();
while (true) {
Thread.sleep(1000);
// 输出随机一个小于1000的数
System.out.println(r.nextInt(1000));
// 如果我要一定范围的呢?50-100?
System.out.println(r.nextInt(50) + 50);
// 输出随机一个double的小数
System.out.println(r.nextDouble());
}
}
}
大数问题:斐波那契数列第100位的数值
当一个数超过了long所能表示的范围怎么办?
import java.time.temporal.TemporalField;
import java.util.Scanner;
import java.util.function.Function;
public class b {
static long up1 = 0;
static long up2 = 0;
//
static void function(long a, long b, int N) {
long temp = a + b;
a = b;
b = temp;
// 高位进位
long tempup = up1 + up2;
up1 = up2;
up2 = tempup;
// 判断是否进位
if (b >= 1000000000) {
b = b % 1000000000;
up2++;
}
N--;
if (N == 0) {
System.out.print(up2 + " ");
System.out.println(b);
} else {
function(a, b, N);
}
}
public static void main(String[] args) {
// 这里N要-2
function((long) 1, (long) 1, 100);
// 354224848179261915075
}
}
输出1-100
import java.util.Scanner;
import javax.sql.rowset.CachedRowSet;
import javax.swing.plaf.basic.BasicTreeUI.TreeCancelEditingAction;
public class b {
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
System.out.print((i + 1) + " ");
if ((i + 1) % 10 == 0) {
System.out.println();
}
}
}
}
数组
int arry[]等同于 int[] arry
new int [长度]{初始化,可选项};
也可以直接初始化
int arry[]={1,2,3,4};



