带参数java的多个方法

时间:2013-10-15 04:00:35

标签: java variables methods

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

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

在这个例子中,当我用运动模型键入car2时,变量的加法和减法是错误的。非常感谢,非常感谢任何帮助。

// 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.

1 个答案:

答案 0 :(得分: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;
}

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

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

if (acc.equals("y"))

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