猿问

Java 验证方法的问题

编辑:代码已更新为正确格式。感谢所有帮助我解决这个问题的人。

感谢您查看这个问题。我在社区学院的 Java 编程课上,我的教授上传了一个 Java 程序的 .pdf 文档,供我们复制并输入到 IDE 中。她实际上只是希望我们自己将它输入到 IDE 中,并在我们的期中考试中将其交上 5 分。

问题是,我发现她的代码存在一些问题,无法运行(或者我可能只是不知道如何正确使用它。我不想显得浮夸)。

程序中有一行代码要验证程序的carType。如下:

         carType = validateCarType(carType);

当我尝试运行该程序时,它说:

CarWash.java:69: error: cannot find symbol

我第一次在自己输入时认为可能是我输入错误,然后我从 PDF 文档中复制/粘贴。没运气。

该命令将如何使用?我应该声明什么吗?如果你能告诉我它是如何使用的并包括解释,我将不胜感激。我目前正在从事一个附带项目,其中包含我从 Java 中学到的东西,该项目将计算我需要从 1099 独立承包工作中节省的税款,我想了解这一点,以防万一我需要使用它。

我确实检查了我的教科书,但在任何地方都没有看到这种验证方法。它们主要是 while 和 for 循环。

非常感激!


萧十郎
浏览 138回答 2
2回答

PIPIONE

您必须在类中添加 validateCarType 方法。您的类中没有定义任何方法。把这个方法放在你的班级里。&nbsp;private static char validateCarType(char carType) {&nbsp; &nbsp; //here is your code&nbsp; &nbsp; return '0';&nbsp; }有一些拼写错误。改变double extraPricec = 0.0;到double extraPrice = 0.0;并在“G”案例后添加一个中断。case 'G':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;extraPrice = 12.90;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;// that was missing当你再次调用函数拼写错误时printLineofChars('*', 60);//O is in uppercase到printLineOfChars('*', 60);下面的代码在我的 IDE 中运行良好:import java.util.Scanner;public class CarWash {&nbsp; public static void main(String[] args)&nbsp; {&nbsp; &nbsp; Scanner keyboard = new Scanner(System.in);&nbsp; &nbsp; String name = " ";&nbsp; &nbsp; String runProgram = " ";&nbsp; &nbsp; int washType = 0;&nbsp; &nbsp; char carType = ' ';&nbsp; &nbsp; char extras = ' ';&nbsp; &nbsp; double basicPrice = 0.0;&nbsp; &nbsp; double adder = 0.0;&nbsp; &nbsp; double extraPrice = 0.0;&nbsp; &nbsp; double totalPrice = 0.0;&nbsp; &nbsp; final double SHINE_PRICE = 4.95;&nbsp; &nbsp; final double MAT_PRICE = 8.95;&nbsp; &nbsp; final double CWAX = 7.95;&nbsp; &nbsp; System.out.println("Welcome to the Car Wsh");&nbsp; &nbsp; System.out.println("Enter Yes to start the program or No to quit.");&nbsp; &nbsp; runProgram = keyboard.nextLine();&nbsp; &nbsp; runProgram = runProgram.toLowerCase();&nbsp; &nbsp; while (runProgram.equals("yes"))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; //Getting user input&nbsp; &nbsp; &nbsp; System.out.println("Please enter your name");&nbsp; &nbsp; &nbsp; name = keyboard.nextLine();&nbsp; &nbsp; &nbsp; System.out.println("Please choose the type of car wash:");&nbsp; &nbsp; &nbsp; System.out.println("1.&nbsp; Pleasant Colony - sedan $34.95 SUV $35.95");&nbsp; &nbsp; &nbsp; System.out.println("2.&nbsp; Secretariat - sedan $24.95 SUV $25.95");&nbsp; &nbsp; &nbsp; System.out.println("3.&nbsp; Gallant Fox - sedan $19.95 SUV $20.95");&nbsp; &nbsp; &nbsp; System.out.println("4.&nbsp; Pony Express - sedan $14.95 SUV $15.95");&nbsp; &nbsp; &nbsp; System.out.println("5.&nbsp; Win - $12.95");&nbsp; &nbsp; &nbsp; System.out.println("6.&nbsp; Show - $8.95");&nbsp; &nbsp; &nbsp; washType = keyboard.nextInt();&nbsp; &nbsp; &nbsp; keyboard.nextLine();&nbsp; &nbsp; &nbsp; //Input validation loop for washType&nbsp; &nbsp; &nbsp; while (washType < 1 || washType > 6) //this works&nbsp; &nbsp; &nbsp; //while (washType != 1 && washType !=2 && washType !=3 && washType !=4 && washType !=5 && washType != 6)//This works&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Invalid data.");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Please enter a value from 1 to 6.");&nbsp; &nbsp; &nbsp; &nbsp; washType = keyboard.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; keyboard.nextLine();&nbsp; &nbsp; &nbsp; }//end washType while&nbsp; &nbsp; &nbsp; System.out.println("Please enter a S for Sedan or V for SUV.");&nbsp; &nbsp; &nbsp; carType = keyboard.nextLine().charAt(0);&nbsp; &nbsp; &nbsp; carType = Character.toUpperCase(carType);&nbsp; &nbsp; &nbsp; //validation method for carType&nbsp; &nbsp; &nbsp; validateCarType(carType);&nbsp; &nbsp; &nbsp; //below presents 2 different menus to the user for extras&nbsp; &nbsp; &nbsp; if (washType == 1 || washType == 2)&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Please choose the extras:");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("A.&nbsp; No Extras $0.00");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("B.&nbsp; Mat Shampoo $8.95");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("C.&nbsp; Carnauba Wax $7.95");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("D.&nbsp; Both Mat Shampoo and Carnauba Wax $16.90"); //On the BB document, you put E, so I changed it to D&nbsp; &nbsp; &nbsp; }//end washType if&nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Please choose the extras:");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("A.&nbsp; No Extras $0.00");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("B.&nbsp; Mat Shampoo $8.95");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("C.&nbsp; Carnauba Wax $7.95");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("D.&nbsp; Tire Shine $4.95");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("E.&nbsp; Both Mat Shampoo and Carnauba Wax $16.90");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("F.&nbsp; Both Mat Shampoo and tire Shine $13.90");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("G.&nbsp; Both Carnauba Wax and Tire Shine $12.90");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("H.&nbsp; All: Mat Shampoo and Carnauba Wax and Tire Shine $21.85");&nbsp; &nbsp; &nbsp; }//end else&nbsp; &nbsp; &nbsp; extras = keyboard.nextLine().charAt(0);&nbsp; &nbsp; &nbsp; extras = Character.toUpperCase(extras);&nbsp; &nbsp; &nbsp; //Validation loop for extras&nbsp; &nbsp; &nbsp; while (extras != 'A' && extras != 'B' && extras != 'C' && extras != 'D' && extras != 'E' && extras != 'F' && extras != 'G' && extras != 'H') //This works&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Invalid data.");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Please enter either A, B, C, D, E, F, G, or H.");&nbsp; &nbsp; &nbsp; &nbsp; extras = keyboard.nextLine().charAt(0);&nbsp; &nbsp; &nbsp; &nbsp; extras = Character.toUpperCase(extras);&nbsp; &nbsp; &nbsp; }//end Invalid extras while&nbsp; &nbsp; &nbsp; //determines basicPrice based on washType&nbsp; &nbsp; &nbsp; basicPrice = setBasicPrice(washType);&nbsp; &nbsp; &nbsp; //determines adder based on carType&nbsp; &nbsp; &nbsp; adder = setAdderPrice(carType);&nbsp; &nbsp; &nbsp; //determines extraPrice based on extras&nbsp; &nbsp; &nbsp; switch (extras)&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; case 'A':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; extraPrice = 0.0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case 'B':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; extraPrice = MAT_PRICE; //extraPrice = 8.95;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case 'C':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; extraPrice = CWAX; //extraPrice = 7.95;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case 'D':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; extraPrice = SHINE_PRICE;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case 'E':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; extraPrice = 16.90;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case 'F':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; extraPrice = 13.90;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case 'G':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; extraPrice = 12.90;&nbsp; &nbsp; &nbsp; &nbsp; case 'H':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; extraPrice = 21.85;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; extraPrice = 0.0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; }//end extras switch&nbsp; &nbsp; &nbsp; //method to calculate totalPrice&nbsp; &nbsp; &nbsp; totalPrice = calcTotalPrice(basicPrice, adder, extraPrice);&nbsp; &nbsp; &nbsp; //method to print a horizontal line of characters&nbsp; &nbsp; &nbsp; printLineOfChars('*', 60);&nbsp; &nbsp; &nbsp; //method to display results&nbsp; &nbsp; &nbsp; displayResults(name, washType, carType, basicPrice, adder, extraPrice, totalPrice);&nbsp; &nbsp; &nbsp; //method to print a horizontal line of characters&nbsp; &nbsp; &nbsp; printLineOfChars('*', 60);&nbsp; &nbsp; &nbsp; //give the user a chance to run the program again or quit&nbsp; &nbsp; &nbsp; System.out.println("Please enter Yes to run the program again or No to quit.");&nbsp; &nbsp; &nbsp; runProgram = keyboard.nextLine();&nbsp; &nbsp; &nbsp; runProgram = runProgram.toLowerCase();&nbsp; &nbsp; }//end runProgram while&nbsp; &nbsp; System.out.println("Thanks for using the Car Wash Program.");&nbsp; }//end main&nbsp; private static void validateCarType(char carType) {&nbsp; }&nbsp; //calctotalPrice method calculates the total price&nbsp; public static double calcTotalPrice(double myBasicPrice, double myAdder, double myExtraPrice)&nbsp; {&nbsp; &nbsp; double myTotalPrice = 0.0;&nbsp; &nbsp; myTotalPrice = myBasicPrice + myAdder + myExtraPrice;&nbsp; &nbsp; return myTotalPrice;&nbsp; }//end double calcTotalPrice() method&nbsp; //printLineOfChars method prints a horizozntal line of chars&nbsp; public static void printLineOfChars(char myCharacter, int myLoopCounter)&nbsp; {&nbsp; &nbsp; for (int i = 0; i <= myLoopCounter; i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; System.out.print(myCharacter);&nbsp; &nbsp; }//end for&nbsp; &nbsp; System.out.println();&nbsp; }//end printLineOfChars()&nbsp; public static void displayResults(String myName, int myWashType, char myCarType, double myBasicPrice, double myAdder, double myExtraPrice, double myTotalPrice)&nbsp; {&nbsp; &nbsp; //display results&nbsp; &nbsp; System.out.printf("%-35s%10s\n", "Customer Name", myName);&nbsp; &nbsp; System.out.printf("%-35s%10s\n", "Car Wash Chosen", myWashType);&nbsp; &nbsp; System.out.printf("%-35s%10s\n", "Car Type", myCarType);&nbsp; &nbsp; System.out.printf("%-35s%10.2f\n", "Basic Price: ", myBasicPrice);&nbsp; &nbsp; System.out.printf("%-35s%10.2f\n", "Adder: ", myAdder);&nbsp; &nbsp; System.out.printf("%-35s%10.2f\n", "Extras: ", myExtraPrice);&nbsp; &nbsp; System.out.printf("%-35s%10.2f\n", "Total Price: ", myTotalPrice);&nbsp; }//end displayresults() method&nbsp; //setAdderPrice method calculates the adder&nbsp; public static double setAdderPrice(char myCarType)&nbsp; {&nbsp; &nbsp; double myAdder = 0.0;&nbsp; &nbsp; if (myCarType == 'S')&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; myAdder = 0.00;&nbsp; &nbsp; }//end if&nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; myAdder = 1.00;&nbsp; &nbsp; }//end else&nbsp; &nbsp; return myAdder;&nbsp; }//end setAdderPrice() method&nbsp; //setBasicPrice method sets the basic price based on washType&nbsp; public static double setBasicPrice(int myWashType)&nbsp; {&nbsp; &nbsp; double myBasicPrice = 0.0;&nbsp; &nbsp; switch (myWashType)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; case 1:&nbsp; &nbsp; &nbsp; &nbsp; myBasicPrice = 34.95;&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; case 2:&nbsp; &nbsp; &nbsp; &nbsp; myBasicPrice = 24.95;&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; case 3:&nbsp; &nbsp; &nbsp; &nbsp; myBasicPrice = 19.95;&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; case 4:&nbsp; &nbsp; &nbsp; &nbsp; myBasicPrice = 14.95;&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; case 5:&nbsp; &nbsp; &nbsp; &nbsp; myBasicPrice = 12.95;&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; case 6:&nbsp; &nbsp; &nbsp; &nbsp; myBasicPrice = 8.95;&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; myBasicPrice = 0.0;&nbsp; &nbsp; }//end Swtich (myWashType)&nbsp; &nbsp; return myBasicPrice;&nbsp; }//end setBasicPrice() method}这是我运行这个程序时的输出:Welcome to the Car WshEnter Yes to start the program or No to quit.yesPlease enter your namekhalidPlease choose the type of car wash:1.&nbsp; Pleasant Colony - sedan $34.95 SUV $35.952.&nbsp; Secretariat - sedan $24.95 SUV $25.953.&nbsp; Gallant Fox - sedan $19.95 SUV $20.954.&nbsp; Pony Express - sedan $14.95 SUV $15.955.&nbsp; Win - $12.956.&nbsp; Show - $8.951Please enter a S for Sedan or V for SUV.SPlease choose the extras:A.&nbsp; No Extras $0.00B.&nbsp; Mat Shampoo $8.95C.&nbsp; Carnauba Wax $7.95D.&nbsp; Both Mat Shampoo and Carnauba Wax $16.90A*************************************************************Customer Name&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; khalidCar Wash Chosen&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1Car Type&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SBasic Price:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 34.95Adder:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0.00Extras:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0.00Total Price:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 34.95*************************************************************Please enter Yes to run the program again or No to quit.

波斯汪

您是正确的,源代码中缺少该方法。如果我不得不猜测,该方法的意图是它应该模仿前面的 while 循环,该循环反复询问washType,直到输入有效值。在上述假设下,您可以自己实现该方法,正如其他一些人已经建议的那样,或者您可以只注释掉该行,它仍然应该运行。carType 变量传递给 setAdderPrice 方法,如果传递的类型无效,该方法将默认为 SUV 的值。编辑: 我看到您已经将方法签名更新为具有 char 的返回类型并使您的循环正常工作。我要提出的唯一额外建议是将您现有的扫描仪传递给该函数,而不是创建一个新的扫描仪。//validation method for carType&nbsp;carType = validateCarType(carType, keyboard);和//validateCarType method validates the car type as either S or Vpublic static char validateCarType(char myCarType, Scanner scanner)&nbsp;{&nbsp; while (myCarType != 'S' && myCarType != 'V')&nbsp; {&nbsp; &nbsp; &nbsp;System.out.println("Invalid data.");&nbsp; &nbsp; &nbsp;System.out.println("Please enter S for Sedan or V for SUV");&nbsp; &nbsp; &nbsp;myCarType = scanner.nextLine().charAt(0);&nbsp; &nbsp; &nbsp;myCarType = Character.toUpperCase(myCarType);&nbsp; }&nbsp; return myCarType;}//end of validateCarType
随时随地看视频慕课网APP

相关分类

Java
我要回答