日期:2014-05-20 浏览次数:20836 次
package chapter05;
import java.util.Scanner;
public class Demo2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("我行我素购物管理系统 > 购物结算");
System.out.println();
System.out.println("****************************");
System.out.println("1.T恤\t"+"2.网球鞋\t"+"3.网球拍\t");
System.out.println("****************************");
boolean i=true;
while(i){
System.out.print("请输入商品编号:");
Scanner console = new Scanner(System.in);
int id = console.nextInt();
switch(id){
case 1:
System.out.println("网球鞋\t&570.0");
break;
case 2:
System.out.println("T恤\t&245.0");
break;
case 3:
System.out.println("网球拍\t&500.0");
break;
}
System.out.println("是否结束(y/n):");
String ok = console.nextLine();
if(ok.equals("n")){
i=false;
}
}
}
}
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
public class Commodity {
public static void main(String[] args) {
Map<Integer, String[]> data = new HashMap<Integer, String[]>();
data.put(1, new String[] { "血滴子", "10元" });
data.put(2, new String[] { "血是子", "12元" });
data.put(3, new String[] { "血人子", "15元" });
System.out.println("我行我素购物管理系统 > 购物结算");
for (Iterator<Entry<Integer, String[]>> it = data.entrySet().iterator(); it
.hasNext();) {
Entry<Integer, String[]> entry = it.next();
System.out.print(entry.getKey() + "." + entry.getValue()[0] + " "
+ entry.getValue()[1] + "\t");
}
System.out.println("");
while (true && data.size() > 0) {
System.out.println("请输入商品编号:");
String[] info = data.get(new Scanner(System.in).nextInt());
if (null == info
------解决方案--------------------
2 > info.length) {
throw new IllegalArgumentException("编号错误,程序结束!");
}
System.out.println(info[0] + "\t" + info[1]);
System.out.println("是否继续?(y / n)");
String contrim = new Scanner(System.in).next();
if ("y".equals(contrim)) {
continue;
} else if ("n".equals(contrim)) {
break;
}
}
}
}
package chapter05;
import java.util.Scanner;
public class Demo2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("我行我素购物管理系统 > 购物结算");
System.out.println();
System.out.println("****************************");
System.out.println("1.T恤\t" + "2.网球鞋\t" + "3.网球拍\t");
System.out.println("****************************");
boolean i = true;
while (i) {
boolean k = true;
Scanner console = new Scanner(System.in);
while (k) {
System.out.print("请输入商品编号:");
int id = console.nextInt();
switch (id) {
case 1:
System.out.println("网球鞋\t&570.0");
System.out.println();
k = false;
break;
case 2:
System.out.println("T恤\t&245.0");
System.out.println();
k = false;
break;
case 3:
System.out.println("网球拍\t&500.0");
System.out.println();
k = false;
break;
default:
System.out.println("请输入正确的商品编号");
System.out.println();
k = true;
break;
}
}
while (!k) {
System.out.print("是否继续(y/n):");
String ok = console.next();
if ("n".equals(ok)
------解决方案--------------------
"N".equals(ok)) {
i = false;
k = true;
} else if ("y".equals(ok)
------解决方案--------------------
"Y".equals(ok)) {
// System.out.println("是否结束(y/n):");
k = true;
} else {
System.out.println("输入错误,重新选择!");
System.out.println();
}
}
}
System.out.println("程序结束!");
}
}