static Scanner console = null; static boolean i=true; /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub while(i){ showMenu(); choose(i); } } /** * Show the menu */ public static void showMenu(){ System.out.println("我行我素购物管理系统 > 购物结算"); System.out.println(); System.out.println("****************************"); System.out.println("1.网球鞋\t"+"2.T恤\t"+"3.网球拍\t"); System.out.println("****************************"); } /** * response to the user choice * @param i * @param id */ public static void choose(boolean 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.next(); if(ok.equals("y")){ i=false; }
import java.util.Scanner;
public class T01 {
static Scanner console = null;
static boolean i=true;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub while(i){
showMenu();
choose(i);
}
}
/**
* Show the menu
*/
public static void showMenu(){
System.out.println("我行我素购物管理系统 > 购物结算");
System.out.println();
System.out.println("****************************");
System.out.println("1.网球鞋\t"+"2.T恤\t"+"3.网球拍\t");
System.out.println("****************************");
}
/**
* response to the user choice
* @param i
* @param id
*/
public static void choose(boolean 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.next();
if(ok.equals("y")){
i=false;
}
}
}
//RMB符号¥,需要改编码
if(ok.equals("y")){
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;
}
}
}
}
/**
* @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("程序结束!");
}
}
不知道这样是不是你想要的结果!