0

我要问的问题对你们来说可能很简单,但是我是Java编程的完全初学者并且陷入了这个问题。问题是我必须使用多种方法和参数以及添加变量。

例如,我必须输入一个用户输入并将不同的整数变量加在一起。我可以编译程序,但不知何故,变量的添加是错误的。

在这个例子中,当我输入car2 with sports model时,变量的加减法是错误的。非常感谢您,非常感谢您的帮助。

// Methods.java
import javax.swing.*; // import swing lib
public class MethodsTest
{
// global int access for all methods
static int basicPrice = 0;
static int carPaints = 0;
static int sportsModel = 0;
static int discount = 0;
static int priceTotal = 0;

public static void main(String[] args)
{
    carModel();
    System.exit(0);
}

public static void carModel()
{
    String askCarTypes = "";
    String carTypes = "";

    askCarTypes = JOptionPane.showInputDialog
    ("Car1 or Car2?");

    carTypes = typesOfCar(askCarTypes);

    String askSportsModel = "";
    String carSportsModel = "";

    askSportsModel = JOptionPane.showInputDialog
    ("Sports Model? (y/n)");

    carSportsModel = sportsModelCar(askSportsModel);

    JOptionPane.showMessageDialog
    (null, "Basic Price: " + basicPrice + "\n" +
        "Car Paints: " + carPaints + "\n" +
        "Car Model: " + sportsModel + "\n" + 
        "Discount: " + discount + "\n" +
        "Total: " + priceTotal);

    return;

}

/* Calculating basic price w/o solar panel
String args = types */
public static String typesOfCar(String types)
{
    String a = "";

    if (firstCar(types))
    {
        basicPrice = basicPrice + 20000;
        priceTotal = basicPrice;
    }
    else if (secondCar(types))
    {
        basicPrice = basicPrice + 20000;
        carPaints = carPaints + 2000;
        priceTotal = basicPrice + carPaints;
    }
    else 
    {
        JOptionPane.showMessageDialog
        (null, "Sorry we have no price available for that model.");
        return a;
    }

    return a;
}

/* Calculating price w/ sports
String args = acc */
public static String sportsModelCar(String acc)
{
    String b = "";

    if (car1SportsModel(acc))
    {
        sportsModel = sportsModel + 5000;
        priceTotal = basicPrice + sportsModel;
    }
    else if (car2SportsModel(acc))
    {
        sportsModel = sportsModel + 5000;
        discount = discount - 500;
        priceTotal = basicPrice + carPaints + sportsModel + discount;
    }

    return b;
}

public static boolean firstCar (String types)
{
    if (types.equals("car1"))
    {
        return true;
    }
    else
    {
        return false;
    }
}

public static boolean secondCar (String types)
{
    if (types.equals("car2"))
    {
        return true;
    }
    else
    {
        return false;
    }
}

public static boolean car1SportsModel (String acc)
{
    if (acc.equals("y"))
    {
        return true;
    }
    else
    {
        return false;
    }
}

public static boolean car2SportsModel (String acc)
{
    if (acc.equals("y"))
    {
        return true;
    }
    else
    {
        return false;
    }
}
}
/* Output with car2 and sportsModel
Basic Price: 20000
Car Paints: 2000
Car Model: 5000
Discount: 0
Total: 25000 */

折扣应显示为 -500,总计应为 26500。

4

1 回答 1

1

您没有设置值“折扣”。

if (car1SportsModel(acc))
{
    sportsModel = sportsModel + 5000;
    priceTotal = basicPrice + sportsModel;
}
else if (car2SportsModel(acc))
{
    sportsModel = sportsModel + 5000;
    discount = discount - 500;
    priceTotal = basicPrice + carPaints + sportsModel + discount;
}

car1SportsModeland方法在功能上是等价的car2SportsModel,因此不可能在上面的代码片段中点击 else 语句。discount永远不会被设置,所以它保持原来的 0 值。

事实上,你甚至不应该拥有这些功能。相反,你为什么不做这样的事情呢?

if (acc.equals("y"))

实际上,您可能应该创建Car某种类并使用它来比较事物。

于 2013-10-15T04:06:57.320 回答