У меня возникли трудности с написанием Java-программы, в частности с помощью метода enterCustomerName. Я использовал String, чтобы я мог получить введенный пользователем ввод. Есть и другие ошибки, но я этого не понимаю. Я новичок в программировании на Java, поэтому любая помощь очень ценится.приватная ошибка метода String, проблемы с получением правильного ввода пользователем
Это программа для магазина фруктов и овощей.
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
{
\t private void printWelcomeMessage()
\t { // print the welcome message
\t System.out.println(" Welcome to Rocky Fruit and Vegetable Market");
\t }
\t private String enterCustomerName()
\t { // prompt for, read and return a customer name
\t \t Scanner in = new Scanner (System.in);
\t \t System.out.println("Enter the name of the customer ==> ");
\t \t String customerName;
\t \t customerName = in.nextLine();
\t return customerName;
\t }
\t private String enterProduceName()
\t { // prompt for, read and return the name of the produce
\t \t Scanner in = new Scanner(System.in);
\t \t System.out.println("Enter the name of the produce ==> ");
\t \t String produceName;
\t \t produceName = in.nextLine();
\t \t return produceName;
\t }
\t private void printPurchaseNumber(int purchaseNumber)
\t { // print the number of the purchase
\t \t int counter = 0;
\t \t while (counter <= 3) // loop the order 3 times
\t
\t }
\t private double enterProduceWeight(String name)
\t { // prompt for, read and return the weight of the produce
\t \t Scanner in = new Scanner (System.in);
\t \t System.out.printf("Enter the weight (kg) of the %s\n ==> ", produceName);
\t \t double produceWeight;
\t \t produceWeight = in.nextDouble();
\t \t return produceWeight;
\t }
\t private double enterProduceCost(String name)
\t { // prompt for, read and return the cost of the produce
\t \t Scanner in = new Scanner (System.in);
\t \t System.out.printf("Enter the cost per kg of the %s\n ==> ", produceName);
\t \t double produceCostPerKg;
\t \t produceCostPerKg = in.nextDouble();
\t \t return produceCostPerKg;
\t }
\t private void printPurchase(String produceName, double produceWeight, double produceCostPerKg, double cost)
\t { // print the details of one pruchase
\t double produceCost;
\t System.out.println("--------------------------------------------------");
\t System.out.printf("%f\n of %s\n at $%.2f\n per kg is $%.2f\n", produceWeight, produceName, produceCostPerKg, produceCost);
\t System.out.println("--------------------------------------------------");
\t produceCost = produceWeight * produceCostPerKg;
\t }
\t private boolean discount(String name)
\t { // determine if the customer is eligle for a discount and return true or false
\t \t Scanner in = new Scanner(System.in);
\t \t if (in.nextLine().equalsIgnoreCase("y"))
\t \t \t return true;
\t \t else
return false;
\t }
\t private void printTotalCost(String name, int numberOfPurchases, double cost, boolean discount)
\t { // print the total cost of the order and discount information if neccessary
\t \t double totalCost;
\t \t double discountValue;
\t \t System.out.println("**********************************************************************");
\t \t System.out.printf("%s\n the total for %d\n fruit and vegetable purchases is $%.2f\n", customerName, purchaseNumber, totalCost);
\t \t System.out.printf("This includes a discount of $%.2d\n", discountValue);
\t \t System.out.println("**********************************************************************");
\t \t // make a addition counter for the order.
\t \t // set a discount value
\t }
\t private void printExitMessage(String name)
\t { // print the exit message
\t \t System.out.printf("Thank you %s\n for shopping at the Rocky Fruit and Vegetable Market", customerName);
\t \t System.out.println("Written by s0270780");
\t }
\t private void processCustomer()
\t { // process one customer's order
\t \t String customerName, \t \t // the name of the customer
\t \t \t \t produceName; \t \t // the name of the produce
\t \t double produceWeight, \t \t // the weight of the produce
\t \t \t \t produceCostPerKg, \t // the cost per kilo of the produce
\t \t \t \t purchaseCost, \t \t // the cost of the purchase
\t \t \t \t totalCost = 0; \t \t // the total cost of the order
\t \t int purchaseNumber = 0; \t \t // the number of the current purchase
\t \t boolean discount; \t \t \t // boolean to say if customer is eligible for a discount
\t \t printWelcomeMessage(); \t \t // print the title message
\t \t customerName = enterCustomerName(); // get the customer name
\t \t purchaseNumber++; \t \t \t \t \t \t \t \t \t // increment the number of the purchase
\t \t printPurchaseNumber(purchaseNumber); \t \t \t \t // print the number of the purchase
\t \t produceName = enterProduceName(); \t \t \t \t \t // get the name of the produce item
\t \t produceWeight = enterProduceWeight(produceName); \t // get the weight of the purchase
\t \t produceCostPerKg = enterProduceCost(produceName); \t // get the cost per kg
\t \t purchaseCost = produceWeight * produceCostPerKg; \t // calculate the cost of the purchase
\t \t totalCost += purchaseCost; \t \t \t \t \t \t \t // add to total cost
\t \t printPurchase(produceName, produceWeight, produceCostPerKg, purchaseCost); // print the purchase details
\t \t discount = discount(customerName); \t // determine if the customer is eligible for a discount
\t \t printTotalCost(customerName, purchaseNumber, totalCost, discount); // print the total cost
\t \t printExitMessage(customerName); \t // print the final exit message
\t }
\t public static void main(String [] args)
\t {
\t \t FruitVegShop fruitVegShop = new FruitVegShop();
\t \t fruitVegShop.processCustomer();
\t }
}
C:\Users\Jockman\Desktop\The Fruit & Veg Shop\FruitVegShop.java:45: error: cannot find symbol
\t \t System.out.printf("Enter the weight (kg) of the %f\n ==> ", produceName);
\t \t ^
symbol: variable produceName
location: class FruitVegShop
C:\Users\Jockman\Desktop\The Fruit & Veg Shop\FruitVegShop.java:55: error: cannot find symbol
\t \t System.out.printf("Enter the cost per kg of the %s\n ==> ", produceName);
\t \t ^
symbol: variable produceName
location: class FruitVegShop
C:\Users\Jockman\Desktop\The Fruit & Veg Shop\FruitVegShop.java:93: error: cannot find symbol
\t \t System.out.printf("%s\n the total for %d\n fruit and vegetable purchases is $%.2f\n", customerName, purchaseNumber, totalCost);
\t \t ^
symbol: variable customerName
location: class FruitVegShop
C:\Users\Jockman\Desktop\The Fruit & Veg Shop\FruitVegShop.java:93: error: cannot find symbol
\t \t System.out.printf("%s\n the total for %d\n fruit and vegetable purchases is $%.2f\n", customerName, purchaseNumber, totalCost);
\t \t ^
symbol: variable purchaseNumber
location: class FruitVegShop
C:\Users\Jockman\Desktop\The Fruit & Veg Shop\FruitVegShop.java:104: error: cannot find symbol
\t \t System.out.printf("Thank you %s\n for shopping at the Rocky Fruit and Vegetable Market", customerName);
\t \t ^
symbol: variable customerName
location: class FruitVegShop
5 errors
Tool completed with exit code 1
Какие ошибки вы получаете точно? И можете ли вы опубликовать стек ошибок? – krisdestruction
Привет, я добавил его к сообщению, и спасибо Эллиоту и Тони, это действительно помогло мне. – Jockman
Я перезапустил код, теперь работаю с третьим кодом в сообщении, и теперь я даже не могу заставить его создать файл .class для него. Я обновил основной код, и журнал ошибок под третьим кодом - это то, что я получаю. – Jockman