我是靠谱客的博主 苹果缘分,最近开发中收集的这篇文章主要介绍智能购物计算小程序,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

【1】 学会分析"智能购物"程序的实现思路
【2】 根据思路独立完成”智能购物”的源代码编写、编译及运行。
【3】 掌握在程序中使用switch条件语句进行运算操作。

编写一个智能购物计算小程序,在一家商店有书本、铅笔、橡皮、可乐、零食五种商品,商品价格如下表所示。
商品名称 价格
书本 12元
铅笔 1元
橡皮 2元
可乐 3元
零食 5元
假如你带了20元,且必须购买一本书,剩余的钱还可以购买哪种商品,可以购买几件,购买完后又能剩余多少钱?

思路:
(1) 从任务描述中可知,要实现此功能,我们需要先定义5种商品,定义五个int值作为这五种商品的价格。
(2) 从运行结果可知,我们需要先打印各个商品的价格以及带了多少钱,并选择需要购买商品的序列号。
(3) 选择到序列号后,我们需要使用switch条件语句进行判断用户要购买那件商品,并在switch条件语句中,计算可以购买多少其他商品和剩余多少钱。

package lesson_02;
import java.util.Scanner; 
public class shopping {

	public static void main(String[] args) {

//		*假如你有20元,至少需要购买一本书,剩余的钱还可以购买那些东西呢?

		int pencil=1;//铅笔价格
		int rubber=2;//橡皮价格
		int cola=3;//可乐价格
		int book=12;//书价格
		int snacks=5;//零食价格
		int spaceship=10000000;
		System.out.println("书本的价格为"+book+"元,您共有20元");
		System.out.println("1.铅笔的价格为"+pencil+"元");
		System.out.println("2.橡皮的价格为"+rubber+"元");
		System.out.println("3.可乐的价格为"+cola+"元");
		System.out.println("4.零食的价格为"+snacks+"元");
		System.out.println("5.飞船的价格为"+spaceship+"元");
		Scanner sc1 = new Scanner(System.in);
		System.out.println("请输入其他需要购买商品的名称:");
		int id=sc1.nextInt();
		switch(id) {
		case 1:
			int pencilmoney=20-book;
			int pencilsum=pencilmoney/pencil;
			int pencilsurplus=pencilmoney%pencil;
			System.out.println("购买完书本后还可以购买铅笔"+pencilsum+"个,还剩"+pencilsurplus+"元");
			break;
		case 2:
			int rubbermoney=20-book;
			int rubbersum=rubbermoney/rubber;
			int rubbersurplus=rubbermoney%rubber;
			System.out.println("购买完书本后还可以购买橡皮"+rubbersum+"个,还剩"+rubbersurplus+"元");
			break;
		case 3:
			int colamoney=20-book;
			int colasum=colamoney/cola;
			int colasurplus=colamoney%cola;
			System.out.println("购买完书本后还可以购买可乐"+colasum+"个,还剩"+colasurplus+"元");
			break;
		case 4:
			int snacksmoney=20-book;
			int snackssum=snacksmoney/snacks;
			int snackssurplus=snacksmoney%snacks;
			System.out.println("购买完书本后还可以购买零食"+snackssum+"个,还剩"+snackssurplus+"元");
			break;
		case 5:
			int spaceshipmoney=20-book;
			int spaceshipsum=spaceshipmoney/spaceship;
			int spaceshipsurplus=spaceshipmoney%spaceship;
			System.out.println("购买完书本后还可以购买飞船"+spaceshipsum+"个,还剩"+spaceshipsurplus+"元");
			break;
		default:
			System.out.println("您输入的有误");
			break;
		}
	
	}

}

最后

以上就是苹果缘分为你收集整理的智能购物计算小程序的全部内容,希望文章能够帮你解决智能购物计算小程序所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(62)

评论列表共有 0 条评论

立即
投稿
返回
顶部