java循环和switch混合简单应用

来源:互联网 发布:c语言排队系统 编辑:程序博客网 时间:2024/06/08 09:50
import java.io.IOException;import java.util.Scanner;public class Lianxi1 {public static void main(String[] args) throws IOException {// TODO Auto-generated method stubScanner in = new Scanner(System.in);System.out.println("购物结算\n" + "*************************************\n" + "请选择购买商品的编号:\n"+ "1.T恤\t2.网球鞋\t3.网球拍\n" + "*************************************\n" + "请输入商品编号:");int bian;bian = in.nextInt();// 此处也可以用字符串定义,String a,//判断字符串相等的时候,用a.equals("y"),返回的是布尔类型的值。char a = 0;while (true) {switch (bian) {case 1:System.out.println("T恤\t¥245\n");break;case 2:System.out.println("网球鞋\t¥570\n");break;case 3:System.out.println("网球拍\t¥870\n");break;}System.out.println("是否继续(y/n):");a = (char) System.in.read();if (a == 'y') {System.out.println("请输入商品编号(1,2,3):");bian = in.nextInt();}if (a == 'n') {System.out.println("结束!");break;} }}}


import java.util.Scanner;public class Ch03 {public static void main(String[] args) {// TODO Auto-generated method stub//提示信息System.out.println("**************************");System.out.println("请选择购买的商品编号");System.out.println("1.T恤\t2.网球鞋\t3.网球拍");System.out.println("****************");Scanner sc=new Scanner(System.in);String str=null;//循环do{//输入编号System.out.print("请输入商品编号");//得到编号int num=sc.nextInt();//根据编号得到价格//根据编号显示价格switch (num) {case 1:System.out.println("T恤\t\t200");break;case 2:System.out.println("网球鞋\t\t570");break;case 3:System.out.println("网球拍\t\t300");break;default:System.out.println("输入编号有误");break;}//得到是否继续System.out.print("是否继续(y/n)");str=sc.next();} while(str.equals("y"));//根据输入信息确定是否继续System.out.println("谢谢使用");}}


原创粉丝点击