2015-04-19 138 views
0

我的程序必須詢問電池的數量並顯示消息疼痛的數量。爲此,我必須通過變量quantity = getQuantity,以便我可以顯示選定的電池數量。問題是,當我這樣做時,它要求用戶輸入兩次「你想購買多少電池」。我正在學習和學生,所以請提供幫助,以提供更好的理解。我不只是想修復我想要了解的代碼。我只是添加了數量和其他幾個選項,但似乎都沒有顯示選擇的電池數量,只提問一次。如何在不調用方法的情況下使用變量

import javax.swing.JOptionPane; 
import java.io.*; 


/** 
* @author Arnie 
*/ 
public class VapeSolutions2 { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 

     // declare variables 
     String openingMsg, nameInputMsg, customerName, nameOutputMsg, 
       returnInputMsg, customerReturn, returnOutputMsg, 
       greetingOutputMsg, outputMsg, colorSelection, colorInputMsg,   ColorOutputMsg, priceOutputMsg, 
       batteryOutputMsg; 
     int quantity; 
     double grandTotal; 



     // display opening message 
     openingMsg = "*** Welcome to Vape Ordering Solutions ***\n" 
        + "      It's a great day to order a Vape Supplies!"; 
     JOptionPane.showMessageDialog(null, openingMsg); 

     // get required input using dialogs 
     nameInputMsg = "Please enter your name: "; 
     customerName = getStringInput(nameInputMsg); 
     returnInputMsg = "Are you a returning customer (yes or no)? "; 
     customerReturn = getStringInput(returnInputMsg); 
     colorInputMsg = "What Color would you like?"; 
     colorSelection = getStringInput(colorInputMsg); 
     grandTotal  = totalCost(); 
     quantity  = getQuantity(); 


     // build output strings 
     nameOutputMsg  = "Welcome " + customerName + ".\n\n"; 
     returnOutputMsg = "Your return customer status is " + customerReturn + ".\n"; 
     greetingOutputMsg = "Thank you for ordering from Vape Ordering Solutions!" + "\n\n" 
          + "Your order will be shipped the following day" + ".\n"; 
     ColorOutputMsg = "Your Color selected is " + colorSelection + ".\n"; 
     batteryOutputMsg = "Total Batteries Ordered is" + quantity + ".\n"; 
     priceOutputMsg = "Your total purchase price is $" + grandTotal + "\n"; 













     // create and display output string 
     outputMsg = nameOutputMsg + returnOutputMsg + greetingOutputMsg + ColorOutputMsg + batteryOutputMsg 
       + priceOutputMsg; 
     JOptionPane.showMessageDialog(null, outputMsg); 

     System.exit(0); 
    } // end main() 

    private static String getStringInput(String prompt) { 
     String input; 

     input= JOptionPane.showInputDialog(prompt); 
     int i = 0; 

     while (input.length() == 0 && i < 3){ 
      JOptionPane.showInputDialog("Please enter a valid value\n"+ prompt); 
      i++; 
      if (i == 3){ 
       JOptionPane.showMessageDialog(null,"You have exceeded the maximum attempts to input correct data"); 
       System.exit(0); 
      } 
     } 

     return input; 
     } 


     private static int getQuantity(){ 
      int quantity; 
      String quantityMsg = "How many batteries would you like to order?"; 
      String quant = getStringInput (quantityMsg); 
      quantity = Integer.parseInt(quant); 
      return quantity; 

     } 



     // total = grandTotal (quantity, 5,.07) 
     private static double totalCost() { 
      int number; 
      double cost, salesTaxRate, tax, grandTotal, subTotal; 


      cost = (20.00); 
      number = getQuantity(); 
      salesTaxRate = (.07); 
      subTotal = number * cost; 
      tax = subTotal * salesTaxRate; 
      grandTotal = subTotal + tax; 
      return grandTotal; 

     } 

} // end class VapeSolutions2 
+0

讓你'TOTALCOST()'方法接受的數量作爲方法參數而不是它的調用'getQuantity()'來獲取它。 –

回答

0

嘗試:

quantity  = getQuantity(); 
grandTotal  = totalCost(quantity); 

和更新方法 「TOTALCOST」:

// total = grandTotal (quantity, 5,.07) 
private static double totalCost(int quantity) { 
//code ... 
number = quantity; 
//code ... 
} 
相關問題