gpt4 book ai didi

java - 私有(private)字符串方法错误,获取正确用户输入的问题

转载 作者:行者123 更新时间:2023-12-02 04:45:04 27 4
gpt4 key购买 nike

我在编写 Java 程序时遇到困难,特别是在使用 EnterCustomerName 方法时。我使用了字符串,以便我可以从用户那里获取文字输入。还有其他错误,但这个我不明白。我是 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();
}
}

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

最佳答案

我认为您打算在 enterCustomerName()enterProduceName() 中返回 name 而不是 "" > 改变

return "";

return customerName;

return name;

分别。此外,enterProduceWeightenterProduceCost 不应 return 0;,但应 return Weight;return costPerKg; (如果您希望它们在 printPurchase 中打印为非空且不是 0)。

关于java - 私有(private)字符串方法错误,获取正确用户输入的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29725742/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com