不能在eclipse(homework)中将变量错误赋予变量

时间:2016-01-26 04:15:42

标签: java eclipse

我正在为学校的一个类的java订购程序构建,我得到的quantityInput无法解析为变量错误我也得到了错误

  

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

解决这两个错误的任何帮助都会有很多不足之处。

/**
 * 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

2 个答案:

答案 0 :(得分: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;
    ...
    }

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

答案 1 :(得分:0)

第二个问题是,JOptionPane只是没有提供任何方法来匹配您尝试在getQuantity中传递的参数 - mehod:

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

您必须选择一种现有方法。