流程控制练习
1. 打印三角形
public class TestDemo {
public static void main(String[] args) {
//打印三角形,拆分为4个三角形,前面为空三角,后面为用*为实三角
for (int i=1;i<=5;i++){
for (int x=5;x>=i;x--){
System.out.print(" ");
}
for (int y=1;y<=i;y++){
System.out.print("*");
}
for (int z=1;z<i;z++){
System.out.print("*");
}
System.out.println();
}
}
}
2.肥胖指数(BIM)
收集你的身高体重,并计算出你的BMI值是多少
BMI的计算公式是 体重【kg】 / (身高【米】*身高【米】
public class IfDemo01 {
public static void main(String[] args) {
//使用Scanner收集你的身高体重,并计算出你的BMI值是多少
//BMI的计算公式是 体重【kg】 / (身高【米】*身高【米】)
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的姓名:");
String s = scanner.nextLine();
System.out.println("请输入你的身高(cm):");
double x = scanner.nextDouble();
System.out.println("请输入你的体重(kg):");
double y = scanner.nextDouble();
double BMI =y/((x/100)*(x/100));//身高厘米转化为米
System.out.println("姓名:"+s);
System.out.println("身高:"+x+"cm");
System.out.println("体重:"+y+"kg");
System.out.println("BMI:"+BMI);
if (BMI<18.5){
System.out.println("身体状态:体重过轻");
}else if (BMI>=18.5 && BMI<24){
System.out.println("身体状态:正常范围");
}else if (BMI>=24 && BMI<27){
System.out.println("身体状态:体重过重");
}else if (BMI>=27 && BMI<30){
System.out.println("身体状态:轻度肥胖");
}else if (BMI>=30 && BMI<35){
System.out.println("身体状态:中度肥胖");
}else {
System.out.println("身体状态:重度肥胖");
}
}
}
3.判断某一年是否为闰年
闰年判断标准(满足任何一个)
1.如果能够被4整除,但是不能被100整除
2.能够被400整除
public class IfDemo02 {
public static void main(String[] args) {
//判断某一年是否为闰年
//闰年判断标准(满足任何一个)
//1. 如果能够被4整除,但是不能被100整除
//2. 能够被400整除
while (true){//输入正确的格式,有break终止循环,格式错误时,由于没有break,循环设置为(true)则重复操作
Scanner scanner = new Scanner(System.in);
System.out.println("输入年份:");
if (scanner.hasNextInt()){//判断是否输入的是整数
int year = scanner.nextInt();
if ((year%4==0 && year%100!=0) || year%400==0){
System.out.println(year+"是闰年");
break;
}else {
System.out.println(year+"不是闰年");
break;
}
}else {
System.out.println("输入格式错误");
}
}
}
}
4.输入月份,然后使用switch 判断季节
public class Demo06 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("输入月份:");
int month = scanner.nextInt();
switch (month){
case 3:
case 4:
case 5:
System.out.println("春季");
break;
case 6:
case 7:
case 8:
System.out.println("夏季");
break;
case 9:
case 10:
case 11:
System.out.println("秋季");
break;
case 12:
case 1:
case 2:
System.out.println("冬季");
break;
default:
System.out.println("输入月份错误");
}
}
}
5.天朝有一个乞丐姓洪,去天桥要钱
第一天要了1块钱
第二天要了2块钱
第三天要了4块钱
第四天要了8块钱
以此类推
问题: 洪乞丐干10天,收入是多少?
public class Demo06 {
public static void main(String[] args) {
for (int i=1, sum=1;i<=10;i++,sum *=2){
System.out.println("第"+i+"天"+",要了"+sum+"块钱");
}
System.out.println("=================================");
int x =1;//第一天的钱
for (int y=1;y<=10;y++){
System.out.println("第"+y+"天"+",要了"+x+"块钱");
x*=2;
}
}
}
6.打印 1-100 之间的数,如果这个数,要么是3,要么5的倍数,就忽略掉
public class Demo06 {
public static void main(String[] args) {
//打印 1-100 之间的数,如果这个数,要么是3,要么5的倍数,就忽略掉
for (int i = 1; i <= 100; i++) {
if (i%3==0 || i%5==0){
continue;
}
System.out.println(i);
}
}
}
7.百万富翁
假设你月收入是3000,除开平时花销,每个月留下1000块钱进行投资。
然后你认真的钻研了 《股票和基金 21天从入门到精通》,达到了每年20%的投资回报率。
那么问题来了,以每个月投资1000块钱的节奏,持续投资多少年,总收入达到100万
(复利计算按照每年12000投入计算,不按照每月计息)
复利公式:
F = p* ( (1+r)^n );
F 最终收入
p 本金
r 年利率
n 存了多少年
public class Demo06 {
public static void main(String[] args) {
//第一年f1=(f+12000)*1.2
//第二年f2=(f1+12000)*1.2 ...
//本金
double j =12000;
//每年总金额:
double total = 0;
//年利率
double year = 1+0.2;
//判断几年后成为百万富翁
for (int i=1;;i++){//循环每一年的金额
total=(total+j)*year;
System.out.println("第"+i+"年,总金额为:"+total);
if (total>100_0000){//判断是否成为百万富翁,并终止循环
System.out.println("恭喜在"+i+"年成为百万富翁!");
break;
}else {
System.out.println("距离百万富翁还差"+(100_0000-total)+",请继续努力!"+"\n");
}
}
}
}
8.寻找某两个数相除,其结果 离黄金分割点 0.618最近
分母和分子不能同时为偶数
分母和分子 取值范围在[1-20]
//——1到20中相除离黄金分割率(0.618)最近的数是: 8.0/13.0=0.6153846153846154
public class Demo06 {
public static void main(String[] args) {
double a =1;//预设找到的分母
double b =1;//预设找到的分子
for (double x=1;x<=20;x++){//分母
for (double y=1;y<x;y++){//分子,分子小与分母,排除无效循环
if (x%2==0 && y%2==0)continue;//不能同时为偶数
if (Math.abs(y/x-0.618)<Math.abs(b/a-0.618)){//Math.abs 取得绝对值,比较值与0.618(原点)的距离,求得最小距离
b=y;//上面成立,取代此时y值
a=x;//上面成立,取代此时x值
//最后慢慢找到绝对值最小的一对数,输出
}
}
}
System.out.println("1到20中相除离黄金分割率(0.618)最近的数是: "+b+"/"+a+"="+(b/a));
}
}
9.水仙花数
水仙花数定义:
- 一定是3位数
- 每一位的立方,加起来恰好是这个数本身,比如153=1 * 1 * 1 + 5 * 5 * 5 + 3 * 3 * 3
—— 寻找所有的水仙花数
public class Demo06 {
public static void main(String[] args) {
//取值100~999,一定是三位数
for (int number=100;number<1000;number++){
//变量表达方法一
// int a=number/100;
// int b=number/10-(a*10);
// int c=number-(a*100)-(b*10);
//变量表达方法二
int a=number/100;//例 >>>2.34 >>>a=2
int b=number/10%10;//例 >>>23.4 >>>23%10 >>>2.3 >>>b=3
int c=number%10;//例 >>>23.4 >>>c=4
if (number==((a*a*a)+(b*b*b)+(c*c*c))){
System.out.println(number+"="+a+b+c);
}
}
}
}
10.小学算术题
求得a,b,c,d的值
a + b = 8 + + c - d = 6 = = 14 10
public class Demo06 {
public static void main(String[] args) {
//a+b=8 a+c=14
//b+d=10
//c-d=6
for (int a=0;a<100;a++){
for (int b=0;b<100;b++){
for (int c=0;c<100;c++){
for (int d=0;d<100;d++){
int i1=a+b;
int i2=a+c;
int i3=b+d;
int i4=c-d;
if (i1==8 && i2==14 && i3==10 && i4==6){
System.out.println(a+" "+b+" "+c+" "+d);//3 5 11 5
break;
}
}
}
}
}
}
}
练习1:计算0~100之间的奇数和偶数的和
public class ForDemo02 {
public static void main(String[] args) {
//计算0~100之间的奇数和偶数的和
int oddSum = 0;//定义奇数总和的变量
int evenSum = 0;//定义偶数总和的变量
for (int i = 0; i <= 100; i++) {
if (i%2!=0){//模(余数)不等于0时,为奇数
oddSum +=i; //依次相加奇数
}else {//不为奇数,则为偶数
evenSum +=i;//依次相加偶数
}
}
System.out.println("奇数的和:"+oddSum);//奇数的和:2500
System.out.println("偶数的和:"+evenSum);//偶数的和:2550
}
}
练习2:用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个
public class ForDemo03 {
public static void main(String[] args) {
//用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个
//while方法
int x = 1; //定义变量x
while (x<=1000){//循环1到1000
if (x%5==0){//除5的模(余数)为0,则为5的倍数
System.out.print(x+"\t");
}
if (x%(5*3)==0){//每隔3个5的倍数被整除,模为0
System.out.println();//换行
}
x++;//递增
}
System.out.println();
System.out.println("==============================");
//for方法
for (int i = 1; i <= 1000; i++) {//从1循环到1000
if (i%5==0){//整除5,模为0,5的倍数
System.out.print(i+"\t");
}
if (i%(5*3)==0){//每隔3个5的倍数被整除,模为0
System.out.println();//换行
}
}
}
}
练习3:打印九九乘法表
public class ForDemo04 {
public static void main(String[] args) {
//打印九九乘法表
/*
1.我们先打印出第一列
2.把固定的数再用一个循环包起来,把固定数改为此变量
3.去掉重复项,被包裹的变量<=外面的变量
4.调整样式
*/
for (int x=1;x <=9;x++){
for (int y=1;y<=x;y++){
System.out.print(y+"*"+x+"="+(x*y)+"\t");
}
System.out.println();
}
}
}