Java循环语句的示例和作业

本文涵盖了多种Java循环语句的实例,包括输出能被5整除的数、购物结算、年龄比例计算、数制转换、判断奇偶、考试成绩等级等。通过这些例子,读者将深入理解如何运用while、for循环以及if和switch语句解决实际问题。
摘要由CSDN通过智能技术生成

用while和for循环输出1-1000之间能被5整除的数,且每行输出三个。

public class Test1 {
    public static void main(String[] args) {
        //用while和for循环输出1-1000之间能被5整除的数,且每行输出三个。
        int j = 0;
        for (int i = 1; i <= 1000 ; i++) {
            if(i % 5 ==0){
                System.out.print(i + "\t");
                j++;
                if(j%3 ==0){
                    System.out.println();
                    j = 0;
                }
            }

        }
    }
}

 购物结算

 

import java.util.Scanner;
public class Test1 {
    public static void main(String[] args) {
        //购物结算
        System.out.println("***************************************************************************************************");
        System.out.println("请选择购买的商品编号:");
        System.out.println("1.T恤 \t 2.网球鞋\t 3.网球拍");
        System.out.println("***************************************************************************************************");
        //创建Scanner对象
        Scanner sc = new Scanner(System.in);
        String flag ="y";
        //存储商品编号
        int pld = 0;
        //存储商品编号的变量
        int num = 0;

        //T恤单价
        double tPrice = 245.0;
        //网球鞋单价
        double sPrice = 570.0;
        //网球拍单价
        double pPrice = 320.0;
        //定义每次购买商品总价
        double oneTotalPrice = 0;
        //定义一个计算购买商品总额的变量
        double totalPrice = 0;
        while("y".equals(flag)){
            System.out.println();
            System.out.println("请输入商品编号:");
            pld =sc.nextInt();
            //用if 判断是否正确的输入商品编号
            if(pld != 1 && pld != 2 && pld != 3){
                System.out.println("亲,你买的商品不存在");
                //跳出当前循环进行下一次循环
                continue;
            }
            System.out.println("请输入购买数:");
            num = sc.nextInt();
            switch(pld){
                case 1:
                    oneTotalPrice = tPrice*num;
                    System.out.println("T恤¥" + tPrice + "\t" + "数量" + num + "\t" + "合计" + "¥" + oneTotalPrice);
                    totalPrice += oneTotalPrice;
                    //清0
                    oneTotalPrice = 0;
                    break;
                case 2:
                    oneTotalPrice = sPrice * num;
                    System.out.println("网球鞋¥" + sPrice + "\t" + "数量" + num + "\t" + "合计" + "¥" + oneTotalPrice);
                    totalPrice += oneTotalPrice;
                    //清0
                    oneTotalPrice = 0;
                    break;
                case 3:
                    oneTotalPrice = pPrice * num;
                    System.out.println("网球拍¥" + pPrice + "\t" + "数量" + num + "\t" + "合计" + "¥" + oneTotalPrice);
                    totalPrice += oneTotalPrice;
                    //清0
                    oneTotalPrice = 0;
                    break;
            }
            System.out.println("是否继续(y/n):");
            flag = sc.next();
        }
        System.out.println();
        System.out.println("折扣:");
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Curran-java

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值