一、if循环
一、排序(插入法)
以三个数为例 a,b,c(从大到小排序)
1.先对两个数a,b进行比较
将较大的数存入a,较小的数存入b
if(a<b){
int temp = a;
a = b;
b = temp;
}
2.再插入c
此时c有三个位置可以插入,即(1) a (2) b (3)。
if(c > a){
System.out.println(c + "," + a +"," + b);
}else if(c > b){
System.out.println(a + "," + c +"," + b);
}else{
System.out.println(a + "," + b +"," + c);
}
3.全过程
如下:
import java.util.Scanner;
/*插入法排序*/
public class ifDemo {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入a");
int a = Integer.parseInt(scanner.nextLine());
System.out.println("请输入b");
int b = Integer.parseInt(scanner.nextLine());
System.out.println("请输入c");
int c = Integer.parseInt(scanner.nextLine());
if(a<b){
int temp = a;
a = b;
b = temp;
}
if(c > a){
System.out.println(c + "," + a +"," + b);
}else if(c > b){
System.out.println(a + "," + c +"," + b);
}else{
System.out.println(a + "," + b +"," + c);
}
}
}
二、猜数
1.random的使用
1.1 导包
import java.util.Random;
1.2 创建对象
Random random = new Random();
1.3 获取随机数
1.直接输出随机数
System.out.println();
2.给范围产生随机值
第一种情况
int a = random.nextInt(min,max);
第二种情况
int a = random.nextInt(10);//默认下限为0,[0.10),包括0,不包括10
2.猜数步骤
1.1产生随机数
System.out.println("请输入下限");
int min = Integer.parseInt(scanner.nextLine());
System.out.println("请输入上限");
int max = Integer.parseInt(scanner.nextLine());
Random random = new Random();
final int TARGET = random.nextInt(min,max);
final:1.是Java中的关键字。
2.final表示最终的,不可变的。
3.final可以修饰变量及方法,类。
1.2 猜数
要想在一次运行过程中猜数直至猜中,使用while循环。
while(true) {
System.out.println("请输入你所猜的数");
int guess = Integer.parseInt(scanner.nextLine());
if(guess == TARGET ){
System.out.println("恭喜猜中啦!!!");
break;
}else if(guess < TARGET ){
min = guess;
System.out.println("没猜中哦,"+"在"+"["+min+"~"+max+"]"+"之间");
}else{
max = guess;
System.out.println("没猜中哦"+"在"+"["+min+"~"+max+"]"+"之间");
}
}
3.全过程
如下:
import java.util.Random;
import java.util.Scanner;
public class GuessNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入下限");
int min = Integer.parseInt(scanner.nextLine());
System.out.println("请输入上限");
int max = Integer.parseInt(scanner.nextLine());
Random random = new Random();
final int TARGET = random.nextInt(min,max);
while(true) {
System.out.println("请输入你所猜的数");
int guess = Integer.parseInt(scanner.nextLine());
if(guess == TARGET ){
System.out.println("恭喜猜中啦!!!");
break;
}else if(guess < TARGET ){
min = guess;
System.out.println("没猜中哦,"+"在"+"["+min+"~"+max+"]"+"之间");
}else{
max = guess;
System.out.println("没猜中哦"+"在"+"["+min+"~"+max+"]"+"之间");
}
}
}
}
二、switch
1.成绩等级划分
1.1输入成绩
对成绩数值进行合法判断
Scanner scanner = new Scanner(System.in);
System.out.println("请输入目标数");
int num = Integer.parseInt(scanner.nextLine());
if(num<0 ||num>100 ){
System.out.println("非法数");}
1.2等级划分
else{
int n = num/10;
switch(n){
case 10:
System.out.println("top");
break;
case 9:
case 8:
System.out.println("A");
break;
case 7:
System.out.println("B");
break;
case 6:
System.out.println("C");
break;
default:
System.out.println("F");
break;
}
1.3全部
如下:
import java.util.Scanner;
public class SwitchDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入目标数");
int num = Integer.parseInt(scanner.nextLine());
if (num < 0 || num > 100) {
System.out.println("非法数");
} else {
int n = num / 10;
switch (n) {
case 10:
System.out.println("top");
break;
case 9:
case 8:
System.out.println("A");
break;
case 7:
System.out.println("B");
break;
case 6:
System.out.println("C");
break;
default:
System.out.println("F");
break;
}
}
}
}
三、易错点
1.if循环
括号中的判断符号 ==