我正在建立一个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
“无法解析为变量”表示变量未被声明。 –
**您在哪里声明quantityInput变量? –
我现在看到我没有声明它,以便修复一个错误,但我仍然无法修复错误,类型为JOptionPane的方法showInputDialog(Component,Object,Object)不适用于参数(String,int,int) –