帮忙写一下Java中while循环语句的代码,题目请看补充
商品列表:1.网球鞋¥3202.网球拍¥2003.T恤¥100请选择购买的商品编号:接收输入的商品编号:请记录购买的商品个数请将购物的总金额算出。每完成一次购买提问用户是...
商品列表:
1.网球鞋¥320 2.网球拍¥200 3.T恤¥100
请选择购买的商品编号:
接收输入的商品编号:
请记录购买的商品个数
请将购物的总金额算出。
每完成一次购买提问用户是否继续购买
求高手写一下这个代码,刚学玩这个,没有思路啊!! 展开
1.网球鞋¥320 2.网球拍¥200 3.T恤¥100
请选择购买的商品编号:
接收输入的商品编号:
请记录购买的商品个数
请将购物的总金额算出。
每完成一次购买提问用户是否继续购买
求高手写一下这个代码,刚学玩这个,没有思路啊!! 展开
展开全部
public static void main(String args[]) {
boolean isC = true;// 是否继续购买
int money = 0;// 总价
int goods1 = 0;// 商品1
int goods2 = 0;// 商品2
int goods3 = 0;// 商品3
BufferedReader br = null;
try {
while (isC) {
int price = 0;
System.out.println("请选择购买的商品编号:");
br = new BufferedReader(new InputStreamReader(System.in));
int num1 = Integer.parseInt(br.readLine());
System.out.println("请选择购买的商品数理:");
br = new BufferedReader(new InputStreamReader(System.in));
int num2 = Integer.parseInt(br.readLine());
System.out.println(num1);
switch (num1) {
case 1:
price = 320 * num2;
goods1 = goods1 + num2;
break;
case 2:
price = 200 * num2;
goods2 = goods2 + num2;
break;
case 3:
price = 100 * num2;
goods3 = goods3 + num2;
// default:System.out.println("您选择的商品不存在!");;1
}
money = money + price;
System.out.println("是否继续购买:Y/N");
br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
if ("Y".equals(str)) {
isC = true;
} else {
isC = false;
}
}
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("你购买了如下商品:");
if(goods1>0){
System.out.println("网球鞋:"+goods1);
}
if(goods2>0){
System.out.println("网球拍:"+goods2);
}
if(goods3>0){
System.out.println("T恤:"+goods3);
}
System.out.println("总价:"+money);
}
boolean isC = true;// 是否继续购买
int money = 0;// 总价
int goods1 = 0;// 商品1
int goods2 = 0;// 商品2
int goods3 = 0;// 商品3
BufferedReader br = null;
try {
while (isC) {
int price = 0;
System.out.println("请选择购买的商品编号:");
br = new BufferedReader(new InputStreamReader(System.in));
int num1 = Integer.parseInt(br.readLine());
System.out.println("请选择购买的商品数理:");
br = new BufferedReader(new InputStreamReader(System.in));
int num2 = Integer.parseInt(br.readLine());
System.out.println(num1);
switch (num1) {
case 1:
price = 320 * num2;
goods1 = goods1 + num2;
break;
case 2:
price = 200 * num2;
goods2 = goods2 + num2;
break;
case 3:
price = 100 * num2;
goods3 = goods3 + num2;
// default:System.out.println("您选择的商品不存在!");;1
}
money = money + price;
System.out.println("是否继续购买:Y/N");
br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
if ("Y".equals(str)) {
isC = true;
} else {
isC = false;
}
}
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("你购买了如下商品:");
if(goods1>0){
System.out.println("网球鞋:"+goods1);
}
if(goods2>0){
System.out.println("网球拍:"+goods2);
}
if(goods3>0){
System.out.println("T恤:"+goods3);
}
System.out.println("总价:"+money);
}
追问
谢谢你,我把你的也看了一遍。你写的这个更人性化了,不愧是高手。受教了!
展开全部
while(true){
System.out.println("请选择购买的商品编号:");
BufferedReader str1=new BufferedReader(new InputStreamReader(System.in));
int a=Integer.parseInt(str1.readLine());
BufferedReader str2=new BufferedReader(new InputStreamReader(System.in));
int b=Integer.parseInt(str2.readLine());
int sum = a*b;
System.out.println("用户是否继续购买(Y/N)?");
BufferedReader str3=new BufferedReader(new InputStreamReader(System.in));
if(str3.equles("N")){
break;
}
}
}
大致代码如上,简单的写了下 如果严谨一点应该捕获转换异常,并且判断用户输入的内容是否正确
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.util.Scanner;
public class Test{
public static void main(String args[]){
System.out.println("\n\n商品列表");
String str="1:人妖 1320,2:小姐 300,3:少爷 800";
System.out.println(str);
Scanner can = new Scanner(System.in);
Boolean flag = true;
while(flag){
System.out.println("请选择购买的商品编号:");
int index = can.nextInt();
System.out.println("请选择购买的数量:");
int num = can.nextInt();
System.out.println("你点的总价:"+(Integer.parseInt(str.split(",")[index-1].split(" ")[1])*num));
System.out.println("是否继续享受?");
String f = can.next();
if(f.equals("是")){
flag = true;
}else{
flag = false;
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-10-24
展开全部
public static void main(String[] args){
double sum=0;
boolean YsOrNo=true;
int danjia=0;
String bianhao;
while(YsOrNo){
Scanner sc=new Scanner(System.in);
System.out.println("Please enter 编号: ");
bianhao=sc.next();
System.out.println("Please enter 数量: ");
int num=Integer.parseInt(sc.next());
if(bianhao.equals("1")){
danjia=320;
}else if(bianhao.equals("2")){
danjia=200;
}else if(bianhao.equals("2")){
danjia=100;
}
sum+=danjia*num;
System.out.println("是否继续购买(Y/N)? ");
if(sc.next().equals("N")){
YsOrNo=false;
System.out.println("总金额是:"+sum);
break;
}else{
continue;
}
}
}
double sum=0;
boolean YsOrNo=true;
int danjia=0;
String bianhao;
while(YsOrNo){
Scanner sc=new Scanner(System.in);
System.out.println("Please enter 编号: ");
bianhao=sc.next();
System.out.println("Please enter 数量: ");
int num=Integer.parseInt(sc.next());
if(bianhao.equals("1")){
danjia=320;
}else if(bianhao.equals("2")){
danjia=200;
}else if(bianhao.equals("2")){
danjia=100;
}
sum+=danjia*num;
System.out.println("是否继续购买(Y/N)? ");
if(sc.next().equals("N")){
YsOrNo=false;
System.out.println("总金额是:"+sum);
break;
}else{
continue;
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
package tt;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.omg.CORBA.PUBLIC_MEMBER;
public class dee {
public static void main(String[] args) throws NumberFormatException, IOException {
int productId=0;
int numberProduct=0;
boolean continue_shop=true;
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String string=null;
while(continue_shop)
{
System.out.println("1.网球鞋¥320 2.网球拍¥200 3.T恤¥100");
System.out.println("输入产品号:");
productId=Integer.parseInt(in.readLine());
System.err.println("输入数量:");
numberProduct=Integer.parseInt(in.readLine());
System.out.println("金额:"+getPrice(productId, numberProduct));
System.out.println("yes/no?");
if(!in.readLine().toUpperCase().equals("YES"))
{
continue_shop=false;
}
}
}
public static int getPrice(int productId,int numberProduct)
{
int i=0;
switch (productId) {
case 1:i=320*numberProduct;
break;
case 2:i=200*numberProduct;
break;
case 3:i=100*numberProduct;
break;
}
return i;
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.omg.CORBA.PUBLIC_MEMBER;
public class dee {
public static void main(String[] args) throws NumberFormatException, IOException {
int productId=0;
int numberProduct=0;
boolean continue_shop=true;
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String string=null;
while(continue_shop)
{
System.out.println("1.网球鞋¥320 2.网球拍¥200 3.T恤¥100");
System.out.println("输入产品号:");
productId=Integer.parseInt(in.readLine());
System.err.println("输入数量:");
numberProduct=Integer.parseInt(in.readLine());
System.out.println("金额:"+getPrice(productId, numberProduct));
System.out.println("yes/no?");
if(!in.readLine().toUpperCase().equals("YES"))
{
continue_shop=false;
}
}
}
public static int getPrice(int productId,int numberProduct)
{
int i=0;
switch (productId) {
case 1:i=320*numberProduct;
break;
case 2:i=200*numberProduct;
break;
case 3:i=100*numberProduct;
break;
}
return i;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询