编辑:该代码已更新为正确的格式。谢谢所有帮助我解决此问题的人。
感谢您查看此问题。我在社区学院的Java编程课程中,我的教授上传了Java程序的.pdf文档,供我们复制并输入到IDE中。实际上,她只是想让我们自己将其输入到IDE中,并在我们的期中考试中额外获得5分。
问题是,我发现她的代码有几个问题使其无法运行(或者也许我只是不知道它的用法正确。我并不想显得浮夸)。
程序中有一行代码要验证程序的carType。如下:
carType = validateCarType(carType);
当我尝试运行该程序时,它说:
CarWash.java:69: error: cannot find symbol
我是第一次自己输入此文件,并以为可能输入错了,然后从PDF文档复制/粘贴了。没有运气。
该命令将如何使用?我应该宣布什么吗?如果您能告诉我它的用法并提供解释,我将不胜感激。我目前正在使用我从Java中学习到的东西进行一个副项目,该工作将计算从1099年独立承包工作中节省下来的税款,如果要使用它,我想了解这一点。
我确实检查了我的课本,但在任何地方都没有看到这种验证方法。它们主要是while和for循环。
非常感谢!
以下是整个代码:
编辑:感谢社区成员的帮助,以下代码现在可以正常工作。以下代码已更新
import java.util.Scanner;
public class CarWash
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
String name = " ";
String runProgram = " ";
int washType = 0;
char carType = ' ';
char extras = ' ';
double basicPrice = 0.0;
double adder = 0.0;
double extraPrice = 0.0;
double totalPrice = 0.0;
final double SHINE_PRICE = 4.95;
final double MAT_PRICE = 8.95;
final double CWAX = 7.95;
System.out.println("Welcome to the Car Wsh");
System.out.println("Enter Yes to start the program or No to quit.");
runProgram = keyboard.nextLine();
runProgram = runProgram.toLowerCase();
while (runProgram.equals("yes"))
{
//Getting user input
System.out.println("Please enter your name");
name = keyboard.nextLine();
System.out.println("Please choose the type of car wash:");
System.out.println("1. Pleasant Colony - sedan $34.95 SUV $35.95");
System.out.println("2. Secretariat - sedan $24.95 SUV $25.95");
System.out.println("3. Gallant Fox - sedan $19.95 SUV $20.95");
System.out.println("4. Pony Express - sedan $14.95 SUV $15.95");
System.out.println("5. Win - $12.95");
System.out.println("6. Show - $8.95");
washType = keyboard.nextInt();
keyboard.nextLine();
//Input validation loop for washType
while (washType < 1 || washType > 6) //this works
//while (washType != 1 && washType !=2 && washType !=3 && washType !=4 && washType !=5 && washType != 6)//This works
{
System.out.println("Invalid data.");
System.out.println("Please enter a value from 1 to 6.");
washType = keyboard.nextInt();
keyboard.nextLine();
}//end washType while
System.out.println("Please enter a S for Sedan or V for SUV.");
carType = keyboard.nextLine().charAt(0);
carType = Character.toUpperCase(carType);
//validation method for carType
carType = validateCarType(carType);
//The above code did not work at all. I had to create my own validator for this.
//below presents 2 different menus to the user for extras
if (washType == 1 || washType == 2)
{
System.out.println("Please choose the extras:");
System.out.println("A. No Extras $0.00");
System.out.println("B. Mat Shampoo $8.95");
System.out.println("C. Carnauba Wax $7.95");
System.out.println("D. Both Mat Shampoo and Carnauba Wax $16.90"); //On the BB document, you put E, so I changed it to D
}//end washType if
else
{
System.out.println("Please choose the extras:");
System.out.println("A. No Extras $0.00");
System.out.println("B. Mat Shampoo $8.95");
System.out.println("C. Carnauba Wax $7.95");
System.out.println("D. Tire Shine $4.95");
System.out.println("E. Both Mat Shampoo and Carnauba Wax $16.90");
System.out.println("F. Both Mat Shampoo and tire Shine $13.90");
System.out.println("G. Both Carnauba Wax and Tire Shine $12.90");
System.out.println("H. All: Mat Shampoo and Carnauba Wax and Tire Shine $21.85");
}//end else
extras = keyboard.nextLine().charAt(0);
extras = Character.toUpperCase(extras);
//Validation loop for extras
while (extras != 'A' && extras != 'B' && extras != 'C' && extras != 'D' && extras != 'E' && extras != 'F' && extras != 'G' && extras != 'H') //This works
{
System.out.println("Invalid data.");
System.out.println("Please enter either A, B, C, D, E, F, G, or H.");
extras = keyboard.nextLine().charAt(0);
extras = Character.toUpperCase(extras);
}//end Invalid extras while
//determines basicPrice based on washType
basicPrice = setBasicPrice(washType);
//determines adder based on carType
adder = setAdderPrice(carType);
//determines extraPrice based on extras
switch (extras)
{
case 'A':
extraPrice = 0.0;
break;
case 'B':
extraPrice = MAT_PRICE; //extraPrice = 8.95;
break;
case 'C':
extraPrice = CWAX; //extraPrice = 7.95;
break;
case 'D':
extraPrice = SHINE_PRICE;
break;
case 'E':
extraPrice = 16.90;
break;
case 'F':
extraPrice = 13.90;
break;
case 'G':
extraPrice = 12.90;
break;
case 'H':
extraPrice = 21.85;
break;
default:
extraPrice = 0.0;
break;
}//end extras switch
//method to calculate totalPrice
totalPrice = calcTotalPrice(basicPrice, adder, extraPrice);
//method to print a horizontal line of characters
printLineOfChars('*', 60);
//method to display results
displayResults(name, washType, carType, basicPrice, adder, extraPrice, totalPrice);
//method to print a horizontal line of characters
printLineOfChars('*', 60);
//give the user a chance to run the program again or quit
System.out.println("Please enter Yes to run the program again or No to quit.");
runProgram = keyboard.nextLine();
runProgram = runProgram.toLowerCase();
}//end runProgram while
System.out.println("Thanks for using the Car Wash Program.");
}//end main
//calctotalPrice method calculates the total price
public static double calcTotalPrice(double myBasicPrice, double myAdder, double myExtraPrice)
{
double myTotalPrice = 0.0;
myTotalPrice = myBasicPrice + myAdder + myExtraPrice;
return myTotalPrice;
}//end double calcTotalPrice() method
//printLineOfChars method prints a horizozntal line of chars
public static void printLineOfChars(char myCharacter, int myLoopCounter)
{
for (int i = 0; i <= myLoopCounter; i++)
{
System.out.print(myCharacter);
}//end for
System.out.println();
}//end printLineOfChars()
//validateCarType method validates the car type as either S or V
public static char validateCarType(char myCarType)
{
Scanner keyboard = new Scanner(System.in);
while (myCarType != 'S' && myCarType != 'V')
{
System.out.println("Invalid data.");
System.out.println("Please enter S for Sedan or V for SUV");
myCarType = keyboard.nextLine().charAt(0);
myCarType = Character.toUpperCase(myCarType);
}
return myCarType;
}//end of validateCarType
public static void displayResults(String myName, int myWashType, char myCarType, double myBasicPrice, double myAdder, double myExtraPrice, double myTotalPrice)
{
//display results
System.out.printf("%-35s%10s\n", "Customer Name", myName);
System.out.printf("%-35s%10s\n", "Car Wash Chosen", myWashType);
System.out.printf("%-35s%10s\n", "Car Type", myCarType);
System.out.printf("%-35s%10.2f\n", "Basic Price: ", myBasicPrice);
System.out.printf("%-35s%10.2f\n", "Adder: ", myAdder);
System.out.printf("%-35s%10.2f\n", "Extras: ", myExtraPrice);
System.out.printf("%-35s%10.2f\n", "Total Price: ", myTotalPrice);
}//end displayresults() method
//setAdderPrice method calculates the adder
public static double setAdderPrice(char myCarType)
{
double myAdder = 0.0;
if (myCarType == 'S')
{
myAdder = 0.00;
}//end if
else
{
myAdder = 1.00;
}//end else
return myAdder;
}//end setAdderPrice() method
//setBasicPrice method sets the basic price based on washType
public static double setBasicPrice(int myWashType)
{
double myBasicPrice = 0.0;
switch (myWashType)
{
case 1:
myBasicPrice = 34.95;
break;
case 2:
myBasicPrice = 24.95;
break;
case 3:
myBasicPrice = 19.95;
break;
case 4:
myBasicPrice = 14.95;
break;
case 5:
myBasicPrice = 12.95;
break;
case 6:
myBasicPrice = 8.95;
break;
default:
myBasicPrice = 0.0;
}//end Swtich (myWashType)
return myBasicPrice;
}//end setBasicPrice() method
}//end class
答案 0 :(得分:0)
您必须在类中添加validateCarType方法。您的课程中没有定义任何方法。将此方法放在您的课程中。
private static char validateCarType(char carType) {
//here is your code
return '0';
}
存在一些拼写错误。更改
double extraPricec = 0.0;
到
double extraPrice = 0.0;
并在'G'格后面加上一个空格。
case 'G':
extraPrice = 12.90;
break;// that was missing
,当您再次调用函数拼写错误时 更改
printLineofChars('*', 60);//O is in uppercase
到
printLineOfChars('*', 60);
下面的代码在我的IDE中可以很好地工作:
import java.util.Scanner;
public class CarWash {
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
String name = " ";
String runProgram = " ";
int washType = 0;
char carType = ' ';
char extras = ' ';
double basicPrice = 0.0;
double adder = 0.0;
double extraPrice = 0.0;
double totalPrice = 0.0;
final double SHINE_PRICE = 4.95;
final double MAT_PRICE = 8.95;
final double CWAX = 7.95;
System.out.println("Welcome to the Car Wsh");
System.out.println("Enter Yes to start the program or No to quit.");
runProgram = keyboard.nextLine();
runProgram = runProgram.toLowerCase();
while (runProgram.equals("yes"))
{
//Getting user input
System.out.println("Please enter your name");
name = keyboard.nextLine();
System.out.println("Please choose the type of car wash:");
System.out.println("1. Pleasant Colony - sedan $34.95 SUV $35.95");
System.out.println("2. Secretariat - sedan $24.95 SUV $25.95");
System.out.println("3. Gallant Fox - sedan $19.95 SUV $20.95");
System.out.println("4. Pony Express - sedan $14.95 SUV $15.95");
System.out.println("5. Win - $12.95");
System.out.println("6. Show - $8.95");
washType = keyboard.nextInt();
keyboard.nextLine();
//Input validation loop for washType
while (washType < 1 || washType > 6) //this works
//while (washType != 1 && washType !=2 && washType !=3 && washType !=4 && washType !=5 && washType != 6)//This works
{
System.out.println("Invalid data.");
System.out.println("Please enter a value from 1 to 6.");
washType = keyboard.nextInt();
keyboard.nextLine();
}//end washType while
System.out.println("Please enter a S for Sedan or V for SUV.");
carType = keyboard.nextLine().charAt(0);
carType = Character.toUpperCase(carType);
//validation method for carType
validateCarType(carType);
//below presents 2 different menus to the user for extras
if (washType == 1 || washType == 2)
{
System.out.println("Please choose the extras:");
System.out.println("A. No Extras $0.00");
System.out.println("B. Mat Shampoo $8.95");
System.out.println("C. Carnauba Wax $7.95");
System.out.println("D. Both Mat Shampoo and Carnauba Wax $16.90"); //On the BB document, you put E, so I changed it to D
}//end washType if
else
{
System.out.println("Please choose the extras:");
System.out.println("A. No Extras $0.00");
System.out.println("B. Mat Shampoo $8.95");
System.out.println("C. Carnauba Wax $7.95");
System.out.println("D. Tire Shine $4.95");
System.out.println("E. Both Mat Shampoo and Carnauba Wax $16.90");
System.out.println("F. Both Mat Shampoo and tire Shine $13.90");
System.out.println("G. Both Carnauba Wax and Tire Shine $12.90");
System.out.println("H. All: Mat Shampoo and Carnauba Wax and Tire Shine $21.85");
}//end else
extras = keyboard.nextLine().charAt(0);
extras = Character.toUpperCase(extras);
//Validation loop for extras
while (extras != 'A' && extras != 'B' && extras != 'C' && extras != 'D' && extras != 'E' && extras != 'F' && extras != 'G' && extras != 'H') //This works
{
System.out.println("Invalid data.");
System.out.println("Please enter either A, B, C, D, E, F, G, or H.");
extras = keyboard.nextLine().charAt(0);
extras = Character.toUpperCase(extras);
}//end Invalid extras while
//determines basicPrice based on washType
basicPrice = setBasicPrice(washType);
//determines adder based on carType
adder = setAdderPrice(carType);
//determines extraPrice based on extras
switch (extras)
{
case 'A':
extraPrice = 0.0;
break;
case 'B':
extraPrice = MAT_PRICE; //extraPrice = 8.95;
break;
case 'C':
extraPrice = CWAX; //extraPrice = 7.95;
break;
case 'D':
extraPrice = SHINE_PRICE;
break;
case 'E':
extraPrice = 16.90;
break;
case 'F':
extraPrice = 13.90;
break;
case 'G':
extraPrice = 12.90;
case 'H':
extraPrice = 21.85;
break;
default:
extraPrice = 0.0;
break;
}//end extras switch
//method to calculate totalPrice
totalPrice = calcTotalPrice(basicPrice, adder, extraPrice);
//method to print a horizontal line of characters
printLineOfChars('*', 60);
//method to display results
displayResults(name, washType, carType, basicPrice, adder, extraPrice, totalPrice);
//method to print a horizontal line of characters
printLineOfChars('*', 60);
//give the user a chance to run the program again or quit
System.out.println("Please enter Yes to run the program again or No to quit.");
runProgram = keyboard.nextLine();
runProgram = runProgram.toLowerCase();
}//end runProgram while
System.out.println("Thanks for using the Car Wash Program.");
}//end main
private static void validateCarType(char carType) {
}
//calctotalPrice method calculates the total price
public static double calcTotalPrice(double myBasicPrice, double myAdder, double myExtraPrice)
{
double myTotalPrice = 0.0;
myTotalPrice = myBasicPrice + myAdder + myExtraPrice;
return myTotalPrice;
}//end double calcTotalPrice() method
//printLineOfChars method prints a horizozntal line of chars
public static void printLineOfChars(char myCharacter, int myLoopCounter)
{
for (int i = 0; i <= myLoopCounter; i++)
{
System.out.print(myCharacter);
}//end for
System.out.println();
}//end printLineOfChars()
public static void displayResults(String myName, int myWashType, char myCarType, double myBasicPrice, double myAdder, double myExtraPrice, double myTotalPrice)
{
//display results
System.out.printf("%-35s%10s\n", "Customer Name", myName);
System.out.printf("%-35s%10s\n", "Car Wash Chosen", myWashType);
System.out.printf("%-35s%10s\n", "Car Type", myCarType);
System.out.printf("%-35s%10.2f\n", "Basic Price: ", myBasicPrice);
System.out.printf("%-35s%10.2f\n", "Adder: ", myAdder);
System.out.printf("%-35s%10.2f\n", "Extras: ", myExtraPrice);
System.out.printf("%-35s%10.2f\n", "Total Price: ", myTotalPrice);
}//end displayresults() method
//setAdderPrice method calculates the adder
public static double setAdderPrice(char myCarType)
{
double myAdder = 0.0;
if (myCarType == 'S')
{
myAdder = 0.00;
}//end if
else
{
myAdder = 1.00;
}//end else
return myAdder;
}//end setAdderPrice() method
//setBasicPrice method sets the basic price based on washType
public static double setBasicPrice(int myWashType)
{
double myBasicPrice = 0.0;
switch (myWashType)
{
case 1:
myBasicPrice = 34.95;
break;
case 2:
myBasicPrice = 24.95;
break;
case 3:
myBasicPrice = 19.95;
break;
case 4:
myBasicPrice = 14.95;
break;
case 5:
myBasicPrice = 12.95;
break;
case 6:
myBasicPrice = 8.95;
break;
default:
myBasicPrice = 0.0;
}//end Swtich (myWashType)
return myBasicPrice;
}//end setBasicPrice() method
}
这是我运行此程序时的输出:
Welcome to the Car Wsh
Enter Yes to start the program or No to quit.
yes
Please enter your name
khalid
Please choose the type of car wash:
1. Pleasant Colony - sedan $34.95 SUV $35.95
2. Secretariat - sedan $24.95 SUV $25.95
3. Gallant Fox - sedan $19.95 SUV $20.95
4. Pony Express - sedan $14.95 SUV $15.95
5. Win - $12.95
6. Show - $8.95
1
Please enter a S for Sedan or V for SUV.
S
Please choose the extras:
A. No Extras $0.00
B. Mat Shampoo $8.95
C. Carnauba Wax $7.95
D. Both Mat Shampoo and Carnauba Wax $16.90
A
*************************************************************
Customer Name khalid
Car Wash Chosen 1
Car Type S
Basic Price: 34.95
Adder: 0.00
Extras: 0.00
Total Price: 34.95
*************************************************************
Please enter Yes to run the program again or No to quit.
答案 1 :(得分:0)
您正确的认为源代码中缺少该方法。如果我不得不猜测,则该方法的目的是它应该模仿前面的while循环,该循环反复询问washType,直到输入有效值为止。
在上述假设下,您可以自己实现该方法,就像其他人已经建议的那样,或者您可以注释掉该行,它仍然可以运行。 carType变量传递给setAdderPrice方法,如果传递了无效的类型,它将默认为SUV的值。
编辑: 我看到您已经更新了方法签名,使其具有char的返回类型,并使循环正常工作。我唯一要提出的建议是将现有的Scanner传递给该功能,而不是创建一个新功能。
import math
def round_up(x):
return math.floor(x + 0.5)
和
//validation method for carType
carType = validateCarType(carType, keyboard);