习题2_Java流程控制

1.根据键盘录入的数值1,2,3,…7输出对应的星期一,星期二,星期三…星期日。(用switch语句实现)

代码:

import java.util.Scanner;

public class exercise01 {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        String s;
        switch (n){
            case 1:
                s = "星期一";
                break;
            case 2:
                s = "星期二";
                break;
            case 3:
                s = "星期三";
                break;
            case 4:
                s = "星期四";
                break;
            case 5:
                s = "星期五";
                break;
            case 6:
                s = "星期六";
                break;
            case 7:
                s = "星期日";
                break;
            default:
                s = "请输入一个1到7之间的整数!";
                break;
        }
        System.out.println(s);
    }
}

运行结果:

在这里插入图片描述

2.判断一个5位数是否是回文数(比如12321,个位等于万位,十位等于千位)

代码:

import java.util.Scanner;

public class exercise02 {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] array = new int[5];
        for(int i=0;i<5;i++){
            int m = n;
            array[i] = m % 10;
            m /= 10;
        }
        if(array[0] == array[4] && array[1] == array[3]){
            System.out.println(n + "是一个回文数");
        }
        else{
            System.out.println(n + "不是一个回文数");
        }
    }
}

运行结果:

在这里插入图片描述

3.利用switch语句,实现对学生分数评级的功能。
程序的输入是一个int类型的变量score,代表学生的分数取值范围[0,100]
当分数范围为 0=< score <60 输出 不及格
当分数范围为 60=< score <70 输出 及格
当分数范围为 70=< score <80 输出 中
当分数范围为 80=< score <90 输出 良
当分数范围为 90=< score <=100 输出 优
提示:可合理利用case穿越

代码:

import java.util.Scanner;

public class exercise03 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你的成绩([0,100]):");
        int score = sc.nextInt();
        score /= 10;
        String s;
        switch (score){
            case 10:
            case 9:
                s = "优";
                break;
            case 8:
                s = "良";
                break;
            case 7:
                s = "中";
                break;
            case 6:
                s = "及格";
                break;
            default:
                s = "不及格";
                break;
        }
        System.out.println("你的成绩是:" + s);
    }
}

运行结果:

在这里插入图片描述

4.在给定的字符数(只包含ascii码表中包含的128个字符)组中找到,第一个只出现一次的字符(假设所给字符数组中一定存在只出现一次的字符,第一个是指从左到右的字符出现的顺序)。

代码:

import java.util.Scanner;

public class addtion01 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个字符串:");
        String s = sc.nextLine();
        char[] str = s.toCharArray();//转化为字符串数组
        int[] count = new int[128];//计数数组,统计字符(ascii码表中包含的128个字符)出现的次数
        for(int i=0;i<128;i++){ //初始化为0
            count[i] = 0;
        }
        for(int i=0;i<str.length;i++){//遍历字符数组,统计字符出现的次数
            count[str[i]]++;
        }
        for(int i = 0;i <str.length;i++){//遍历字符数组,找到第一个出现次数为1的字符
            if(count[str[i]] == 1) {
                System.out.println("第一个只出现一次的字符是" + str[i]);
                break;
            }
        }
    }
}

运行结果:

在这里插入图片描述

5.在一个整形数组里,除了两个数字之外,其他数字都出现了2次。请写程序找到这两个只出现了一次的数字。

代码:

public class addtion02 {
    public static void main(String[] args) {
        int array[] = {1, 5, 6, 5, 1, 2};
        int tag[] = {1, 1, 1, 1, 1, 1};  //tag数组为标记数组,初始值为1
        //若两元素相等,则把它们的tag值标记为0。
        for(int i = 0;i < array.length-1;i++){
            for(int j = i + 1 ;j < array.length;j++){
                if(array[i] == array[j]){
                    tag[i] = 0;
                    tag[j] = 0;
                }
            }
        }
        //tag值为1的元素即为只出现一次的元素
        for(int i = 0;i < tag.length;i++){
            if(tag[i] == 1){
                System.out.println(array[i]);
            }
        }
    }
}

运行结果:

在这里插入图片描述

6.定义方法完成,打印 nxn乘法表(n的取值范围是[1-9])

代码:

import java.util.Scanner;

public class Printnxn {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你想得到的乘法表大小(取值范围是[1-9]):");
        int n = sc.nextInt();
        multiply(n);
    }
    public static void multiply(int n){
        for(int i=1;i<=n;i++){
            for(int j=1;j<=i;j++){
                System.out.print(i + "x" + j + "=" + i * j + "  ");
            }
            System.out.println();
        }
    }
}

运行结果:

在这里插入图片描述

7.小芳的妈妈每天给她2.5元钱,她都会存起来,但是, 每当这一天是存钱的第5天或者5的倍数的话,她都会花去6元钱, 请问,至少经过多少天,小芳可以存到100元

代码:

public class SaveMoney {
    public static void main(String[] args){
        double money = 0;
        int days = 0;
        while(money<100){
            days++;
            money += 2.5;
            if(days % 5 == 0)
                money -= 6;
        }
        System.out.println("至少经过" + days +"天,小芳可以存到100元");
    }
}

运行结果:

在这里插入图片描述

8.正整数n若是它平方数的尾部,则称n为同构数,比如5是5 * 5=25右边的数,25是25 * 25=625右边的数 求1-99范围内的所有同构数

代码:

//求1-99的同构数
public class IsomorphismNumber {
    public static void main(String[] args){
        for(int i=1;i<=99;i++){
            if(i<10){
                if(i == (i*i%10))
                    System.out.print(i + " ");
            }
            else {
                if(i == (i*i%100))
                    System.out.print(i + " ");
            }
        }
    }
}

运行结果:

在这里插入图片描述
#个人学习记录,如发现有错误之处,欢迎与我交流

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值