java学习第6天,今天是循环结构

1.while循环结构

while ( 循环条件 ) {
        
       循环操作

}
 

练习(1)

计算100以内的偶数之和

package cn.loyer;
/**
 * @authorDesc 诸神在上,佑我程序,玄功护体,BUG皆去!
 * @author Xunan
 * @date 2022-07-11 14:40:37
 * @version 1.0.0
 * @description 100以内偶数和
 */
public class Evennumber {
    /**
     * @description
     * @author Xunan
     * @date 2022-07-11 14:40:48
     * @param args
     * @return
     */
    public static void main(String[] args) {
        int num = 0;
        int sum = 0;
        while (num <= 100){
            sum += num;
            num += 2;
        }
        System.out.println(sum);
    }
}

运行成功

练习(2)

计算培训学员人数,2012年培养学员25万人,每年增长25%。请问按此增长速度,到哪一年培训学员人数将达到100万人?

package cn.loyer;
/**
 * @authorDesc 诸神在上,佑我程序,玄功护体,BUG皆去!
 * @author Xunan
 * @date 2022-07-11 16:22:59
 * @version 1.0.0
 * @description 计算培训学员人数
 */
public class Students {
    /**
     * @description 2012年培养学员25万人,每年增长25%。
     * 请问按此增长速度,到哪一年培训学员人数将达到100万人?
     * @author Xunan
     * @date 2022-07-11 16:23:12
     * @param args
     * @return
     */
    public static void main(String[] args) {
        double num = 25;//初始为25W
        double year = 2012;//初始为2012年
        while (num <= 100){//增加至100W
            num = num * 1.25;//每次增加25%
            year++;//年份每次加1
        }
        System.out.println(year);
    }
}

运行成功

练习(3)

查询商品价,格用户从控制台输入需要查询的商品编号,根据编号显示对应的商品价格。假设商品名称和商品价格:
     T 恤 ¥245.0,网球鞋 ¥570.0,网球拍¥320.0

package cn.loyer;

import java.util.Scanner;

/**
 * @authorDesc 诸神在上,佑我程序,玄功护体,BUG皆去!
 * @author Xunan
 * @date 2022-07-11 15:11:46
 * @version 1.0.0
 * @description 查询商品价格
 */
public class Price {
    /**
     * @description 用户从控制台输入需要查询的商品编号,根据编号显示对应的商品价格。假设商品名称和商品价格:
     *      T 恤 ¥245.0,网球鞋 ¥570.0,网球拍¥320.0
     * @author Xunan
     * @date 2022-07-11 15:11:55
     * @param args
     * @return
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("MyShopping购物管理系统>购物结算");
        System.out.println("********************************************");
        System.out.println("请选择购买的商品的编号:");
        System.out.println("1.T恤\t2.网球鞋\t网球拍");
        System.out.println("********************************************");
        String answer = "y";
        while ("y".equals(answer)){
            System.out.println("请输入商品编号:");
            int num = sc.nextInt();
            switch (num){
                case 1:
                    System.out.println("T恤\t¥245.0");
                    break;
                case 2:
                    System.out.println("网球鞋\t¥570.0");
                    break;
                case 3:
                    System.out.println("网球拍\t¥320.0");
                    break;
                default:
                    System.out.println("请输入正确编号");
                    break;
            }
            System.out.println("是否继续(y/n)");
            answer = sc.next();
        }
        System.out.println("程序结束!");
    }
}

运行成功

练习(4&#

  • 3
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

方片6

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

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

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

打赏作者

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

抵扣说明:

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

余额充值