练习题
1.用for循环和while循环完成一个9*9乘法表
/*
用for循环完成一个9*9乘法表
*/
class Demo1 {
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 + " ");
}
System.out.println();
}
}
}
/*
用while循环完成一个9*9乘法表
1 * 1 = 1
2 * 1 = 2 2 * 2 = 4
3 * 1 = 3 3 * 2 = 6 3 * 3 = 9
【嵌套循环】
1. 总计9 行数据
2. 每一行数据和当前行号一致
*/
class Demo2 {
public static void main(String[] args) {
int i = 1;
while (i <= 9) {
int j = 1;
while (j <= i) {
System.out.print(j + "*" + i + "=" + i*j + " ");
j += 1;
}
System.out.println();
i += 1;
}
}
}
2.用while循环和for循环将一个正整数进行分解质因数操作
/*
将一个正整数进行分解质因数操作 例如: 输入90 结果 2*3*3*5
*/
class Demo3 {
public static void main(String[] args) {
int i = 2;
int num = 40;
while (i <= num) {
if (num % i == 0) {
System.out.println("i : " + i);
num /= i;
}
i -= 1;
}
}
}
/*
输入一个正整数,将正整数进行分解质因数操作
例如: 输入90 结果 2*3*3*5
*/
import java.util.Scanner;
class Demo4 {
public static void main(String[] args) {
System.out.println("请输入一个正整数:");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
System.out.print(num + "=");
for(int i = 2;i <= num/2;i++) {
if(num % i == 0){
System.out.print(i + "*");
num /= i;
i--;
}
}
System.out.println(num);
}
}
3.30以内斐波那契数列
class Demo5 {
public static void main(String[] args) {
int x = 1;
int y = 0;
int sum = 0;
for (int i = 1; i <= 30; i++) {
sum = x + y;
x = y;
y = sum;
System.out.println(sum);
}
}
}
4.利用for循环完成15的阶乘
/*
利用for循环完成15的阶乘
*/
import java.util.Scanner;
public class Demo6 {
public static void main(String[] args) {
System.out.println("请输入一个整数:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sum = 1;
for (int i = n;i > 0; i--) {
sum *= i;
if (n == 1) {
System.out.print(n + "! = " + i+ "*" + i);
}else if (i == n) {
System.out.print(n + "! = " + i);
}else{
System.out.print("*" + i);
}
}
System.out.print(" = " + sum);
}
}
5.逢七过游戏
/*
逢七过,1 ~ 100以内的所有数值展示,
如果带有7或者和7有关,打印过
*/
class Demo7 {
public static void main(String [] args) {
int num1 = 1;
int num2 = 1;
int num3 = 1;
for (int i = 1;i <= 100; i++) {
num1 = i % 7;
num2 = i / 10;
num3 = i % 10;
if (num1 == 0 || num2 == 7 || num3 == 7) {
System.out.println(i);
}
}
}
}
6.输入三个整数比较,输出最大值
/*
流程控制(数值比较2)
定义三个整型变量x,y,z,从键盘初始化变量值,判断三个变量的大小,将较大的值
赋给变量max,将max输出,注意输入使用Scanner输入
*/
import java.util.Scanner;
class Demo8 {
public static void main(String[] args) {
int x = 0;
int y = 0;
int z = 0;
int max = 0;
Scanner sc = new Scanner(System.in);
System.out.println("请输入三个整数:");
x = sc.nextInt();
y = sc.nextInt();
z = sc.nextInt();
// 假设最大值是 x,max保存x的值,参照物来完成
max = x;
// 判断y的值是否大于max
if (y > max) {
max = y;
}
// 判断z的值是否大于max
if (z > max) {
max = z;
}
System.out.println("max : " + max);
}
}
7.输入月份,输出天数
/*
流程控制(月份天数判断)
输入一个月数,然后输出对应月份有多少天(不考虑闰年),将天数输出,注意输入
使用Scanner输入
比如:
输入 6 输出为30
输入 2 输出为28
*/
import java.util.Scanner;
class Demo9 {
public static void main(String[] args) {
// 方案1 switch case
int month = 0;
Scanner sc = new Scanner(System.in);
System.out.println("请输入月份:");
month = sc.nextInt();
if (month < 1 || month > 12) {
System.out.println("输入错误");
System.exit(0);
}
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
System.out.println("31 天");
break;
case 4:
case 6:
case 9:
case 11:
System.out.println("30 天");
break;
case 2:
System.out.println("28 天");
break;
default:
System.out.println("输入错误");
break;
}
}
}
/*
流程控制(月份天数判断)
输入一个月数,然后输出对应月份有多少天(不考虑闰年),将天数输出,注意输入
使用Scanner输入
比如:
输入 6 输出为30
输入 2 输出为28
*/
import java.util.Scanner;
class Demo10 {
public static void main(String[] args) {
// 方案1 switch case
int month = 0;
Scanner sc = new Scanner(System.in);
System.out.println("请输入月份:");
month = sc.nextInt();
if (month < 1 || month > 12) {
System.out.println("输入错误");
System.exit(0);
}
/*
month == 1 1 == month 两个结果展示效果是一致的!!!
没有区别,没有错误
1 == month 常量在前,如果代码书写中缺少了一个 = 号,编译报错
可以形成一些小的习惯,快速找到代码错误。
month == 1 没有问题,但是如果缺少了一个 = 号,有可能代码编译
通过,整个运算结果就不是代码所预期的结果。
*/
if (1 == month || 3 == month || 5 == month || 7 == month
|| 8 == month || 10 == month || 12 == month) {
System.out.println("31 天");
} else if (2 == month) {
System.out.println("28 天");
} else {
System.out.println("30 天");
}
}
}
整理笔记