JAVA编程题目,求大神高手帮忙解答下,谢谢! 30
4个回答
展开全部
package org.xxl.demo;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ProtectPrice {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("*****************************************************");
System.out.println("请选择要购买的商品编号:");
List<Product> list = getProductDate() ;
if(list!=null && list.size()>0){
for (Product product : list) {
System.out.print(product.getNumber() + "、" + product.getName() + " ");
}
}
System.out.println();
System.out.println("*****************************************************");
String flag = "" ;
int productNumber = 0 , count = 0;
Product product = null ;
float tranAmt = 0 ;
while(!flag.equals("n")){
productNumber = getProductNumber() ;
switch (productNumber) {
case 1:
count = getProductCount() ;
product = list.get(0) ;
System.out.println(product.getName() + " ¥" + product.getPrice() + " 数量:" + count + " 合计:" + (float) product.getPrice()*count) ;
tranAmt += (float) (product.getPrice()*count*0.8) ;
break ;
case 2:
count = getProductCount() ;
product = list.get(1) ;
System.out.println(product.getName() + " ¥" + product.getPrice() + " 数量:" + count + " 合计:" + (float) product.getPrice()*count) ;
tranAmt += (float) (product.getPrice()*count*0.8) ;
break ;
case 3:
count = getProductCount() ;
product = list.get(2) ;
System.out.println(product.getName() + " ¥" + product.getPrice() + " 数量:" + count + " 合计:" + (float) product.getPrice()*count) ;
tranAmt += (float) (product.getPrice()*count*0.8) ;
break ;
default:
System.out.println("请输入正确的商品编号!!");
break;
}
flag = getFlag() ;
if(flag.equals("n")){
System.out.println("折扣:0.8");
System.out.println("应付金额:"+tranAmt);
System.out.print("实付金额:");
String amt = new Scanner(System.in).next() ;
float amts = Float.valueOf(amt) - tranAmt ;
while(amts<0){
System.out.println("应付金额:"+tranAmt);
amt = new Scanner(System.in).next() ;
amts = Float.valueOf(amt) - tranAmt ;
}
System.out.println("找钱:" + amts);
System.out.println("");
}
}
}
public static int getProductNumber(){
int number = 0 ;
try{
System.out.print("请输入商品编号:");
Scanner scanner1 = new Scanner(System.in) ;
if(scanner1!=null){
number = Integer.parseInt(scanner1.next()) ;
}
}catch(Exception e){
System.out.println("请输入0-9的自然数字!");
}
return number ;
}
public static int getProductCount(){
int count = 0 ;
try{
System.out.println("请输入商品数量:");
Scanner scanner = new Scanner(System.in) ;
if(scanner!=null){
count = Integer.parseInt(scanner.next()) ;
}
}catch(Exception e){
System.out.println("请输入0-9的自然数字!");
}
return count ;
}
public static String getFlag(){
String flag = "" ;
System.out.println("是否继续(y/n):");
Scanner scanner = new Scanner(System.in) ;
if(scanner!=null){
flag = scanner.next() ;
if(!flag.equals("y") && !flag.equals("n")){
getFlag() ;
}
}
return flag ;
}
public static List<Product> getProductDate(){
List<Product> list = new ArrayList<Product>() ;
Product p1 = new Product(1,"T 恤",245.0f) ;
Product p2 = new Product(2,"网球鞋",570.0f) ;
Product p3 = new Product(3,"网球拍",897.0f) ;
list.add(p1) ;
list.add(p2) ;
list.add(p3) ;
return list ;
}
}
class Product{
int number ;
String name ;
float price ;
public Product() {
}
public Product(int num,String name,float price){
this.number = num ;
this.name = name ;
this.price = price ;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
展开全部
import java.util.Scanner;
public class Buy {
/**
* @param args
*/
public static void main(String[] args) {
String str = "";
String no = "";
String num = "";
double count = 0.0 ;
double one = 0.0 ;
Scanner sc = new Scanner(System.in);
System.out.println("************************************************");
System.out.println("请选择购买的商品编号:");
System.out.println("1.T恤 2.网球鞋 3.网球拍");
System.out.println("************************************************");
while(!str.equals("n")){
System.out.println();
System.out.println();
System.out.print("请输入商品编号:");
no = sc.next();
System.out.print("请输入购买数量:");
num = sc.next();
switch (no.charAt(0)) {
case '1':
one = Integer.parseInt(num) * 245.0;
count += one ;
System.out.println("T恤 ¥245.0 数量 "+num+" 合计 ¥"+one);
break;
case '2':
one = Integer.parseInt(num) * 570.0;
count += one ;
System.out.println("网球鞋 ¥570.0 数量 "+num+" 合计 ¥"+one);
break;
case '3':
one = Integer.parseInt(num) * 770.0;
count += one ;
System.out.println("网球拍 ¥770.0 数量 "+num+" 合计 ¥"+one);
break;
default:
break;
}
System.out.print("是否继续(y/n)");
str = sc.next();
}
System.out.println();
System.out.println();
System.out.println("折扣:0.8");
System.out.println("应付金额:"+count);
System.out.print("实付金额:");
String m = sc.next();
System.out.println("找钱:"+(Double.parseDouble(m)-count));
}
}
public class Buy {
/**
* @param args
*/
public static void main(String[] args) {
String str = "";
String no = "";
String num = "";
double count = 0.0 ;
double one = 0.0 ;
Scanner sc = new Scanner(System.in);
System.out.println("************************************************");
System.out.println("请选择购买的商品编号:");
System.out.println("1.T恤 2.网球鞋 3.网球拍");
System.out.println("************************************************");
while(!str.equals("n")){
System.out.println();
System.out.println();
System.out.print("请输入商品编号:");
no = sc.next();
System.out.print("请输入购买数量:");
num = sc.next();
switch (no.charAt(0)) {
case '1':
one = Integer.parseInt(num) * 245.0;
count += one ;
System.out.println("T恤 ¥245.0 数量 "+num+" 合计 ¥"+one);
break;
case '2':
one = Integer.parseInt(num) * 570.0;
count += one ;
System.out.println("网球鞋 ¥570.0 数量 "+num+" 合计 ¥"+one);
break;
case '3':
one = Integer.parseInt(num) * 770.0;
count += one ;
System.out.println("网球拍 ¥770.0 数量 "+num+" 合计 ¥"+one);
break;
default:
break;
}
System.out.print("是否继续(y/n)");
str = sc.next();
}
System.out.println();
System.out.println();
System.out.println("折扣:0.8");
System.out.println("应付金额:"+count);
System.out.print("实付金额:");
String m = sc.next();
System.out.println("找钱:"+(Double.parseDouble(m)-count));
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
小白们好好学吧,这么简单的循环都不会。。。。问问自己问什么学这个呢?要学就学会它,要么就不学,选自己喜欢的方向不可以吗?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
给你看个简单点的把~!
public class C103 {
public static void main(String[] args) {
System.out.println("MyShopping管理系统>购物结算"+"\n");
System.out.println("***************************************************");
System.out.println("请选择购买的商品编号:");
System.out.println("1.T恤 2.网球鞋 3.网球拍");
System.out.println("***************************************************");
int price=0;
int b=0;
int c=0;
int d=0;
int total1=0;
int total2=0;
int total3=0;
double discount=0.8;
String name;
Scanner input=new Scanner(System.in);
System.out.print("输入请按(y):");
String a=input.next();
while(!a.equals("n"))
{
System.out.print("请输入商品编号:");
int q1=input.nextInt();
System.out.print("商品数量:");
int q=input.nextInt();
switch(q1)
{
case 1:
name="T恤";
b=245;
total1=b*q;
break;
case 2:
name="网球鞋";
c=570;
total2=c*q;
break;
case 3:
name="网球拍";
d=320;
total3=d*q;
break;
}
System.out.print("输入请按(y)退出请按(n):");
a=input.next();
}
System.out.println("折扣:"+discount);
System.out.println("应付金额:"+(total1+total2+total3)*discount);
double q3=(total1+total2+total3)*discount;
System.out.print("输入请实际付款金额:");
double q2=input.nextInt();
System.out.println("实付金额:"+q2);
System.out.println("找零:"+(q2-q3));
System.out.println("已经退出");
}
}
public class C103 {
public static void main(String[] args) {
System.out.println("MyShopping管理系统>购物结算"+"\n");
System.out.println("***************************************************");
System.out.println("请选择购买的商品编号:");
System.out.println("1.T恤 2.网球鞋 3.网球拍");
System.out.println("***************************************************");
int price=0;
int b=0;
int c=0;
int d=0;
int total1=0;
int total2=0;
int total3=0;
double discount=0.8;
String name;
Scanner input=new Scanner(System.in);
System.out.print("输入请按(y):");
String a=input.next();
while(!a.equals("n"))
{
System.out.print("请输入商品编号:");
int q1=input.nextInt();
System.out.print("商品数量:");
int q=input.nextInt();
switch(q1)
{
case 1:
name="T恤";
b=245;
total1=b*q;
break;
case 2:
name="网球鞋";
c=570;
total2=c*q;
break;
case 3:
name="网球拍";
d=320;
total3=d*q;
break;
}
System.out.print("输入请按(y)退出请按(n):");
a=input.next();
}
System.out.println("折扣:"+discount);
System.out.println("应付金额:"+(total1+total2+total3)*discount);
double q3=(total1+total2+total3)*discount;
System.out.print("输入请实际付款金额:");
double q2=input.nextInt();
System.out.println("实付金额:"+q2);
System.out.println("找零:"+(q2-q3));
System.out.println("已经退出");
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询