gpt4 book ai didi

java - 带参数的多个方法java

转载 作者:行者123 更新时间:2023-12-01 14:00:36 25 4
gpt4 key购买 nike

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

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

在这个例子中,当我输入带有运动模型的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。

最佳答案

您没有设置值“discount”。

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 类并使用它来比较事物。

关于java - 带参数的多个方法java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19373077/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com