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

java练习题

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

java练习题

练习题1:手机选号:根据用户输入的手机号来确认用户实际支付的价格

如果尾数为 8,需支付办卡费 50 元
如果尾数为 4,需支付办卡费用 0 元
如果是其他尾号, 需支付办卡费用 20 元

package demo;

import java.util.Scanner;

public class D1 {
    public static void main(String[] args) {
        System.out.println("请输入电话号码");
        Scanner s=new Scanner(System.in);
        String tel=s.nextLine();//String类型数据一般用nextLine()
        getPrice(tel);
    }

    public static void getPrice(String tel){

        if(tel.length()!=11){//判断手机号tel的长度
            System.out.println("格式不正确");
            return  ;
        }

        int price;
        char c=tel.charAt(10);//charAt(int index),返回指定索引处的字符
        switch (c){
            case 8:
                price=50;
                break;
            case 4:
                price=0;
                break;
            default:
                price=20;
        }
        System.out.println("需支付办卡费用"+price+"元");
    }
}
请输入电话号码
12345678910
需支付办卡费用20元
练习题2: 求任意自然数各位数之和

接收用户输入的任意自然数, 累计所有位数数字之和

package demo;

import java.util.Scanner;

public class D3 {

    public static void main(String[] args) {
        System.out.println("输入自然数");
        long l=new Scanner(System.in).nextLong();
        long sum=0;
        while (l!=0) {
            sum = sum+l%10;//l%10 最后一位的值
            l = l / 10;//小数点后舍掉,减一位
        }
        System.out.println(sum);
    }
}
输入自然数
123
所有位数数字之和为6
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/781685.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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