2013-10-15 96 views
0

我問的問題可能對你們來說很簡單,但是我是一個完全的初學者,在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.

+5

它通常不贊成上傳整個源文件,然後只是要求我們爲您解決它。你認爲問題是什麼? – Michael

+0

@邁克爾我相信是其他如果在公共靜態字符串sportsModelCar – user2875021

回答

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類某種並用它來比較不同的事物。

+0

的方法是的我可能已經使東西比它應該更復雜。但是,我遵循你的建議後,我的折扣價值仍然爲0。我仍然不確定爲什麼。對不起,這是一個小白菜。 – user2875021

+0

我假設你在eclipse中運行這個。你知道如何使用eclipse調試器嗎?它將幫助您輕鬆解決這個問題併爲您提供解決所有未來問題的工具。 https://www.ibm.com/developerworks/library/os-ecbug/ – Michael

+0

不,我實際上只是使用文本編輯器並使用終端進行編譯。我可能應該現在開始使用eclipse。非常感謝您的幫助。 – user2875021