-->

Error: cannot find symbol symbol: variable A

2019-08-28 23:30发布

问题:

I am trying to create a sort of pay slip to print in the console but I receive the following errors from my code. Its really just one error many times, but I don't think I have enough knowledge of java to fix it on my own.

import java.io.*;
import java.text.*;
import java.util.Scanner;

class Assign2num2 
{
  public static void main (String [] args)
  {
   String famname, firstname;
   double gross, deduct, tax, pay, hrs, ovtm;
   char letter, yn;
   Scanner in = new Scanner(System.in);
   System.out.println("Please enter your family name and first name.");
   famname = in.nextLine();
   firstname = in.nextLine();
System.out.println("Please enter how much you are paid per hour and the amount of hours you worked this week. Enter overtime hours seperately please.");
pay = in.nextInt();
 if (pay<0) {
  System.out.println("You must enter a positive number or zero for your hourly pay.");
    pay = in.nextInt();
 }
  else {
    pay = pay;
  }
hrs = in.nextInt();
 if (hrs<0||hrs>40) {
  System.out.println("You must enter a positive number less than or equal to 40, or zero for the amount of hours worked this week.");
    hrs = in.nextInt();
 }
  else {
    hrs = hrs;
  }
ovtm = in.nextInt();
 if (ovtm<0) {
  System.out.println("You must enter a positive number or zero for your overtime hours worked.");
    ovtm = in.nextInt();
 }
  else {
    ovtm = ovtm;
  }
   gross = (pay*hrs) + ((pay*2)*ovtm);
System.out.println("Please enter a letter indicating your tax category: A is no tax deduction, B is 10% of gross pay, \nC is 20% of gross pay, D is 29% of gross pay, E is 35% of gross pay");
letter = in.next().charAt(0);
 if (letter==A||letter==a){
  letter = letter;
  gross = gross;
 }
  else if (letter==B||letter==b){
  letter = letter;
  deduct = (gross*0.1);
  tax = gross - deduct;  
 }
  else if (letter==C||letter==c){
  letter = letter;
  deduct = (gross*0.2);
  tax = gross - deduct;
 }
  else if (letter==D||letter==d){
  letter = letter;
  deduct = (gross*0.29);
  tax = gross - deduct;
 }
  else if (letter==E||letter==e){
  letter = letter;
  deduct = (gross*0.35);
  tax = gross - deduct;
 }
  else {
    System.out.println("You must enter a letter between A and E.");
     letter = in.next().charAt(0);
 }

System.out.println("If you want $20.00 deducted from your weekly pay as a contribution to the \nUnited Way Charity, enter Y for yes. If not, enter N for no.");
yn = in.next().charAt(0);
if (letter==Y||letter==y||letter==N||letter==n){
  yn = yn;
 }
  else {
    System.out.println("You must enter either Y or N.");
    yn = in.next().charAt(0);
 }
  if (yn==Y||yn==y) {
    deduct = deduct + 20;
    tax = tax - 20;
  }
   else {
    deduct = deduct;
    tax = tax;
  System.out.println("PAY SLIP");
  System.out.println("---------");
  System.out.println(famname + ", " + firstname);
  System.out.println("---------");
  System.out.println("Gross Pay: " + gross);
  System.out.println("---------");
  System.out.println("Deductions: " + deduct);
  System.out.println("---------");
  System.out.println("Net Pay: " + tax);
  }
 }
}

16 errors found:
File: G:\Computer Science\Dr Java Exercises\Java Files\Assign2num2.java  [line: 44]
Error: cannot find symbol
symbol:   variable A
location: class Assign2num2
File: G:\Computer Science\Dr Java Exercises\Java Files\Assign2num2.java  [line: 44]
Error: cannot find symbol
symbol:   variable a
location: class Assign2num2
File: G:\Computer Science\Dr Java Exercises\Java Files\Assign2num2.java  [line: 48]
Error: cannot find symbol
symbol:   variable B
location: class Assign2num2
File: G:\Computer Science\Dr Java Exercises\Java Files\Assign2num2.java  [line: 48]
Error: cannot find symbol
symbol:   variable b
location: class Assign2num2
File: G:\Computer Science\Dr Java Exercises\Java Files\Assign2num2.java  [line: 53]
Error: cannot find symbol
symbol:   variable C
location: class Assign2num2
File: G:\Computer Science\Dr Java Exercises\Java Files\Assign2num2.java  [line: 53]
Error: cannot find symbol
symbol:   variable c
location: class Assign2num2
File: G:\Computer Science\Dr Java Exercises\Java Files\Assign2num2.java  [line: 58]
Error: cannot find symbol
symbol:   variable D
location: class Assign2num2
File: G:\Computer Science\Dr Java Exercises\Java Files\Assign2num2.java  [line: 58]
Error: cannot find symbol
symbol:   variable d
location: class Assign2num2
File: G:\Computer Science\Dr Java Exercises\Java Files\Assign2num2.java  [line: 63]
Error: cannot find symbol
symbol:   variable E
location: class Assign2num2
File: G:\Computer Science\Dr Java Exercises\Java Files\Assign2num2.java  [line: 63]
Error: cannot find symbol
symbol:   variable e
location: class Assign2num2
File: G:\Computer Science\Dr Java Exercises\Java Files\Assign2num2.java  [line: 75]
Error: cannot find symbol
symbol:   variable Y
location: class Assign2num2
File: G:\Computer Science\Dr Java Exercises\Java Files\Assign2num2.java  [line: 75]
Error: cannot find symbol
symbol:   variable y
location: class Assign2num2
File: G:\Computer Science\Dr Java Exercises\Java Files\Assign2num2.java  [line: 75]
Error: cannot find symbol
symbol:   variable N
location: class Assign2num2
File: G:\Computer Science\Dr Java Exercises\Java Files\Assign2num2.java  [line: 75] 
Error: cannot find symbol
symbol:   variable n
location: class Assign2num2
File: G:\Computer Science\Dr Java Exercises\Java Files\Assign2num2.java  [line: 82]
Error: cannot find symbol
symbol:   variable Y
location: class Assign2num2
File: G:\Computer Science\Dr Java Exercises\Java Files\Assign2num2.java  [line: 82]
Error: cannot find symbol
symbol:   variable y
location: class Assign2num2

回答1:

When you compare variables to an expected character, the character must be in single quotes, else the compiler thinks that it's another variable, which isn't defined.

E.g. change

if (letter==A||letter==a){

to

if (letter=='A'||letter=='a'){


回答2:

you need to use ' '

if (letter == 'A')

char literals have to be sorrounded by ' ' and string literals have to be surrounded by " "



回答3:

Use letter=='D', and likewise. Char literals are enclosed in single quotes.