栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

javase练习题3

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

javase练习题3

第一题

语法点:变量,运算符,if…else

案例:从键盘输入一个整数,判断它是奇数还是偶数(这里把0归为偶数)

开发提示:

​ 键盘输入需要用到Scanner类。

java.util.Scanner input = new java.util.Scanner(System.in);//准备从键盘输入的对象
int num = input.nextInt();//输入整数

​ 能够被2整除的是偶数,不能被2整除的是奇数

import java.util.Scanner;

public class Test1 {

    public static void main(String[] args) {

        // 案例:从键盘输入一个整数,判断它是奇数还是偶数(这里把0归为偶数)

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个整数:");
        int num = scanner.nextInt();
        if(num%2==0){

            System.out.println(num+"是偶数");

        }else {
            System.out.println(num+"是奇数");

        }
    }
}

第二题

语法点:变量,运算符,if…else

案例:从键盘输入一个字符,判断它是字母还是数字,还是其他字符

开发提示:

​ 键盘输入需要用到Scanner类。

java.util.Scanner input = new java.util.Scanner(System.in);//准备接收从键盘输入的扫描仪
char c = input.next().charAt(0);//输入单个字符
import java.util.Scanner;

public class Test2 {

    public static void main(String[] args) {
       // 案例:从键盘输入一个字符,判断它是字母还是数字,还是其他字符
        // 数字0-9对应的ASCII是48 -57
        // 字母是A65 a 97  z 是122

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个字符;");
        char c = scanner.next().charAt(0);

        int ch =(int)c;
        if (ch>=48&&ch<=48+9){
            System.out.println(c+"是数字");

        }else if(ch>=65&&ch<=122){

            System.out.println(c+"是字母");
            if (ch>=65&&ch<=96){
                System.out.println(c +"大写字母");

            }else {
                System.out.println(c +"小写字母");

            }

        }else {

            System.out.println(c+"其他字符");

        }


    }
}

第三题
  • 语法点:变量,运算符,if…else

  • 编写步骤:

    1. 定义类 Test2
    2. 定义 main方法
    3. 定义变量折扣 discount,初始化为1, 总价totalPrice的值从键盘输入
    4. 判断当totalPrice >=500 ,discount赋值为0.5
    5. 判断当totalPrice >=400 且<500时,discount赋值为0.6
    6. 判断当totalPrice >=300 且<400时,discount赋值为0.7
    7. 判断当totalPrice >=200 且<300时,discount赋值为0.8
    8. 判断当totalPrice >=0 且<200时,discount赋值为1
    9. 判断当totalPrice<0时,显示输入有误
    10. 输出结果
  • 开发提示:

    键盘输入需要用到Scanner类。

    java.util.Scanner input = new java.util.Scanner(System.in);//准备接收从键盘输入的扫描仪
    double totalPrice = input.nextDouble();//输入double值
    
import java.util.Scanner;

public class Test3 {

    public static void main(String[] args) {

        // 1. 定义类 Test22.
        // 定义 main方法3.
        // 定义变量折扣 discount,初始化为1,
        double discount =1;
        // 总价totalPrice的值从键盘输入4.

        Scanner input = new Scanner(System.in);
        System.out.println("请输入totalprice:");
        double totalprice = input.nextDouble();

        // 判断当totalPrice >=500 ,discount赋值为0.55.
        // 判断当totalPrice >=400 且<500时,discount赋值为0.66.
        // 判断当totalPrice >=300 且<400时,discount赋值为0.77.
        // 判断当totalPrice >=200 且<300时,discount赋值为0.88.
        // 判断当totalPrice >=0 且<200时,discount赋值为19.
        // 判断当totalPrice<0时,显示输入有误10.
        // 输出结果
        if (totalprice>=500){

            discount = 0.55;
        }else if(totalprice>=400){

            discount=0.66;
        }else if (totalprice>=300){
            discount = 0.77;
        }else if (totalprice>=200){
            discount = 0.88;
        }else if (totalprice>=0){

        }else {

            System.out.println("您输入的数据有误!!!");

        }
        totalprice-=(totalprice*discount);
        System.out.println("totalprice = " + totalprice);


    }
}

第四题

语法点:变量,运算符,if…else

案例:从键盘输入生日,判断星座

  • 开发提示:
    1. 各个星座的日期范围如下:
import java.util.Scanner;

public class Test4 {
    public static void main(String[] args) {

        // 按照从键盘录入的天数和月份进行星座判断
        // 1.从键盘开始录入天数和月份为了使代码更加完整 可以使用年份对二月进行划分
        // 2.首先是进行月份判断 如果是一月 此后要判断输入的数据是那个星座 此外 还要知道天数是不是合法
        Scanner input = new Scanner(System.in);

        System.out.println("year=");
        int year = input.nextInt();

        System.out.println("month = ");
        int month = input.nextInt();
        if (month < 0 && month > 12) {

            System.out.println("你输入的月份是错误的,请重新输入:");
            while (month < 0 && month > 12) {
                System.out.println("month = ");
                month = input.nextInt();


            }

        }

        System.out.println("请输入天数;");
        int day = input.nextInt();
        if (month == 1) {

            if (day < 1 && day > 31) {
                System.out.println("你输入的天数错误");

            } else {
                if (day >= 0 && day <= 19) {
                    System.out.println("巨蟹座");

                } else {

                    System.out.println("水瓶座");

                }
            }
        }
        if (month == 2) {
            int two_MonthDay;

            if ((year%4==0&&year%100!=0)||year%400==0){
                two_MonthDay=28;
            }else {
                two_MonthDay=29;
            }

            if (day < 1 && day > two_MonthDay) {
                // 此时直接使用功能月份进行判断 此时闰年和平年是没有进行区分的 如果知道年份可以使用年份进行月份的判断
                // 此时代码更加完整
                System.out.println("你输入的天数错误");

            } else {
                if (day >= 0 && day <= 18) {
                    System.out.println("水瓶座");

                } else {

                    System.out.println("双鱼座");

                }
            }
        }
        if (month == 3) {

            if (day < 1 && day > 31) {
                System.out.println("你输入的天数错误");

            } else {
                if (day >= 0 && day <= 20) {
                    System.out.println("双鱼座");

                } else {

                    System.out.println("白羊座");

                }
            }
        }

        if (month == 4) {

            if (day < 1 && day > 30) {
                System.out.println("你输入的天数错误");

            } else {
                if (day >= 0 && day <= 19) {
                    System.out.println("白羊座");

                } else {

                    System.out.println("金牛座");

                }
            }
        }
        if (month == 5) {

            if (day < 1 && day > 30) {
                System.out.println("你输入的天数错误");

            } else {
                if (day >= 0 && day <= 20) {
                    System.out.println("金牛座");

                } else {

                    System.out.println("双子座");

                }
            }
        }
        if (month == 6) {

            if (day < 1 && day > 30) {
                System.out.println("你输入的天数错误");

            } else {
                if (day >= 0 && day <= 21) {
                    System.out.println("双子座");

                } else {

                    System.out.println("巨蟹座");

                }
            }
        }

        if (month == 7) {

            if (day < 1 && day > 31) {
                System.out.println("你输入的天数错误");

            } else {
                if (day >= 0 && day <= 22) {
                    System.out.println("巨蟹座");

                } else {

                    System.out.println("狮子座");

                }
            }
        }

        if (month == 8) {

            if (day < 1 && day > 31) {
                System.out.println("你输入的天数错误");

            } else {
                if (day >= 0 && day <= 22) {
                    System.out.println("狮子座");

                } else {

                    System.out.println("处女座");

                }
            }
        }

        if (month == 9) {

            if (day < 1 && day > 30) {
                System.out.println("你输入的天数错误");

            } else {
                if (day >= 0 && day <= 22) {
                    System.out.println("处女座");

                } else {

                    System.out.println("天秤座");

                }
            }
        }

        if (month == 10) {

            if (day < 1 && day > 31) {
                System.out.println("你输入的天数错误");

            } else {
                if (day >= 0 && day <= 24) {
                    System.out.println("天蝎座");

                } else {

                    System.out.println("射手座");

                }
            }
        }

        if (month == 11) {

            if (day < 1 && day > 3) {
                System.out.println("你输入的天数错误");

            } else {
                if (day >= 0 && day <= 22) {
                    System.out.println("天蝎座");

                } else {

                    System.out.println("射手座");

                }
            }
        }
        if (month == 12) {

            if (day < 1 && day > 31) {
                System.out.println("你输入的天数错误");

            } else {
                if (day >= 0 && day <= 21) {
                    System.out.println("射手座");

                } else {

                    System.out.println("摩羯座");

                }
            }
        }
    }
}

第五题

语法点:变量,运算符,switch…case

案例需求:编写一个程序,为一个给定的年份找出其对应的中国生肖。中国的生肖基于12年一个周期,每年用一个动物代表:rat(鼠)、ox(牛)、tiger(虎)、rabbit(兔)、dragon(龙)、snake(蛇)、

​ horse(马)、sheep(羊)、monkey(候)、rooster(鸡)、dog(狗)、pig(猪)。

提示:2017年:鸡 2017 % 12 == 1

import java.util.Scanner;

public class Test5 {

    public static void main(String[] args) {

        // 提示:2017年:鸡 2017 % 12 == 1

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入年份;");
        int year = scanner.nextInt();

        int switch_Num = year%12;
        switch (switch_Num){
            case 1:
                System.out.println("rat");
                break;
            case 2:
                System.out.println("ox");
                break;
            case 3:
                System.out.println("tiger");
                break;

            // rat(鼠)、ox(牛)、tiger(虎)、rabbit(兔)、dragon(龙)、snake(蛇)、 horse(马)、sheep(羊)、
            // monkey(候)、rooster(鸡)、dog(狗)、pig(猪)。
            case 4:
                System.out.println("rabbit");
                break;
            case 5:
                System.out.println("dragon");
                break;
            case 6:
                System.out.printf("snake");
                break;
            case 7:
                System.out.println("horse");
                break;
            case 8:
                System.out.printf("sheep");
                break;
            case 9:
                System.out.println("monkey");
                break;
            case 10:
                System.out.println("rooster");
                break;
            case 11:
                System.out.println("dog");
                break;
            case 12:
                System.out.println("pig");
                break;
                default:
                    System.out.println("你输入的数据错误");

        }
    }
}

第六题

语法点:变量,运算符,if…else

案例:

  • 开发提示:
  1. Math.sqrt(num):求num的平方根

  2. 定义double类型变量a,b,c,并从键盘输入它们的值

    键盘输入需要用到Scanner类。

java.util.Scanner input = new java.util.Scanner(System.in);//准备接收从键盘输入的扫描仪
double totalPrice = input.nextDouble();//输入double值
public class Test6 {

    public static void main(String[] args) {


        int a = 10;

        int b = 20;
        int c = 30;
        if (b * b - 4 * a * c > 0) {
            if (a == 0 && b != 0) {

                double x1 = -c / b;

            } else {
                double x1 = (-b + Math.sqrt(b * b - 4 * a * c) / 2 * a);
                double x2 = (-b - Math.sqrt(b * b - 4 * a * c) / 2 * a);
            }
        } else if (b * b - 4 * a * c == 0) {

            double x1 = -b / 2 * a;
        } else {

            System.out.println("无解");

        }
    }
}

第七题

语法点:变量,运算符,if和switch…case

案例:已知2019年1月1日是星期二,从键盘输入2019年的任意一天,请判断它是星期几

  • 开发提示:
  1. 先统计这一天是这一年的第几天days
  2. 然后声明一个变量week,初始化为2
  3. 然后week加上days-1
  4. 然后求week与7的模数
  5. 然后输出结果,考虑星期天的特殊判断
import java.util.Scanner;

public class Test7 {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.println("请输入月份:");
        int month = input.nextInt();
        System.out.println("请输入天数:");
        int day = input.nextInt();
        int sum_Day=0;
        switch (month-1){
            case 11:
                sum_Day+=30;
            case 10:
                sum_Day+=31;
            case 9:
                sum_Day+=30;
            case 8:
                sum_Day+=31;
            case 7:
                sum_Day+=31;
            case 6:
                sum_Day+=30;
            case 5:
                sum_Day+=31;
            case 4:
                sum_Day+=30;
            case 3:
                sum_Day+=31;
            case 2:
                sum_Day+=29;
            case 1:
                sum_Day+=31;



        }
        sum_Day+=day;
        int week=2;
        week+=sum_Day-1;
        int new_Week=week%7;
        if (new_Week==0){

            System.out.println(new_Week+"周日");

        }else {
            System.out.println("周"+new_Week);
        }


    }
}

简答题

1、switch是否能作用在byte上,是否能作用在long上,是否能作用在String上?
可以使用的数据类型我6中分别是 short byte int char string(1.7) enum(1.5)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/489634.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号