我在编写Java程序时遇到困难,尤其是使用enterCustomerName方法。我已经使用了String,以便我可以从用户那里获得措辞输入。还有其他错误,但这个我不明白。我是Java编程的新手,所以非常感谢任何帮助。
这是一个水果和蔬菜商店的计划。
import java.util.Scanner;
public class FruitVegShop
{
private void printWelcomeMessage()
{ // print the welcome message
System.out.println("Welcome to Rocky Fruit and Vegetable Market");
}
private String enterCustomerName()
{ // prompt for, read and return a customer name
Scanner in = new Scanner (System.in);
System.out.println("Please enter the customer's name: ");
String customerName;
customerName = in.nextLine();
System.out.printf("Hello %s\n", customerName);
return "";
}
private String enterProduceName()
{ // prompt for, read and return the name of the produce
Scanner in = new Scanner(System.in);
System.out.println("Please enter the name of the produce");
String name;
name = in.nextLine();
System.out.printf("P: %s\n", name);
return "";
}
private void printPurchaseNumber(int purchaseNumber)
{ // print the number of the purchase
}
private double enterProduceWeight(String name)
{ // prompt for, read and return the weight of the produce
Scanner in = new Scanner (System.in);
System.out.println("Please enter the weight of the produce: ");
double weight;
weight = in.nextDouble();
System.out.printf("Produce Weight: %d\n", weight);
return 0;
}
private double enterProduceCost(String name)
{ // prompt for, read and return the cost of the produce
Scanner in = new Scanner (System.in);
System.out.println("Please enter the price of the produce: ");
double costPerKg;
costPerKg = in.nextDouble();
System.out.printf("Produce Price: $%d\n", costPerKg);
return 0;
}
private void printPurchase(String name, double weight, double costPerKg, double cost)
{ // print the details of one pruchase
double produceTotal;
System.out.println("Produce:");
System.out.printf("Produce: %d\n", name);
System.out.printf("Weight (kg): %d\n", weight);
System.out.printf("@ $%d.2\n /kg", costPerKg);
System.out.printf("TOTAL: $%d.2\n", produceTotal); // ERROR: variable produceTotal might not have been initialized
produceTotal = weight * costPerKg;
}
private boolean discount(String name)
{ // determine if the customer is eligle for a discount and return true or false
if(
return scannerObject.nextLine().equalsIgnoreCase(“y”);
}
private void printTotalCost(String name, int numberOfPurchases, double cost, boolean discount)
{ // print the total cost of the order and discount information if neccessary
}
private void printExitMessage(String name)
{ // print the exit message
}
private void processCustomer()
{ // process one customer's order
String customerName, // the name of the customer
produceName; // the name of the produce
double produceWeight, // the weight ofthe produce
produceCostPerKg, // the cost per kilo of the produce
purchaseCost, // the cost of the purchase
totalCost = 0; // the total cost of the order
int purchaseNumber = 0; // the number of the current purchase
boolean discount; // boolean to say if customer is eligible for a discount
printWelcomeMessage(); // print the title message
customerName = enterCustomerName(); // get the customer name
purchaseNumber++; // increment the number of the purchase
printPurchaseNumber(purchaseNumber); // print the number of the purchase
produceName = enterProduceName(); // get the name of the produce item
produceWeight = enterProduceWeight(produceName); // get the weight of the purchase
produceCostPerKg = enterProduceCost(produceName); // get the cost per kg
purchaseCost = produceWeight * produceCostPerKg; // calculate the cost of the purchase
totalCost += purchaseCost; // add to total cost
printPurchase(produceName, produceWeight, produceCostPerKg, purchaseCost); // print the purchase details
discount = discount(customerName); // determine if the customer is eligible for a discount
printTotalCost(customerName, purchaseNumber, totalCost, discount); // print the total cost
printExitMessage(customerName); // print the final exit message
}
public static void main(String [] args)
{
FruitVegShop fruitVegShop = new FruitVegShop();
fruitVegShop.processCustomer();
}
}
这是我得到的错误:
Welcome to Rocky Fruit and Vegetable Market
Please enter the customer's name:
Anonymous
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at FruitVegShop.enterCustomerName(FruitVegShop.java:20)
at FruitVegShop.processCustomer(FruitVegShop.java:102)
at FruitVegShop.main(FruitVegShop.java:127)
Press any key to continue . . .

import java.util.Scanner;
public class FruitVegShop
{
private void printWelcomeMessage()
{ // print the welcome message
System.out.println(" Welcome to Rocky Fruit and Vegetable Market");
}
private String enterCustomerName()
{ // prompt for, read and return a customer name
Scanner in = new Scanner (System.in);
System.out.println("Enter the name of the customer ==> ");
String customerName;
customerName = in.nextLine();
return customerName;
}
private String enterProduceName()
{ // prompt for, read and return the name of the produce
Scanner in = new Scanner(System.in);
System.out.println("Enter the name of the produce ==> ");
String produceName;
produceName = in.nextLine();
return produceName;
}
private void printPurchaseNumber(int purchaseNumber)
{ // print the number of the purchase
int counter = 0;
while ( counter <= 3 ) // loop the order 3 times
}
private double enterProduceWeight(String name)
{ // prompt for, read and return the weight of the produce
Scanner in = new Scanner (System.in);
System.out.printf("Enter the weight (kg) of the %s\n ==> ", produceName);
double produceWeight;
produceWeight = in.nextDouble();
return produceWeight;
}
private double enterProduceCost(String name)
{ // prompt for, read and return the cost of the produce
Scanner in = new Scanner (System.in);
System.out.printf("Enter the cost per kg of the %s\n ==> ", produceName);
double produceCostPerKg;
produceCostPerKg = in.nextDouble();
return produceCostPerKg;
}
private void printPurchase(String produceName, double produceWeight, double produceCostPerKg, double cost)
{ // print the details of one pruchase
double produceCost;
System.out.println("--------------------------------------------------");
System.out.printf("%f\n of %s\n at $%.2f\n per kg is $%.2f\n", produceWeight, produceName, produceCostPerKg, produceCost);
System.out.println("--------------------------------------------------");
produceCost = produceWeight * produceCostPerKg;
}
private boolean discount(String name)
{ // determine if the customer is eligle for a discount and return true or false
Scanner in = new Scanner(System.in);
if (in.nextLine().equalsIgnoreCase("y"))
return true;
else
return false;
}
private void printTotalCost(String name, int numberOfPurchases, double cost, boolean discount)
{ // print the total cost of the order and discount information if neccessary
double totalCost;
double discountValue;
System.out.println("**********************************************************************");
System.out.printf("%s\n the total for %d\n fruit and vegetable purchases is $%.2f\n", customerName, purchaseNumber, totalCost);
System.out.printf("This includes a discount of $%.2d\n", discountValue);
System.out.println("**********************************************************************");
// make a addition counter for the order.
// set a discount value
}
private void printExitMessage(String name)
{ // print the exit message
System.out.printf("Thank you %s\n for shopping at the Rocky Fruit and Vegetable Market", customerName);
System.out.println("Written by s0270780");
}
private void processCustomer()
{ // process one customer's order
String customerName, // the name of the customer
produceName; // the name of the produce
double produceWeight, // the weight of the produce
produceCostPerKg, // the cost per kilo of the produce
purchaseCost, // the cost of the purchase
totalCost = 0; // the total cost of the order
int purchaseNumber = 0; // the number of the current purchase
boolean discount; // boolean to say if customer is eligible for a discount
printWelcomeMessage(); // print the title message
customerName = enterCustomerName(); // get the customer name
purchaseNumber++; // increment the number of the purchase
printPurchaseNumber(purchaseNumber); // print the number of the purchase
produceName = enterProduceName(); // get the name of the produce item
produceWeight = enterProduceWeight(produceName); // get the weight of the purchase
produceCostPerKg = enterProduceCost(produceName); // get the cost per kg
purchaseCost = produceWeight * produceCostPerKg; // calculate the cost of the purchase
totalCost += purchaseCost; // add to total cost
printPurchase(produceName, produceWeight, produceCostPerKg, purchaseCost); // print the purchase details
discount = discount(customerName); // determine if the customer is eligible for a discount
printTotalCost(customerName, purchaseNumber, totalCost, discount); // print the total cost
printExitMessage(customerName); // print the final exit message
}
public static void main(String [] args)
{
FruitVegShop fruitVegShop = new FruitVegShop();
fruitVegShop.processCustomer();
}
}
&#13;
C:\Users\Jockman\Desktop\The Fruit & Veg Shop\FruitVegShop.java:45: error: cannot find symbol
System.out.printf("Enter the weight (kg) of the %f\n ==> ", produceName);
^
symbol: variable produceName
location: class FruitVegShop
C:\Users\Jockman\Desktop\The Fruit & Veg Shop\FruitVegShop.java:55: error: cannot find symbol
System.out.printf("Enter the cost per kg of the %s\n ==> ", produceName);
^
symbol: variable produceName
location: class FruitVegShop
C:\Users\Jockman\Desktop\The Fruit & Veg Shop\FruitVegShop.java:93: error: cannot find symbol
System.out.printf("%s\n the total for %d\n fruit and vegetable purchases is $%.2f\n", customerName, purchaseNumber, totalCost);
^
symbol: variable customerName
location: class FruitVegShop
C:\Users\Jockman\Desktop\The Fruit & Veg Shop\FruitVegShop.java:93: error: cannot find symbol
System.out.printf("%s\n the total for %d\n fruit and vegetable purchases is $%.2f\n", customerName, purchaseNumber, totalCost);
^
symbol: variable purchaseNumber
location: class FruitVegShop
C:\Users\Jockman\Desktop\The Fruit & Veg Shop\FruitVegShop.java:104: error: cannot find symbol
System.out.printf("Thank you %s\n for shopping at the Rocky Fruit and Vegetable Market", customerName);
^
symbol: variable customerName
location: class FruitVegShop
5 errors
Tool completed with exit code 1
&#13;
答案 0 :(得分:2)
我认为您打算在name
和""
更改
enterCustomerName()
而不是enterProduceName()
return "";
到
return customerName;
和
return name;
分别。此外,enterProduceWeight
和enterProduceCost
不应return 0;
,而应return weight;
和return costPerKg;
(如果您希望它们打印为非空而不是0
在printPurchase
)。