获取"找不到符号"尝试使用复杂公式时出错

时间:2015-10-01 05:24:59

标签: java

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

public class VoltCap {
    public static final double e = 2.71828;

    public void volt_Cap_accept() {
        // called from acceptselection().
        // Here you accept the various values to calculate the voltage
        // a do/while loop. The main exit character here is 'e'/'E'

        Scanner stdin = new Scanner(System.in);
        int B;
        int initialR;
        int finalR;
        double C;
        String exit;

        System.out.println("Voltage values across a capacitor");
        System.out.println();

        System.out.print("Please enter Battery voltage (integer) (0 for Quick exit): ");
        B = stdin.nextInt();

        System.out.print("Please enter initial resistance value (integer) from: ");
        initialR = stdin.nextInt();

        System.out.print("Please enter final resistance value (integer) to: ");
        finalR = stdin.nextInt();

        System.out.print("Please enter capacitor value (MUST be between 0.2 and 0.8 inclusive): ");
        C = stdin.nextDouble();

        VoltCap v = new VoltCap();
        v.voltCapCalc(B, initialR, finalR, C);      
    } // volt_Cap_accept closing bracket

    public void voltCapCalc(int b, int iR, int fR, double c) {
        // here you calculate the results, from the values you passed to it from volt_Cap_accept.
        // You will need to use a nested for loop

        DecimalFormat twoDecimal = new DecimalFormat("#,###,#00.00");
        double v;       

        System.out.println("=====================================================================");
        System.out.print("\t");
        for (iR = iR; iR <= fR; iR += 100) {
            System.out.print(iR + "\t");
            for (int t = 0; t <= 500; t += 10) {
                System.out.println(v(t) = b(1 - Math.pow(e, -t)/(iR*c)));
            }
        }
    } // voltCapCalc closing bracket
}

v(t)= B(1 - e ^ -t /(RC))

v(t) = b(1 - Math.pow(e, -t)/(iR*c)) //My attempt at converting it into code

我对Java非常陌生,我尝试使用上面的公式进行分配但是我一直在找&#34;找不到符号&#34; v b 的错误消息。我在这里完全失败,有人可以帮忙吗?

错误消息:

 ----jGRASP exec: javac -g VoltCap.java

 VoltCap.java:60: error: cannot find symbol
             System.out.println(v(t) = b(1 - Math.pow(e, -t)/(iR*c)));
                                ^
   symbol:   method v(int)
   location: class VoltCap
 VoltCap.java:60: error: cannot find symbol
             System.out.println(v(t) = b(1 - Math.pow(e, -t)/(iR*c)));
                                       ^
   symbol:   method b(double)
   location: class VoltCap
 2 errors

  ----jGRASP wedge2: exit code for process is 1.
  ----jGRASP: operation complete.

3 个答案:

答案 0 :(得分:2)

仔细阅读错误输出。符号v(t)告诉Java您要调用名为v()的方法,并将t作为参数传入。此方法不存在,您也没有使用此表达式的评估,因此您可以从print语句中删除它。您还在调用b(),它再次告诉Java您要调用名为b()的方法。这个方法不存在,看起来你真的希望乘以 b来表达其余部分。尝试使用此代码:

System.out.println(b*(1 - Math.pow(e, -t)/(iR*c)));

此外,您可以使用e类中提供的VoltCap常量,而不是在e类中定义常量java.lang.Math

import java.lang.Math;
// ...
System.out.println(b*(1 - Math.pow(Math.E, -t)/(iR*c)));

答案 1 :(得分:1)

因为在voltCapCalc变量v(voltCapCalc的对象)不可见,因为它们在类级别不是静态的,它们也没有名为v(t)和b()的方法,这会产生编译错误你的班级。

答案 2 :(得分:0)

下面的行是错误的

System.out.println(v(t) = b(1 - Math.pow(e, -t)/(iR*c)));

您可以按如下方式修改

System.out.println(v*(t) + " = " + b*(1 - Math.pow(e, -t)/(iR*c)));