繁体   English   中英

输入信息后程序冻结

[英]Program freezes after information in input

这是 PartyOrder 类的驱动程序,代码中没有错误。 我在这里先向您的帮助表示感谢。

import   javax.swing.JOptionPane;
public class Proj4
{
public static void main(String[] args)
{
    int iBalloons;  
    int iCandles;
    int iBanners;
    char    chrChoice;
    String strInput;        
    String strName; 
    String strInfo;
    strName = "Clayton Southerland's Party Store";

    JOptionPane.showMessageDialog(null,"~2014 Party Store~\n\nGet your Birthday Party Supplies Here!",
        strName,1);

    strInput= JOptionPane.showInputDialog(null,"2014 Birthday Balloons\n   Just $2.50 each!\n\n How many balloons would you like to order? (0-50)",
        strName ,3);
        iBalloons = Integer.parseInt(strInput);

    while(iBalloons > 50 || iBalloons < 0)
        {
        JOptionPane.showMessageDialog(null,"You MUST enter a number between 0-50!",
        strName,0);
        strInput= JOptionPane.showInputDialog(null,"2014 Birthday Balloons\n   Just $2.50 each!\n\n How many balloons would you like to order? (0-50)",
        strName ,3);
        iBalloons = Integer.parseInt(strInput);
        }

    strInput= JOptionPane.showInputDialog(null,"2014 Large Birthday Candles\n   Just $6.00 each!\n\n How many candles would you like to order? (0-50)",
        strName ,3);
        iCandles = Integer.parseInt(strInput);

    while(iCandles< 0 || iCandles > 50)  
        {
        JOptionPane.showMessageDialog(null,"You MUST enter a number between 0-50!",
        strName,0);
        strInput= JOptionPane.showInputDialog(null,"2014 Large Birthday Candles\n   Just $6.00 each!\n\n How many candles would you like to order? (0-50)",
        strName ,3);
        iCandles = Integer.parseInt(strInput);
        }

    strInput= JOptionPane.showInputDialog(null,"2014 Birthday Banners\n   Just $2.50 each!\n\n How many banners would you like to order? (0-50)",
        strName ,3);
        iBanners = Integer.parseInt(strInput);

    while(iBanners< 0 || iBanners > 50)  
        {
        JOptionPane.showMessageDialog(null,"You MUST enter a number between 0-50!",
        strName,0);
        strInput= JOptionPane.showInputDialog(null,"2014 Large Birthday Candles\n   Just $6.00 each!\n\n How many candles would you like to order? (0-50)",
        strName ,3);
        iBanners = Integer.parseInt(strInput);
        }

    strInput= JOptionPane.showInputDialog(null,"2014 Shipping Option\n\n(O)vernight shipping - $10.00\n(T)wo-Day shipping - $7.50\n(P)riority shipping - $5.00\n\nPlease select a shipping option(O,T, or P)",
        strName ,3);
        chrChoice = strInput.charAt(0);

    PartyOrder Order1 = new PartyOrder(iBalloons,iCandles,iBanners,chrChoice);
    strInfo = Order1.invoice();

    JOptionPane.showMessageDialog(null,strInfo,
        strName,0); 

    System.exit(0);

}//end Main(String)

}//end Proj4

我的程序在输入步骤后冻结,不再允许使用命令行中的任何内容。 它应该显示发票但它被冻结

import java.text.DecimalFormat;
public class PartyOrder
{
int balloons;           //number of balloons
int candles;            //number of candles
int banners;            //number of banners ordered
char    shippingOption; //either O,T,P
DecimalFormat d1 = new DecimalFormat ( "$##0.00" );
public PartyOrder()
{

}//end PartyOrder()

public PartyOrder(int balloons, int candles, int banners, char shipping)
{
    setBalloons(balloons);
    setCandles(candles);
    setBanners(banners);
    setShipping(shipping);
}//end PartyOrder(int,int,int,char)

public void setBalloons(int num)
{
    if (num>0)
        {
            balloons = num;
        }
}//end setBalloons(int)

public void setCandles(int num)
{
    if (num>0)
        {
            candles = num;
        }

}//end setCandles(int)

public void setBanners(int num)
{
    if (num>0)
        {
            banners = num;
        }
}//end setBanners(int)

public void setShipping(char option)
{
    if(option == 'O' && option == 'o')
        {;
            shippingOption = 'O';
        }
    else if (option == 'T' && option == 't')
        {
            shippingOption = 'T';
        }
    else if (option == 'P' && option == 'p')
        {
            shippingOption = 'P';
        }
    else
        {
            shippingOption = 'N';
        }
}//end setShipping(int)

public int getBalloons()
{
    return balloons;
}//end getBalloons()

public int getCandles()
{
    return candles;
}//end getCandles()

public int getBanners()
{
    return banners;
}//end getBanners()

public int getShipping()
{
    return shippingOption;
}//end getShipping()

public double shippingCost()
{
    double dShip;       //holds the value given in this method  
    dShip = 0.00;
    if (shippingOption == 'O')
        {
            dShip = 10.00;
        }
    else if (shippingOption == 'T')
        {
            dShip = 7.50;
        }
    else if (shippingOption == 'P')
        {
            dShip = 5.00;
        }
    else if (shippingOption == 'N')
        {
            dShip = 0.00;
        }

    return dShip;
}//end shippingCost()

public String shippingType()
{
    String strShip;     //holds the value given in this method  
    strShip = "";

        if (shippingOption == 'O')
                {
                    strShip = "Overnight";
                }
            else if (shippingOption == 'T')
                {
                    strShip = "Two-Day Shipping";
                }
            else if (shippingOption == 'P')
                {
                    strShip = "Priority Shipping";
                }
            else if (shippingOption =='N')
                {
                    strShip = "Normal (free) Shipping ";
                }
        return strShip;
}//end shippingType()

private double discount(char chr)
{
    double   dDisc;
    dDisc = 0.00;

    while (chr == 'B')
    {
        if (balloons <10)
            {
                dDisc = 0.00;
            }
        else if (balloons >=10 && balloons <20)
            {
                dDisc = 0.10;
            }
        else if (balloons >=20 && balloons <30)
            {
                dDisc = 0.15;
            }
        else if (balloons >=30 && balloons<40)
            {
                dDisc = 0.20;
            }
        else if (balloons >=40)
            {
                dDisc = 0.25;
            }
    }// end while

    while (chr == 'C')
    {
        if (candles <10)
            {
                dDisc = 0.00;
            }
        else if (candles >=10 && candles <20)
            {
                dDisc = 0.10;
            }
        else if (candles>=20 && candles <30)
            {
                dDisc = 0.15;
            }
        else if (candles >=30 && candles <40)
            {
                dDisc = 0.20;
            }
        else if (candles >=40)
            {
                dDisc = 0.25;
            }
    }// end while 

    while (chr == 'N')
    {
        if (banners <10)
            {
                dDisc = 0.00;
            }
        else if (banners >=10 && banners <20)
            {
                dDisc = 0.10;
            }
        else if (banners >=20 &&banners <30)
            {
                dDisc = 0.15;
            }
        else if (banners >=30 && banners <40)
            {
                dDisc = 0.20;
            }
        else if (banners >=40)
            {
                dDisc = 0.25;
            }
    }// end while 

    return dDisc;

}//end discount(char)

private double subtotal(char chr)
{
    double dSub;
    dSub = 0.00;

    if (chr == 'B')
        {
            dSub = ((balloons * 2.50)*(discount('B'))) + (balloons * 2.50);
        }
    else if (chr == 'C')
        {
            dSub = ((candles * 6.00)*(discount('C'))) + (candles * 6.00);
        }
    else if (chr == 'N')
        {
            dSub = ((banners * 2.00)*(discount('N'))) + (banners* 2.00);
        }

    return dSub;
}//end subtotal(char)

public double subtotal()
{
    double  dSubtot;
    dSubtot = 0.00;

    dSubtot = subtotal('B')+ subtotal('C') + subtotal('N');

    return  dSubtot;
}//end subtotal()

public double tax()
{
    double dTax;
    dTax = (.05 * subtotal());
    return dTax;
}//end tax()

public double orderTotal()
{
    double  dTotal;
    dTotal = subtotal() + tax() + shippingCost();
    return  dTotal;
}//end orderTotal()

public String invoice()
{
    String strInfo;

    strInfo = " Balloons: " + balloons +" @ $2.50  = " + d1.format(balloons * 2.50) + " * Discount Rate: " + discount('B') + " = " + subtotal('B');
    strInfo+="\n Candles: " + candles + " @ $6.00  = "  + d1.format(candles * 6.00) + " * Discount Rate: " + discount('C') + " = " + subtotal('C');
    strInfo+="\n Banners: " + banners + " @ $2.00  = "  + d1.format(banners * 2.00) + " * Discount Rate: " + discount('N') + " = " + subtotal('N');
    strInfo+="\nSubtotal: " + d1.format(subtotal());
    strInfo+="\n\tTax: " + d1.format(tax());
    strInfo+="\nShipping: " + d1.format(shippingCost()) + " - " + shippingType();
    strInfo+="\nTotal: " + d1.format(orderTotal());

    return strInfo;
}//end invoice()

}

在“discount()”方法中,您陷入无限循环:

private double discount(char chr)
{
    double   dDisc;
    dDisc = 0.00;

    while (chr == 'B')
    {
        if (balloons <10)
        {
            dDisc = 0.00;
            break;//add it here
        }
        else if (balloons >=10 && balloons <20)
        {
            dDisc = 0.10;
            break;//add it here
            ...

您应该按照您的逻辑为这些循环添加一个break

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM