2016-01-26 46 views
-3

我正在建立一个java的订购程序在学校的班,我越来越quantityInput不能解析为变量错误我也得到错误不能被赋予一个变量错误在eclipse(作业)

方法showInputDialog(Component, Object, Object)在类型JOptionPane不适用于参数(String, int, int)

在解决2个错误的任何帮助将非常aprecated。

/** 
* Course: IT110 - Introduction to Programming 
* Filename: KagesKreationsPhase1.java 
* Created: 04/09/10 by Dr. Debby Telfer 
* Modified: 11/26/13 by Dr. Bary W Pollack 
* 
* Purpose: Created a simple online ordering system 
*   for Pizzas-R-Us customers 
*/ 

import javax.swing.JOptionPane; 
import java.io.*; 
import java.util.Scanner; 
/** 
* @author bary 
*/ 
public class KagesKreations { 

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

     // declare variables 
     String openingMsg, nameInputMsg, customerName, nameOutputMsg, getReturning, getColor, getQuantity, 
       returnInputMsg, customerReturn, returnOutputMsg, quantityMsg, quantityOutputMsg, totalOutputMsg, 
       colorChoiceMsg, colorChoice, colorChoiceOutputMsg, greetingOutputMsg, outputMsg, quantityInput, getname; 

     int number = 0; 
     double cost = 10.00; 
     double taxRate = 1.07; 
     double total; 


     try { 

      // display opening message 
      openingMsg = "*** Welcome to Kage's Kreations Online Ordering System ***\n" 
        + "      Lets Pick A Kustiom Kreation!"; 
      JOptionPane.showMessageDialog(null, openingMsg); 

      // get required input using dialogs 
      customerName = getName(); 
      customerReturn = getReturning(); 
      colorChoice = getColor(); 
      quantityInput = getQuantity(); 
      number = Integer.parseInt(quantityInput); 

      KagesKreations.totalCost(number, cost, taxRate); 

      total = totalCost(number, cost, taxRate); 

      writeOrderFile(customerName, customerReturn, colorChoice,quantityInput, total); 
      confirmation(); 

     } 
     catch (Exception e) { 
      JOptionPane.showMessageDialog(null, e.getMessage()); 
      System.exit(1); 
     } 

    } // end main() 
    public static String getStringInput(String prompt) throws Exception { 
     String value; 
     int i = 0; 
     do { 

      value = JOptionPane.showInputDialog(prompt);i++; 

      if (value == null) { 
       throw new Exception("Cancle was pressed. Closeing the program."); 

      } 
     } while (value.equals("") && i < 3); 
      if (value.equals("")) { 
       throw new Exception("No input vale was entered after three attempts."); 

      } 
      return value; 
    } 


    public static int getQuantity(int lowValue, int highValue) throws Exception { 
     // quantity must be between 1-99 
     int quantity; 
     int counter = 0; 
     int quantityInput; 

     do { 
      quantityInput = Integer.parseInt(JOptionPane.showInputDialog("How many bracelets would you like to order? (1-99)", 1, 99)); 
      counter = counter + 1; 
      } while (quantityInput < lowValue || quantityInput > highValue && counter < 3); 
     if (quantityInput < lowValue || quantityInput > highValue) { 
      throw new Exception("Invalid responce please enter a number between 1 and 99"); 

     } 


     quantity = quantityInput; 
     return quantity; 
    } 

    public static String getColor() throws Exception { 
    String color; 
    int counter = 0; 
    do { 
     color = JOptionPane.showInputDialog("Please choose Brown or Black for the color of your bracelet"); 
     counter = counter + 1; 
     } while (!color.equals("Brown") && !color.equals("Black") && counter < 3); 

     if (!color.equals("Brown") && !color.equals("Black")) { 
      throw new Exception("Invalid responce please enter Brown or Black."); 
      } 
     return color; 
    } 

    public static String getReturning() throws Exception { 
     String returning; 
     int counter = 0; 
     do { 
      returning = JOptionPane.showInputDialog("Are you a returning customer? (Yes or No)"); 
      counter = counter +1; 

      } while (!returning.equals("Yes") && !returning.equals("No") && counter < 3); 

     if (!returning.equals("Yes") && !returning.equals("No")) { 
      throw new Exception("Invalid responce please enter Yes or No."); 
      } 

     return returning; 
    } 

    public static String getName() throws Exception { 
     String name; 
     int counter = 0; 

     do { 
      name = JOptionPane.showInputDialog("Please Enter your name."); 
      counter = counter + 1; 
     } while (counter < 3); 
    return name; 

    } 

    public static double totalCost(int number, double cost, double taxRate){ 
     double total = 0; 
     total = (number * cost) * taxRate; 

     return total; 
    } 

    public static void writeOrderFile(String name, String returning, String color, String quantity, double total) throws Exception { 
     File order = new File("order.tx"); 
     PrintWriter pw = new PrintWriter(order); 
     pw.println(name); 
     pw.println(returning); 
     pw.println(color); 
     pw.println(quantity); 
     pw.println(total); 
     pw.close(); 
    } 

    public static void confirmation() throws Exception{ 
     String nameOutputMsg, customerName, returnOutputMsg, customerReturn, colorChoiceOutputMsg, colorChoice, quantityOutputMsg, quantityInput, 
       totalOutputMsg, total, greetingOutputMsg, outputMsg; 

     FileReader fr = new FileReader("order.txt"); 
     BufferedReader bf = new BufferedReader(fr); 
     customerName = bf.readLine(); 
     customerReturn = bf.readLine(); 
     colorChoice = bf.readLine(); 
     quantityInput = bf.readLine(); 
     total = bf.readLine(); 

     fr.close(); 
     // build output strings 
        nameOutputMsg  = "Welcome " + customerName + ".\n\n"; 
        returnOutputMsg = "Your return customer status is " + customerReturn + ".\n"; 
        colorChoiceOutputMsg = "You have chosen " + colorChoice + " as the color for your braclet.\n"; 
        quantityOutputMsg = "You have ordered " + quantityInput + " bracelets.\n"; 
        totalOutputMsg = "Your total cost is $" + total + ".\n"; 
        greetingOutputMsg = "Thank you for visiting Kage's Kreations!" + "\n\n" 
            + "Your order should ships in 24 to 48 hours.\n"; 


        // create and display output string 
        outputMsg = nameOutputMsg + returnOutputMsg + colorChoiceOutputMsg + quantityOutputMsg + totalOutputMsg + greetingOutputMsg; 
        JOptionPane.showMessageDialog(null, outputMsg); 

    } 

} // end class KagesKreationsPhase1 
+0

“无法解析为变量”表示变量未被声明。 –

+0

**您在哪里声明quantityInput变量? –

+0

我现在看到我没有声明它,以便修复一个错误,但我仍然无法修复错误,类型为JOptionPane的方法showInputDialog(Component,Object,Object)不适用于参数(String,int,int) –

回答

0

第二prolem是JOptionPane只是不提供您尝试在getQuantity -mehod来传递参数匹配任何方法:

quantityInput = Integer.parseInt(JOptionPane.showInputDialog("How many bracelets would you like to order? (1-99)", 1, 99)); 

你必须选择现有的方法之一。

0

你应该在使用它之前声明一个变量。您可以在getQuantity()或类级别内声明变量(请详细了解本地变量和类变量)。您可以将其声明为您声明数量和计数器变量的方式。

public static int getQuantity(int lowValue, int highValue) throws Exception { 
     // quantity must be between 1-99 
     int quantity; 
     int counter = 0; 
     int quantityInput = 0; 
    ... 
    } 

在你的代码中,我无法找到一个你正在使用showInputDialog()和String,int,int参数的地方。请分享您尝试的完整代码。

+0

而不是JOptionPane.showInputDialog(“您想订购多少手镯?(1-99)”,1,99),请使用“JOptionPane.showInputDialog(”您会有多少手镯喜欢订购?手链的数量应该在1-99之间)“)”因为你要求输入参数。 JOption showInputDialog()中没有方法,它需要String,int,int。您可以参考[1]并在“方法摘要”区域中搜索showInputDialog以查找可以与showInputDialog()一起使用的参数 – Hasanthi

相关问题