Проблемы с методом проверки Java - PullRequest
0 голосов
/ 18 октября 2018

РЕДАКТИРОВАТЬ: КОД ОБНОВЛЕНО ДЛЯ ПРАВИЛЬНОГО ФОРМАТА.СПАСИБО ВСЕМ, КТО ПОМОГИЛ МНЕ РЕШИТЬ ЭТО.

Спасибо, что взглянули на этот вопрос.Я учусь на уроке Java-программирования в Community College, и мой профессор загрузил PDF-файл java-программы, которую мы могли бы скопировать и ввести в IDE.Она буквально просто хотела, чтобы мы сами ввели ее в IDE и включили ее на 5 баллов за наш промежуточный срок.

Проблема в том, что я обнаружил пару проблем с ее кодом, которые не позволяют ему работать (или, может быть, я просто не знаю, как он используется должным образом. Я не пытаюсь казаться напыщенным).

В программе есть строка кода, которая хочет проверить тип программы.Это выглядит следующим образом:

         carType = validateCarType(carType);

Когда я пытаюсь запустить программу, она говорит:

CarWash.java:69: error: cannot find symbol

Я набрал ее в первый раз и подумал, что, возможно, набрал ее неправильно,затем я копирую / вставляю из документа PDF.Не повезло.

Как эта команда будет использоваться?Должен ли я объявить что-то?Если бы вы могли показать мне, как он используется и включить объяснение, я был бы очень признателен.В настоящее время я работаю над сайд-проектом с материалом, который я изучаю на Java, который подсчитает налоги, которые мне нужно сэкономить, с моей 1099 независимых контрактных работ, и я хочу понять это на случай, если мне понадобится это использовать.

Я проверил учебник и нигде не видел такого метода проверки.Они были в основном в то время как и для петель.

Очень ценится!

следующий код целиком:

РЕДАКТИРОВАТЬ: следующий код теперь работает благодаря другим членам сообщества, которые помогают мне решить эту проблему.Код ниже ОБНОВЛЕН

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

Ответы [ 2 ]

0 голосов
/ 18 октября 2018

Вы правы, что метод отсутствует в исходном коде.Если бы мне пришлось угадывать, целью этого метода является то, что он должен имитировать предыдущий цикл 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
0 голосов
/ 18 октября 2018

Вы должны добавить метод 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.
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...