选择结构和循环结构java练习

1、通过键盘输入学生分数并根据成绩定档:
0-59分“不及格”,60-69分“及格”,70-79分“中等”,80-89分“良好”,90-100分“优秀”

import java.util.Scanner;
public class HomeWork8_22 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入学生的成绩:");
        int score =  scanner.nextInt();
        if (score >= 90 && score <=100) {
            System.out.println("优秀");
        } else if (score >= 80 && score < 90) {
            System.out.println("良好");
        } else if (score >= 70 && score < 80) {
            System.out.println("中等");
        } else if (score >= 60 && score < 70) {
            System.out.println("及格");
        } else if (score >= 0 && score < 60) {
            System.out.println("不及格");
        } else {
            System.out.println("成绩输入错误");
        }

    }
}

2、通过键盘输入年份判断其是否为闰年并显示该年二月份有多少天?【闰年判别方法:能被4整除但不能被100整除或能被400整除的整数】

import java.util.Scanner;
public class HomeWork8_22 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入年份:");
        int year = scanner.nextInt();
        if ((year % 4 == 0 && year % 100 !=0) || year % 400 == 0){
            System.out.println(year + "年为闰年有29天");
        }else {
            System.out.println(year + "年为平年有28天");
        }
    }
}

3、用户通过键盘输入两个整数,然后再输入功能选择号实现两个数的运算,注意0不能作为除数,必须要有switch功能的使用
*****选择运算******
1.加法
2.减法
3.乘法
4.除法
*******************

import java.util.Scanner;
public class HomeWork8_22 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("*****选择运算******");
        System.out.println("1.加法");
        System.out.println("2.减法");
        System.out.println("3.乘法");
        System.out.println("4.除法");
        System.out.println("*******************");
        System.out.print("请输入第一个整数:");
        int x = scanner.nextInt();
        System.out.print("请输入第二个整数:");
        int y = scanner.nextInt();
        System.out.print("请输入功能选择号:");
        int z = scanner.nextInt();
        int sum, difference, product;
        double quotient;
        switch (z){
            case 1:
                sum = x + y;
                System.out.println(x + "与" + y + "的和是:" + sum);
                break;
            case 2:
                difference = x - y;
                System.out.println(x + "与" + y + "的差是:" + difference);
                break;
            case 3:
                product = x * y;
                System.out.println(x + "与" + y + "的积是:" + product);
                break;
            case 4:
                if (y != 0){
                    quotient =  x / (double) y;
                    System.out.println(x + "与" + y + "的商是:" + quotient);
                    break;
                } else {
                    System.out.println("0不能做除数,请重新输入");
                    break;
                }
        }
    }
}

4、求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加由键盘控制。

import java.util.Scanner;
public class HomeWork8_22 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入您要计算的数字:");
        int x = scanner.nextInt();
        System.out.print("请输入要计算的次数:");
        int n = scanner.nextInt();
        int y = x;
        int s = 0;
        for (int i = 1;i <= n; i++){
            s += x;
            x = x * 10 + y;
        }
        System.out.println("和为:" + s);
    }
}

5、一球从100米高度自由落下,每次落地后反弹回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?

public class HomeWork8_22 {
    public static void main(String[] args) {
        double height = 100;
        double sum = 0;
        for (int i = 1;i <=10 ;i++){
            if (i == 1){
                sum += height;
            }else {
                sum = sum + height * 2;
            }
            height = height / 2;
        }
        System.out.println("第10次落地时共经过"+ sum + "米");
        System.out.println("第10次反弹有" + height + "米");

    }
}

6、古典问题:有一对兔子,从出生后第三个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问前10个月每个月的兔子数为多少并计算总共有多少只兔子?

public class HomeWork8_22 {
    public static void main(String[] args) {
        int month1 = 1, month2 = 1, sum = 0;
        for (int i = 1; i <= 10; i++){
            if (i >= 3){
                sum = month1 + month2;
                month1 = month2;
                month2 = sum;
            }else {
                sum = month1;
            }
            System.out.println("第" + i +"月有" + sum*2 + "只兔子");
        }
    }
}

7、幸运猜猜猜:游戏随机给出一个0~99(包括0和99)的数字,然后让你猜是什么数字。你可以随便猜一个数字,游戏会提示太大还是太小,从而缩小结果范围。经过几次猜测与提示后,最终推出答案。在游戏过程中,记录你最终猜对时所需要的次数,游戏结束后公布结果。

import java.util.Random;
import java.util.Scanner;

public class HomeWork8_22 {
    public static void main(String[] args) {
        Random r= new Random();
        int x = r.nextInt(99);
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入0~99的随机数:");
        int s = scanner.nextInt();
        int a = 1;
        while (a >= 1) {
            if (s > x) {
                System.out.print("大了点,请重新输入:");
                int j = scanner.nextInt();
                s = j;
                a++;
            } else if (s < x) {
                System.out.print("小了点,请重新输入:");
                int j = scanner.nextInt();
                s = j;
                a++;
            } else {
                System.out.println("刚刚好,生成的随机数为:" + x + ",总共猜了" + a + "次");
                break;
            }
        }

    }
}

8、猴子第一天摘了若干个桃子,当即吃了一半,还不解馋,又多吃了一个;第二天,吃剩下的桃子的一半,还不过瘾,又多吃了一个;以后每天都吃前一天剩下的一半多一个,到第10天想再吃时,只剩下一个桃子了。问第一天共摘了多少个桃子?

public class HomeWork8_22 {
    public static void main(String[] args) {
        int i, s = 1;
        for (i = 0; i < 9; i++) {
            s = (s + 1) * 2;
        }
        System.out.println("第一天共摘了桃子为:" + s);
    }
}

 

posted @ 2023-08-25 22:23  韩世康  阅读(45)  评论(0编辑  收藏  举报