请帮助我更正此编码.很紧急. [英] please help me correct this coding. very urgent.

查看:67
本文介绍了请帮助我更正此编码.很紧急.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

公司的供应商将根据客户购买的总单位数和每个单位的价格为其产品提供一些折扣.

编写程序以解决问题并在
之后显示价格 折扣.

A vendor of company will give some discount on its products based on the total units bought by the customer and the price of each unit.

Write the program to solve the problem and display the price after
discount.

import javax.swing.*;
import java.text.DecimalFormat;

public class afterDiscount
  {
    public static void main(String args[])
       {
           int item;
           int sold;
           double price;
           double discount;
           double afterDiscount;
           double prices;
           double totalPrice;
           String str;

           DecimalFormat twoDeci = new DecimalFormat("##,##0.00");

           str = JOptionPane.showInputDialog("Please enter total number    
                 of item bought: ");
           item = Integer.parseInt(str);

           price=0;
           afterDiscount=0;

           str = JOptionPane.showInputDialog("Please enter the price for 
                 the item bought: ");
           prices = Double.parseDouble(str);

               if(item < 10 && price >= 0.01 || price <= 10.00)
               {
                   totalPrice = item * price;
                   afterDiscount = totalPrice;
               }
               else if(item < 10 && price >= 10.01 || price <= 100.00)
               {
                   totalPrice = item * price;
                   discount = totalPrice * 0.02;
                   afterDiscount = totalPrice - discount;
               }
               else if(item < 10 && price >= 100.01)
               {
                   totalPrice = item * price;
                   discount = totalPrice * 0.05;
                   afterDiscount = totalPrice - discount;
               }
               else if(item >= 10 || item <;100 && price <= 10.00)
               {
                   totalPrice = item * price;
                   discount = totalPrice * 0.05;
                   afterDiscount = totalPrice - discount;
               }
               else if(item >= 10 || item <100 && price >= 10.01 || 
                       price <= 100.00)
               {
                   totalPrice = item * price;
                   discount = totalPrice * 0.07;
                   afterDiscount = totalPrice - discount;
               }
               else if(item >= 10 || item <100 && price >= 100.01)
               {
                   totalPrice = item * price;
                   discount = totalPrice * 0.09;
                   afterDiscount = totalPrice - discount;
               }
               else if(item >= 100 || item <500 && price <= 10.00)
               {
                   totalPrice = item * price;
                   discount = totalPrice * 0.09;
                   afterDiscount = totalPrice - discount;
               }
               else if(item >= 100 || item <500 && price >= 10.01 || 
                       price <= 100.00)
               {
                   totalPrice = item * price;
                   discount = totalPrice * 0.15;
                   afterDiscount = totalPrice - discount;
               }
               else if(item >= 100 || item <500 && price >= 100.01)
               {
                   totalPrice = item * price;
                   discount = totalPrice * 0.21;
                   afterDiscount = totalPrice - discount;
               }
               else if(item >= 500 || item <1000 && price <= 10.00)
               {
                   totalPrice = item * price;
                   discount = totalPrice * 0.14;
                   afterDiscount = totalPrice - discount;
               }
               else if(item >= 500 || item <1000 && price >= 10.01 || 
                       price <= 100.00)
               {
                   totalPrice = item * price;
                   discount = totalPrice * 0.23;
                   afterDiscount = totalPrice - discount;
               }
               else if(item >= 500 || item <1000 && price >= 100.01)
               {
                   totalPrice = item * price;
                   discount = totalPrice * 0.32;
                   afterDiscount= totalPrice - discount;
               }
               else if(item > 1000 && price <= 10.00)
               {
                   totalPrice = item * price;
                   discount = totalPrice * 0.21;
                   afterDiscount = totalPrice - discount;
               }
               else if(item > 1000 && price >= 10.01 || price <= 100.00)
               {
                   totalPrice = item * price;
                   discount = totalPrice * 0.32;
                   afterDiscount = totalPrice - discount;
               }
               else if(item > 1000 && price >= 100.01)
               {
                   totalPrice = item * price;
                   discount = totalPrice * 0.43;
                   afterDiscount = totalPrice - discount;
               }

               System.out.print("Here your total price after less: $ "  
               +twoDeci.format(afterDiscount));
            }
        }




////请给我发送电子邮件至protomanexe92@yahoo.com//




// please email me at protomanexe92@yahoo.com //

推荐答案

" + twoDeci.format(afterDiscount)); } }
" +twoDeci.format(afterDiscount)); } }




//请给我发电子邮件protomanexe92@yahoo.com//




// please email me at protomanexe92@yahoo.com //



这是经过优化的代码,易于理解和修改.

Hi
Here is optimized code for easy to understand and easy to modify

if (item < 10 && price >= 0.01 || price <= 10.00)
    discount = 0;
else if (item < 10 && price >= 10.01 || price <= 100.00)
    discount = 0.02;
else if (item < 10 && price >= 100.01)
    discount = 0.05;
else if (item >= 10 || item < 100 && price <= 10.00)
    discount = 0.05;
else if (item >= 10 || item < 100 && price >= 10.01 || price <= 100.00)
    discount = 0.07;
else if (item >= 10 || item < 100 && price >= 100.01)
    discount = 0.09;
else if (item >= 100 || item < 500 && price <= 10.00)
    discount = 0.09;
else if (item >= 100 || item < 500 && price >= 10.01 || price <= 100.00)
    discount = 0.15;
else if (item >= 100 || item < 500 && price >= 100.01)
    discount = 0.21;
else if (item >= 500 || item < 1000 && price <= 10.00)
    discount = 0.14;
else if (item >= 500 || item < 1000 && price >= 10.01 || price <= 100.00)
    discount = 0.23;
else if (item >= 500 || item < 1000 && price >= 100.01)
    discount = 0.32;
else if (item > 1000 && price <= 10.00)
    discount = 0.21;
else if (item > 1000 && price >= 10.01 || price <= 100.00)
    discount = 0.32;
else if (item > 1000 && price >= 100.01)
    discount =  0.43;
totalPrice = item * price;
double toaldiscount = totalPrice * discount;
afterDiscount = totalPrice - toaldiscount;




如有任何疑问,请让我知道.

如果有帮助,请提供"投票",如果这是正确的答案,请提供"接受答案".:rose:

谢谢,
Imdadhusen




Please do let me know, if you have any doubt.

Please provide "Vote" if this would be helpful, and make "Accept Answer" if this would be correct answer.:rose:

Thanks,
Imdadhusen


没有什么可以解决的,尽管Switch/Case比许多If/Then/Elses更好.

您遇到的实际问题是什么?


注意:已声明此行prices = Double.parseDouble(str);,但从未使用过价格.

我认为可能要看一下.
Nothing jumps out, although a Switch/Case would be better than a lot of If/Then/Elses.

What is actual problem you are having?


Note: This line prices = Double.parseDouble(str); is declared, but prices is never used.

I think that may be something to look at.


这篇关于请帮助我更正此编码.很紧急.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆