系列文章目录
JavaSE第四节————逻辑控制(下)
逻辑控制
- 顺序结构
- 分支结构
- 循环结构
- 输入输出
- 猜数字游戏
- 练习
一、猜数字游戏
游戏规则
系统自动生成一个随机整数(1-100), 然后由用户输入一个猜测的数字. 如果输入的数字比该随机数小, 提示 “低了”,
如果输入的数字比该随机数大, 提示 “高了” , 如果输入的数字和随机数相等, 则提示 “猜对了” .
生成随机数的方法( * * * )
//生成随机数
Random random = new Random(); // 默认随机种子是系统时间
int toGuess = random.nextInt(100);//生成[0-100)的随机数不包含100
参考代码( * * )
public static void main(String[] args) {
//生成随机数
Random random = new Random(); // 默认随机种子是系统时间
int toGuess = random.nextInt(100);//生成[0-100)的随机数不包含100
Scanner sc = new Scanner(System.in);
// System.out.println("要猜的数字: " + toGuess);
while (true) {
System.out.println("请输入要输入的数字: (1-100)");
int num = sc.nextInt();
if (num < toGuess) {
System.out.println("低了");
} else if (num > toGuess) {
System.out.println("高了");
} else {
System.out.println("猜对了");
break;
}
}
sc.close();
}
二、练习
1.打印年龄分布
根据年龄, 来打印出当前年龄的人是少年(低于18), 青年(19-28), 中年(29-55), 老年(56以上)
import java.util.Scanner; 需要导入 util 包,放在第一行
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if(n <= 18) {
System.out.println("少年");
} else if(n <= 28) {
System.out.println("青年");
} else if(n <= 55) {
System.out.println("中年");
} else {
System.out.println("老年");
}
sc.close();
}
2.判定一个数字是否是素数
import java.util.Scanner; 需要导入 util 包,放在第一行
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int i = 2;
boolean a = true;
while(i <= n/2) {
if(n%i == 0) {
a = false;
break;
}
}
if(a) {
System.out.println(n+"是素数");
} else {
System.out.println(n+"不是素数");
}
sc.close();
}
3. 打印 1 - 100 之间所有的素数
public static void main(String[] args) {
int n = 1;
for(n = 1;n <= 100;n++) {
int i = 2;
boolean a = true;
while(i <= n/2) {
if(n%i == 0) {
a = false;
break;
}
i++;
}
if(a) {
System.out.print(n+" ");
}
}
}
4.输出 1000 - 2000 之间所有的闰年
public static void main(String[] args) {
int a = 1000;
for(a = 1000;a < 2000;a++) {
if(a%100 == 0) {
if(a%400 == 0) {
System.out.println(a+"是闰年");
}
} else if(a%4 == 0) {
System.out.println(a+"是闰年");
}
}
}
5.输出乘法口诀表
public static void main(String[] args) {
int i = 0;
int j = 0;
for(i = 1;i <= 9;i++) {
for(j = 1;j <= i;j++) {
System.out.printf("%d*%d=%d ",j,i,i*j);
}
System.out.println();
}
}
6.求两个正整数的最大公约数( * * )
import java.util.Scanner; 需要导入 util 包,放在第一行
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
boolean con = true;
int c = 1;
for( c = a<b?a:b;c>1;c--) {
if(a%c == 0&&b%c == 0) {
System.out.println(c+"为最大公约数");
con = false;
break;
}
}
if(con) {
System.out.println("没有公约数");
}
}
7.水仙花数( * * * )
求出0~999之间的所有“水仙花数”并输出。(“水仙花数”是指一个三位数,其各位数字的立方和确好等于该数本身,如: 153=1^ 3+5 ^ 3+3^3 ,则153是一个“水仙花数”。)
153 % 10 = 3
153 /10 = 12
12 % 10 = 2
12 / 10 = 1
1 % 10 = 1
1 / 10 =1
方法一
public static void main(String[] args) {
int i = 1;
int x = 0;
double w = 1.0;
for(i = 0;i<1000;i++) {
x = 0;
double p = Math.pow(10.0,w);
while (i/Math.pow(10.0,w) > 1) {
p = i/Math.pow(10.0,w);
w++;
}
int j = i;
while (j>0) {
x += Math.pow(j%10,w);
j /= 10;
}
if (x == i) {
System.out.println(i);
}
}
}
方法二
public static void main(String[] args) {
// 153 = 1^3 + 5^3 + 3^3
for (int i = 1; i < 1000; i++) {
int count = 0;//用来计算i 是几位数
int temp = i;//避免i的值被改变
while (temp != 0){
count++;
temp = temp / 10;
}
//count的值计算完成后,i不改变
//计算i的每一位
temp = i;
int sum = 0;
while (temp != 0){
sum += Math.pow(temp%10,count);
temp /= 10;
}
if(sum == i ){
System.out.println(i);
}
// 153 % 10 = 3
//153 /10 = 12
// 12 % 10 = 2
//12 / 10 = 1
//1 % 10 = 1
//1 / 10 =0
}
}
8.写一个函数返回参数二进制中 1 的个数( * * * )
比如: 15 0000 1111 4 个 1
方法一
7 ---- 0111
7 & 1 ---- 0001
7 ---- 0111
7 >> 1 ---- 0011
import java.util.Scanner; 需要导入 util 包,放在第一行
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int count = 0;
while ( n != 0) {
if ((n&1) == 1) {
count++;
}
n >>= 1;
}
System.out.println(count);
sc.close();
}
方法二
7(n) ---- 0111
6(n-1) ---- 0110
7 & 6 ---- 0110
6 ---- 0110
5 ---- 0101
6 & 5 — 0100
4 ---- 0100
3 ---- 0011
4 & 3 — 0000
import java.util.Scanner; 需要导入 util 包,放在第一行
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int count = 0;
int temp = n;
while ( temp != 0){
count++;
temp = temp & (temp - 1);
}
System.out.println(n + "有" + count + "个1");
}
9.获取一个数二进制序列中所有的偶数位和奇数位,分别输出二进制序列( * * * )
方法一
import java.util.Scanner; 需要导入 util 包,放在第一行
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int j = n;
int o = n;
int i = 0;
while(i < 16) {
System.out.print(j&1);
j >>= 2;
i++;
}
System.out.println();
o >>= 1;
i = 0;
while(i < 16) {
System.out.print(o&1);
o >>= 2;
i++;
}
}
方法二
import java.util.Scanner; 需要导入 util 包,放在第一行
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();//7----0000 0000 0000 0000 0000 0000 0000 0111
//从左往右数
//奇数位
System.out.print("奇数位");
for (int i = 31; i >=0 ; i-=2) {//有32位二进制数因此i= 31 i>= 0 ,>>一次判断是否是1
System.out.print(((n >>> i) & 1) +" " );
}
System.out.println();
//偶数位
System.out.print("偶数位");
for (int i = 30; i >=0 ; i-=2) {//有32位二进制数因此i= 31 i>= 0 ,>>一次判断是否是1
System.out.print(((n >>> i) & 1) +" " );
}
}