问题描述
小弟我刚接触JAVA不是太明白。有一个这样题目,我不懂。希望各位好心人帮下忙!可能的话在帮我讲解下!谢谢了***********************************************1.T恤2.网球鞋3.网球拍***********************************************请输入商品编号:1请输入购买数量:2T恤¥245.0¥490.0是否继续(Y/N)Y请输入商品编号:2请输入商品数量:1网球鞋¥570.0¥570.0是否继续(Y/N)N折扣:0.8金额总计¥848.0实际交费¥900找钱¥52.0
解决方案
解决方案二:
packagetest;importjava.util.Scanner;publicclassTestBuyer{/***@paramargs*/publicstaticvoidmain(String[]args){longsum=0l;System.out.println("***********************************************");System.out.println();System.out.println("1.T恤2.网球鞋3.网球拍");System.out.println("***********************************************");System.out.println();while(true){longsinglePrice=0l;System.out.print("请输入商品编号:");Scannersc1=newScanner(System.in);Stringsc1Value=sc1.next();System.out.print("请输入购买数量:");Scannersc2=newScanner(System.in);Stringsc2Value=sc2.next();switch(newInteger(sc1Value).intValue()){case1:singlePrice+=newInteger(sc2Value).intValue()*245.0;System.out.println("T恤¥245.0"+singlePrice);sum+=singlePrice;break;case2:singlePrice+=newInteger(sc2Value).intValue()*570.0;System.out.println("网球鞋¥570.0"+singlePrice);sum+=singlePrice;break;case3:singlePrice+=newInteger(sc2Value).intValue()*650.0;System.out.println("网球拍¥650.0"+singlePrice);sum+=singlePrice;break;default:System.out.print("输入错误");}System.out.print("是否继续(Y/N)");Scannersc3=newScanner(System.in);if(sc3.next().equalsIgnoreCase("N")){break;}}System.out.println("折扣:0.8");System.out.println("金额总计¥"+sum);System.out.print("实际交费¥");Scannersc=newScanner(System.in);longremain=newLong(sc.next()).longValue()-sum;if(remain<0){System.out.println("实际交费不足");}else{System.out.println("找钱¥"+remain);}}}
解决方案三:
不好意思,sum和singlePrice应该是double型的
解决方案四:
支持楼上的
解决方案五:
publicstaticvoidmain(String[]args)throwsException{System.out.println("***********************************************n1.T恤2.网球鞋3.网球拍n***********************************************");booleanisContiune=true;BufferedReaderreader=newBufferedReader(newInputStreamReader(System.in));Stringstr=null;intnum=0;intcount=0;intprice=0;Stringname=null;intsumprice=0;while(isContiune){System.out.println("请输入商品编号:");str=reader.readLine();num=Integer.valueOf(str);name=num==1?"T恤":num==2?"网球鞋":"网球拍";System.out.println("请输入购买数量:");str=reader.readLine();count=Integer.valueOf(str);price=num==1?245:num==2?570:500;sumprice+=price;System.out.println(name+"t¥"+price+"t¥"+price*count);System.out.println("是否继续(Y/N");str=reader.readLine();if("n".equalsIgnoreCase(str)){isContiune=false;}}System.out.println("输入折扣:");str=reader.readLine();doublediscount=Double.valueOf(str);System.out.println("折扣:"+discount);System.out.println("金额总计:"+sumprice);System.out.println("实际缴费:¥"+900);System.out.println("找钱:"+(900-sumprice));}
解决方案六:
我是把各个方法都封装起来的楼主看下:importjava.util.*;publicclassRetailer{privatestaticfinaldoubleTSHIRT_PRICE=245.0;privatestaticfinaldoubleSNEAKER_PRICE=570.0;privatestaticfinaldoubleRACKET_PRICE=700.0;//这个是我自己加的privatestaticintpieces;privatestaticintoption;privatestaticdoublediscount,change,sum;publicstaticvoidrequestProduct(){System.out.println("请输入商品编号:");}publicstaticvoidrequestNum(){System.out.println("请输入购买数量:");}publicstaticvoidrequestDiscount(){System.out.println("折扣:");}publicstaticvoidrequestMoney(){System.out.println("实际交费:");}publicstaticvoiddealRequestProduct(Stringstr){if(str.equals("1"))option=1;elseif(str.equals("2"))option=2;elseoption=3;}publicstaticvoiddealDiscount(Stringstr){discount=Double.parseDouble(str);sum*=discount;}publicstaticvoiddealMoney(Stringstr){doubletemp=Double.parseDouble(str);System.out.println("实际缴费:"+temp);change=temp-sum;}publicstaticvoiddealRequestNum(Stringstr){pieces=Integer.parseInt(str);if(option==1)sum+=TSHIRT_PRICE*pieces;elseif(option==2)sum+=SNEAKER_PRICE*pieces;elsesum+=RACKET_PRICE*pieces;}publicRetailer(){Stringstr;Scannerscanner;System.out.println("***********************************************n1.T恤2.网球鞋3.网球拍n***********************************************nn");do{scanner=newScanner(System.in);requestProduct();str=scanner.nextLine();dealRequestProduct(str);requestNum();str=scanner.nextLine();dealRequestNum(str);System.out.println("n是否继续?输入"Y"继续选购程序,输入"N"退出。n");str=scanner.nextLine();}while(!str.toUpperCase().equals("N"));requestDiscount();str=scanner.nextLine();dealDiscount(str);System.out.println("金额总计:"+sum);requestMoney();str=scanner.nextLine();dealMoney(str);System.out.println("找钱:"+change);}publicstaticvoidmain(Stringargs[]){newRetailer();}}
解决方案七:
importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;publicclassSport{publicstaticvoidmain(String[]args)throwsException{System.out.println("***********************************************");System.out.println("1.T恤2.网球鞋3.网球拍");System.out.println("***********************************************");Stringtext1="";Stringtext2="";Stringtext3="";Stringtext4="";Stringtext5="";doublesum1=0;booleanflag=true;while(flag){BufferedReaderbufferedReader1=newBufferedReader(newInputStreamReader(System.in));try{System.out.print("请输入商品编号:");text1=bufferedReader1.readLine();System.out.print("请输入购买数量:");text2=bufferedReader1.readLine();}catch(IOExceptione){e.printStackTrace();}doublesum2=0;try{switch(Integer.parseInt(text1)){case1:sum1+=245.0*Integer.parseInt(text2);sum2+=245.0*Integer.parseInt(text2);System.out.println("T恤¥245.0"+sum2);break;case2:sum1+=570.0*Integer.parseInt(text2);sum2+=570.0*Integer.parseInt(text2);System.out.println("T恤¥570.0"+sum2);break;case3:sum1+=100.0*Integer.parseInt(text2);sum2+=100.0*Integer.parseInt(text2);System.out.println("T恤¥100.0"+sum2);break;}}catch(Exceptione){System.out.println("输入错误!");e.printStackTrace();}System.out.print("是否继续(Y/N):");try{text3=bufferedReader1.readLine();}catch(IOExceptione){e.printStackTrace();}if(!("Y".equalsIgnoreCase(text3))){flag=false;System.out.print("折扣:");try{text4=bufferedReader1.readLine();}catch(IOExceptione){e.printStackTrace();}try{System.out.println("金额总计¥"+sum1*Double.parseDouble(text4));}catch(Exceptione){System.out.println("输入错误!");e.printStackTrace();}System.out.println("实际交费¥:");try{text5=bufferedReader1.readLine();}catch(IOExceptione){e.printStackTrace();}try{System.out.println("找钱¥"+(Double.parseDouble(text5)-sum1*Double.parseDouble(text4)));}catch(Exceptione){System.out.println("输入错误!");e.printStackTrace();}}else{flag=true;}sum2=0;}}}
解决方案八:
还不如用c实现呢
解决方案九:
路过