为了账号安全,请及时绑定邮箱和手机立即绑定

Java 验证方法的问题

Java 验证方法的问题

萧十郎 2021-11-24 16:04:56
编辑:代码已更新为正确格式。感谢所有帮助我解决这个问题的人。感谢您查看这个问题。我在社区学院的 Java 编程课上,我的教授上传了一个 Java 程序的 .pdf 文档,供我们复制并输入到 IDE 中。她实际上只是希望我们自己将它输入到 IDE 中,并在我们的期中考试中将其交上 5 分。问题是,我发现她的代码存在一些问题,无法运行(或者我可能只是不知道如何正确使用它。我不想显得浮夸)。程序中有一行代码要验证程序的carType。如下:         carType = validateCarType(carType);当我尝试运行该程序时,它说:CarWash.java:69: error: cannot find symbol我第一次在自己输入时认为可能是我输入错误,然后我从 PDF 文档中复制/粘贴。没运气。该命令将如何使用?我应该声明什么吗?如果你能告诉我它是如何使用的并包括解释,我将不胜感激。我目前正在从事一个附带项目,其中包含我从 Java 中学到的东西,该项目将计算我需要从 1099 独立承包工作中节省的税款,我想了解这一点,以防万一我需要使用它。我确实检查了我的教科书,但在任何地方都没有看到这种验证方法。它们主要是 while 和 for 循环。非常感激!
查看完整描述

2 回答

?
PIPIONE

TA贡献1829条经验 获得超9个赞

您必须在类中添加 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.


查看完整回答
反对 回复 2021-11-24
?
波斯汪

TA贡献1811条经验 获得超4个赞

您是正确的,源代码中缺少该方法。如果我不得不猜测,该方法的意图是它应该模仿前面的 while 循环,该循环反复询问washType,直到输入有效值。


在上述假设下,您可以自己实现该方法,正如其他一些人已经建议的那样,或者您可以只注释掉该行,它仍然应该运行。carType 变量传递给 setAdderPrice 方法,如果传递的类型无效,该方法将默认为 SUV 的值。


编辑: 我看到您已经将方法签名更新为具有 char 的返回类型并使您的循环正常工作。我要提出的唯一额外建议是将您现有的扫描仪传递给该函数,而不是创建一个新的扫描仪。


//validation method for carType

 carType = validateCarType(carType, keyboard);


//validateCarType method validates the car type as either S or V

public static char validateCarType(char myCarType, Scanner scanner) 

{


  while (myCarType != 'S' && myCarType != 'V')

  {


     System.out.println("Invalid data.");

     System.out.println("Please enter S for Sedan or V for SUV");

     myCarType = scanner.nextLine().charAt(0);

     myCarType = Character.toUpperCase(myCarType);


  }


  return myCarType;


}//end of validateCarType


查看完整回答
反对 回复 2021-11-24
  • 2 回答
  • 0 关注
  • 137 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信