我問的問題可能對你們來說很簡單,但是我是一個完全的初學者,在java編程中陷入了這個問題。問題是我必須使用多個方法和參數以及添加變量。帶參數的多個方法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.
它通常不贊成上傳整個源文件,然後只是要求我們爲您解決它。你認爲問題是什麼? – Michael
@邁克爾我相信是其他如果在公共靜態字符串sportsModelCar – user2875021