While循环结构

While循环结构_第1张图片
需求说明:
实现商品查价
1.输入商品编号,显示商品价格
2.循环查询
3.输入“n”退出循环
/**

  • while语句实现
  • @author 天才史努比

*/
importjava.util.Scanner;
public class WhileWork1 {
public static void main(String[] args) {
String name;
int price;
String answer = “y”;
while(answer.equals(“y”)) {
System.out.println(“请输入商品编号:”);
Scanner in = newScanner(System.in);
//获取商品编号
int goodsnum = in.nextInt();
switch (goodsnum) {
case 1:{
name = “T恤”;
price = 245;
System.out.println(“商品:”+name+ " "+“价格:”+price);
}
break;
case 2:{
name = “网球鞋”;
price = 570;
System.out.println(“商品:”+name+ " "+“价格:”+price);
}
break;
case 3:{
name = “网球拍”;
price = 320;
System.out.println(“商品:”+name+ " "+“价格:”+price);
}
break;
default:
break;
}
System.out.println(“是否继续?(y/n)”);
Scanner k =newScanner(System.in);
answer = k.nextLine();
}
System.out.println(“程序结束!”);
}
}

你可能感兴趣的:(While循环结构)