实验内容:
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!
这个咋写呢,老师上课老砍别的,真心没学会
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!
这个咋写呢,老师上课老砍别的,真心没学会
解决方案 »
- 为什么总是出现未结束的字符字面(c1 = '/u534e报错)谢谢了
- 【新手求助】gui插件安装问题
- How to finish the java constructor throw Exception test?
- 急:如何动态引用java控件?
- JS中这个东西怎么弄,一个数字去对应一个字母如97对应a
- 关于键盘的监听的问题
- 哪里有jdbc to db2驱动下载?
- java中 switch case 语句的用法问题!
- 突然脑子打结了,Integer怎么转回int,不要用String等作中间过渡
- CORBA和RMI实现分布式计算是不是都需要一个命名服务器呀,谁能给介绍介绍。
- java 线程和Applet
- 求解 如下函数执行过程
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;public class Demo1 {
private Map<String, Integer> products; /**
* 构造方法初始化数据
*/
public Demo1() {
products = new HashMap<String, Integer>();
products.put("gum", 0);
products.put("chocolate", 0);
products.put("popcorn", 0);
products.put("juice", 0);
} /**
* 获取键盘输入的数据
* @return
*/
public int getInfo() {
System.out.print("请输入:");
String info = null;
BufferedReader read = null;
try {
read = new BufferedReader(new InputStreamReader(System.in));
info = read.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (info.matches("\\d+")) {
return Integer.parseInt(info);
} else {
return 0;
}
} /**
* 打印菜單
*/
public void print() {
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] Display total sold");
System.out.println("[6] Quit");
System.out.println("----------菜单结束----------");
} /**
* 循环购买商品
*/
public void gen() {
while (true) {
print();
int id = getInfo();
switch (id) {
case 1: {
System.out.println("Here is your popcorn");
products.put("gum", products.get("gum") + 1);
break;
}
case 2: {
System.out.println("Here is your chocolate");
products.put("chocolate", products.get("chocolate") + 1);
break;
}
case 3: {
System.out.println("Here is your popcorn");
products.put("popcorn", products.get("popcorn") + 1);
break;
}
case 4: {
System.out.println("Here is your juice");
products.put("juice", products.get("juice") + 1);
break;
}
case 5: {
for (String str : products.keySet()) {
System.out.println(products.get(str) + " items of " + str
+ " sold");
}
break;
}
case 6: {
System.err.println("byb!!!");
System.exit(1);
}
default:
System.err.println("Error, option 1-6 only!");
break;
}
}
} /**
* 主方法
*/
public static void main(String[] args) {
Demo1 d = new Demo1();
d.gen();
}
}下面是测试的信息: