2013-11-04 55 views
0

我正在找不到符號錯誤。我正在製作一個可以執行各種數學運算的程序。在這裏,我得到的錯誤:獲取'找不到符號'錯誤

a=Double.parseDouble(in.readLine()); 

的錯誤是:

cannot find symbol- method readLine() 

我給我的整個程序:

// I(Rachit Bhargava) am writing my first program that includes almost every feature of Mathematical Operations. 
import java.io.*; 
import java.lang.*; 
import java.lang.Math; 
public class All_In_One 
{ 
    public static void main(String args[])throws IOException 
    { 
     InputStreamReader in = new InputStreamReader(System.in); 
     BufferedReader read = new BufferedReader(read); 
     double a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, r, s, t, u, v, w, x, y, z, i1, a1, i2, a2; 
     char ch1, ch2; 
     System.out.println("To which topic is your question based upon?"); 
     System.out.println("Enter 'm' for Mathematics and 'g' for Geometry"); 
     System.out.println("Please enter your choice."); 
     ch1= (char)(in.read()); 
     switch (ch1) 
     { 
      case 'm': 
      System.out.println("What operation do you want to do?"); 
      System.out.println("Enter 'a' for addtion, 's' for subtraction, 'm' for multiplication, 'd' for division, 't' for finding out simple interest(with amount), 'c' for finding out compound interest(with amount) and 'b' for finding out both(with amount) and their difference"); 
      System.out.println("Please enter your choice."); 
      ch2= (char)(in.read()); 
      switch (ch2) 
      { 
       case 'a': 
       System.out.println("Please enter first number."); 
       a=Double.parseDouble(in.readLine()); 
       System.out.println("Please enter second number."); 
       b=Double.parseDouble(in.readLine()); 
       c = a+b; 
       System.out.println("Sum = "+c); 
       break; 
       case 's': 
       System.out.println("Please enter first number."); 
       a=Double.parseDouble(in.readLine()); 
       System.out.println("Please enter second number."); 
       b=Double.parseDouble(in.readLine()); 
       c = a-b; 
       System.out.println("Difference = "+c); 
       break; 
       case 'm': 
       System.out.println("Please enter first number."); 
       a=Double.parseDouble(in.readLine()); 
       System.out.println("Please enter second number."); 
       b=Double.parseDouble(in.readLine()); 
       c = a*b; 
       System.out.println("Answer = "+c); 
       break; 
       case 'd': 
       System.out.println("Please enter first number."); 
       a=Double.parseDouble(in.readLine()); 
       System.out.println("Please enter second number."); 
       b=Double.parseDouble(in.readLine()); 
       c = (a>b)? (a/b):(b/a); 
       System.out.println("Answer = "+c); 
       break; 
       case 't': 
       System.out.println("Please enter principal"); 
       p=Double.parseDouble(in.readLine()); 
       System.out.println("Please enter rate of interest."); 
       r=Double.parseDouble(in.readLine()); 
       System.out.println("Please enter time(in years)"); 
       t=Double.parseDouble(in.readLine()); 
       i = p*r*t/100; 
       a = p+i; 
       System.out.println("Simple interest = "+i); 
       System.out.println("Amount = "+a); 
       break; 
       case 'c': 
       System.out.println("Please enter principal"); 
       p=Double.parseDouble(in.readLine()); 
       System.out.println("Please enter rate of interest."); 
       r=Double.parseDouble(in.readLine()); 
       System.out.println("Please enter time(in years)"); 
       t=Double.parseDouble(in.readLine()); 
       a = p*(Math.pow(((100+r)/100), t)); 
       i = a-p; 
       System.out.println("Compound Interest = "+i); 
       System.out.println("Amount = "+a); 
       break; 
       case 'b': 
       System.out.println("Please enter principal"); 
       p=Double.parseDouble(in.readLine()); 
       System.out.println("Please enter rate of interest."); 
       r=Double.parseDouble(in.readLine()); 
       System.out.println("Please enter time(in years)"); 
       t=Double.parseDouble(in.readLine()); 
       i1 = p*r*t/100; 
       a1 = p+i1; 
       a2 = p*(Math.pow(((100+r)/100), t)); 
       i2 = a-p; 
       d = a2-a1; 
       System.out.println("Simple interest = "+i1); 
       System.out.println("Amount(as by simple interest) = "+a1); 
       System.out.println("Compound Interest = "+i2); 
       System.out.println("Amount(as by compound interest) = "+a2); 
       System.out.println("Difference in interests and amount = "+d); 
       break; 
       default: 
       System.out.println("Invalid input :("); 
      } 
      break; 
      case 'g': 
      System.out.println("Work In Progress!"); 
      break; 
      default: 
      System.out.println("Invalid Input :("); 
     } 
     System.out.println("Thank you for using my program"); 
     System.out.println("All credits goes to Rachit Bhargava"); 
    } 
} 

請幫我解決這個問題!

回答

0

你需要做以下2個變化:

  1. InputStreamReader in = new InputStreamReader(System.in); 
    BufferedReader read = new BufferedReader(in); 
    
  2. 使用讀。readLine()而不是in.readLine(); 例如,

    a = Double.parseDouble(read.readLine());

0

正如您在InputStreamReader javadoc中看到的那樣,沒有readLine方法。也許你想使用readLine方法從BufferedReader read變量,因爲BufferedReader這種方法:

b = Double.parseDouble(read.readLine()); 

順便說一句:

  • 這條線:

    BufferedReader read = new BufferedReader(read); 
    

    應:

    BufferedReader read = new BufferedReader(in); 
    

    編譯並不意味着它會按預期工作。

  • 我建議你改變你的變量更有意義的名稱,例如buffReader而不是readfirstNumber代替asecondNumber代替b,等等...

0

那是因爲InputStreamReader沒有readLine()方法。您需要使用BufferedReader

更改代碼:

InputStreamReader in = new InputStreamReader(System.in); 
BufferedReader read = new BufferedReader(read); 

到:

InputStreamReader in = new InputStreamReader(System.in); 
BufferedReader read = new BufferedReader(in); 

然後你就可以做到以下幾點:

a = Double.parseDouble(read.readLine()); 

或者您可以使用Scanner這將允許你做Scanner.nextDouble()

Scanner scanner = new Scanner(System.in); 
double a = scanner.nextDouble();