急求简易自动售货机(java编程) 5

实验内容:1熟悉forwhiledo..while语句,为具体操作选择最合适的循环实验重点:for语句的使用实验题目:设计一个自动售货机,提供如下选择:[1]Getgum... 实验内容:
1 熟悉for while do..while语句,为具体操作选择最合适的循环
实验重点:for 语句的使用
实验题目:
设计一个自动售货机,提供如下选择:
[1] Get gum
[2] Get chocolate
[3] Get popcorn
[4] Get juice
[5] Display total sold
[6] Quit
允许用户连续的从这些选项中进行选择。当选中1-4选项时,显示适当的信息确认选项。例如当用户选择3时,可以显示如下信息:
Here is your popcorn
当用户选择5时,显示已经售出的每种商品的数量。例如:
4 items of gum sold
3 items of chocolate sold
8 items of popcorn sold
当用户选择6时,程序终止。如果输入1-6以外的选项,显示出错信息,例如:
Error, option 1-6 only!
展开
 我来答
龙光瑞像416
推荐于2017-05-26 · 超过21用户采纳过TA的回答
知道答主
回答量:48
采纳率:0%
帮助的人:37.5万
展开全部
package com.test;
import java.util.Scanner;
import com.sun.java_cup.internal.internal_error;
public class Demo {
/**
* @param args
*/
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int c;
int gum=0,cho=0,pop=0,jui=0;
do{
Demo.play_bord();
c=s.nextInt();
if(c<0||c>6){
System.out.println("1-6 only ,input a new choice");
c=s.nextInt();

}

switch(c){
case 1:
System.out.println("here is your gum");
gum++;
break;
case 2:
System.out.println("here is your chocolate");
cho++;
break;
case 3:
System.out.println("here is your popcorn");
pop++;
break;
case 4:
System.out.println("here is your juice");
jui++;
break;
case 5:
System.out.println(gum+" gum are sold");
System.out.println(cho+" chocolate are sold");
System.out.println(pop+" popcorn are sold");
System.out.println(gum+" juice are sold");
break;
case 6:
System.out.println("system exit");
}

}while(c!=6);
}
public static void play_bord(){
System.out.println("****这是简易售货机,请按下数字选择服务*****");
System.out.println("[1]:get gum");
System.out.println("[2]:get chocolate");
System.out.println("[3]:get popcorn");
System.out.println("[4]:get juice");
System.out.println("[5]:dispaly totalsold");
System.out.println("[6]:quit");
System.out.println("***************************");

}
}

刚好以前写了个 改完给你了
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2018-05-09
引用z17199的回答:
package com.test;
import java.util.Scanner;
import com.sun.java_cup.internal.internal_error;
public class Demo {
/**
* @param args
*/
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int c;
int gum=0,cho=0,pop=0,jui=0;
do{
Demo.play_bord();
c=s.nextInt();
if(c<0||c>6){
System.out.println("1-6 only ,input a new choice");
c=s.nextInt();

}

switch(c){
case 1:
System.out.println("here is your gum");
gum++;
break;
case 2:
System.out.println("here is your chocolate");
cho++;
break;
case 3:
System.out.println("here is your popcorn");
pop++;
break;
case 4:
System.out.println("here is your juice");
jui++;
break;
case 5:
System.out.println(gum+" gum are sold");
System.out.println(cho+" chocolate are sold");
System.out.println(pop+" popcorn are sold");
System.out.println(gum+" juice are sold");
break;
case 6:
System.out.println("system exit");
}

}while(c!=6);
}
public static void play_bord(){
System.out.println("****这是简易售货机,请按下数字选择服务*****");
System.out.println("[1]:get gum");
System.out.println("[2]:get chocolate");
System.out.println("[3]:get popcorn");
System.out.println("[4]:get juice");
System.out.println("[5]:dispaly totalsold");
System.out.println("[6]:quit");
System.out.println("***************************");

}
}

刚好以前写了个 改完给你了
展开全部
//Example类文件Example.javapackage cn.zhouhan;import java.util.Scanner;public class Example { static Drink cola, fanta, blacktea, greentea; static int money; public static void main(String[] args) { // TODO Auto-generated method stub cola = new Drink("可乐", 50, 2.0, 1); fanta = new Drink("芬达", 40, 3.0, 2); blacktea = new Drink("红茶", 30, 4.0, 3); greentea = new Drink("绿茶", 20, 5.0, 4); money = 500; for(;;) { System.out.println("-------------自动售货机-------------"); cola.UserPrintDrinkInfo(); fanta.UserPrintDrinkInfo(); blacktea.UserPrintDrinkInfo(); greentea.UserPrintDrinkInfo(); System.out.println("----------------------------------"); System.out.println("1.购买饮料"); System.out.println("2.管理员查询"); System.out.println("3.返回"); System.out.println("请输入要执行的操作序号:"); Scanner scannerIndex = new Scanner(System.in); int indexScanner = scannerIndex.nextInt(); switch(indexScanner) { case 1: System.out.println("请选择购买的饮料(1.可乐,2.芬达,3红茶,4.绿茶):"); Scanner numberScanner = new Scanner(System.in); int buyIndex = numberScanner.nextInt(); Drink drink = getDrink(buyIndex); System.out.println("请输入购买的数量:"); Scanner countScanner = new Scanner(System.in); int buyCount = countScanner.nextInt(); if (buyCount > 0) { System.out.println("您选择了" + buyCount + "瓶" + drink.getName() + "." ); System.out.println("总价格:" + drink.getPrice() * buyCount + "元,请投入1元或5元纸币。"); System.out.println("确定购买?(1.确定,2.退款):"); Scanner sureScanner = new Scanner(System.in); int sure = sureScanner.nextInt(); if (sure == 1) { drink.sellDrink(buyCount); money += drink.getPrice() * buyCount; System.out.println("您购买了" + buyCount + "瓶" + drink.getName() + ",交易成功." ); } if (sure ==2 ) { System.out.println("退款成功!"); } } else System.out.println("输入的数量有误!"); break; case 2: System.out.println("请输入管理员密码:"); Scanner pwScanner = new Scanner(System.in); int password = pwScanner.nextInt(); if (password == 123) { System.out.println("密码正确,目前的余额为:" + money + "元。"); } else System.out.println("密码错误。"); break; case 3: break; default: System.out.println("请输入正确的数字。"); break; } } } public static Drink getDrink(int drinkIndex) { Drink drink = null; switch(drinkIndex) { case 1: drink = cola; System.out.println("您选择了可乐。"); break; case 2: drink = fanta; System.out.println("您选择了芬达。"); break; case 3: drink = blacktea; System.out.println("您选择了红茶。"); break; case 4: drink = greentea; System.out.println("您选择了绿茶。"); break; default: //drink = null; System.out.println("输入错误,请输入1~4的数字!"); } return drink; }} //创建的Drink类文件Drink.javapackage cn.zhouhan;public class Drink { private String name; private int number; private double price; private double drinkMoney; private int index; public Drink(String name, int number, double price, int index) { this.name = name; this.number = number; this.price = price; this.drinkMoney = number * price; this.index = index; } public String getName() { return this.name; } public int getNumber() { return this.number; } public double getPrice() { return this.price; } public void sellDrink(int count) { this.number -= count; this.drinkMoney += this.price * count; } public void UserPrintDrinkInfo() { System.out.println("品名:"+ this.name +",单价:" + this.price + "元,剩余数量:" + this.number + "瓶。"); } }
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式