java - 在java中,可以在不使用数组的情况下在另一个方法的循环中调用/调用一个方法吗?

标签 java loops methods return-value

public static double numBeers()
{
    String numBeersStr;
    double numBeers;

    numBeersStr = JOptionPane.showInputDialog("How many beers would you like?");

    numBeers = Double.parseDouble(numBeersStr);

    if (numBeers < 3 || numBeers > 6)
    {
        numBeersStr = JOptionPane.showInputDialog("You must pick 3-6 beers");
        numBeers = Double.parseDouble(numBeersStr);
    }

    return numBeers;
}

public static double beerPrice(double theBeerBrand)
{

    double beer = 0;
    final double LAGIMRED = 1.90;  
    final double DESINIPA = 2.00;
    final double BELBEBRN = 1.80;
    final double SCHOATST = 2.50;
    final double BOULFHSAISN = 2.75;
    final double GANDANCPRTR = 1.75;


    if (theBeerBrand == 1)
        beer = LAGIMRED;

    else if (theBeerBrand == 2)
        beer = DESINIPA;

    else if (theBeerBrand == 3)
        beer = BELBEBRN;

    else if (theBeerBrand == 4)
        beer = SCHOATST;

    else if (theBeerBrand == 5)
        beer = BOULFHSAISN;

    else 
        beer = GANDANCPRTR;        

    return beer;

}


public static double beerBrand(double theNumBeers )
{   
    String beerTypeStr;
    double count = 1, total = 0, beerType, beeer = 0;

    while (count <= theNumBeers)
    {    
        beerTypeStr = JOptionPane.showInputDialog("Please choose between these fine selections:\n1 - Lagunitas Imperial Red - $1.90\n2 - Deschutes Inversion IPA - $2.00\n3 - Bell's Best Brown Ale - 1.80\n4 - Schlafly's Oatmeal Stout - $2.50" +"\n5 - Boulevard's Farmhouse Saison - $2.75\n6 - Gandy Dancer Porter - $1.75");

            beerType = Double.parseDouble(beerTypeStr);
            // method to be invoked/called------> beeer = beerPrice(beerType);


        count++;
    }    

    total += beeer;

    return total;

我试图在 beerBrand 方法 while 循环中调用 beerPrice 方法。每次程序提示用户想要什么类型的啤酒时,该类型的啤酒都会添加到总数中,然后再次询问该问题,次数与用户想要的啤酒数量相同。我很确定我解决了提示用户与他们想要的啤酒数量相同的次数的问题,我只是没有得到我想要的输出。

如果有人知道如何捕获该类型的啤酒并将其添加到总数中,那么我将不胜感激,这样我就可以使用折扣价格和最终价格的总数。我不想使用任何数组或任何更复杂的东西,因为这是一个章节测试的练习程序,没有涉及数组等。非常感谢您提供的任何帮助。

最佳答案

您可以保留有效的行:

beeer = beerPrice(beerType);

但是,您需要在每次循环时将价格添加到总计中,因此将相应的行带回到 while 中:

while (count <= theNumBeers) {    
    beerTypeStr = JOptionPane.showInputDialog(...);
    beerType = Double.parseDouble(beerTypeStr);
    beeer = beerPrice(beerType);
    total += beeer;

    count++;
}

尽可能限制变量的范围也是一个很好的做法 - 而且你确实是一个 for ,而且我想你通常会订购整瓶啤酒,所以你可以使用 int 而不是 double 来表示啤酒的数量。因此,您可以像这样重构您的方法:

public static double beerBrand(int theNumBeers) {   
    double total = 0;

    for (int count = 0; count < theNumBeers; count++) {    
        String beerTypeStr = JOptionPane.showInputDialog(...);
        double beerType = Double.parseDouble(beerTypeStr);
        total += beerPrice(beerType);
    }

    return total;
}

注意:我还没有检查你的其余代码。

关于java - 在java中,可以在不使用数组的情况下在另一个方法的循环中调用/调用一个方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13334105/

相关文章:

java - Checkstyle 规则强制类部分的空行分隔

java - 在 java 中将 CSV 复制到数组时遇到问题

java - 道。 JDBC。在哪里放置连接对象?

ruby - Watir:如何检索与属性匹配的所有 HTML 元素? (类、ID、标题等)

c - 使用 GDB 更改 for 循环条件?

java - Java中的方法覆盖与类变量覆盖

java - 在 SWT/Eclipse RCP 中对基本树进行排序

python - 删除列表中的匹配项目

java - 检查方法参数是否为空?

java - 传递参数的最佳实践