【问题标题】:java using methods with a arrayjava使用带有数组的方法
【发布时间】:2012-11-06 21:05:05
【问题描述】:

感谢阅读,我是 Java 新手,我正在尝试创建一个程序。

所以我有一个名为 public class mysystem 的方法,我在其中对数组执行循环

for (int i = 0; i < tax.length; i++)

在 main 方法中,但我总是出错 - 它需要以这种方式设置,因为我将使用该数组两次计算一个值,第二次使用另一个值

我在eclipse中遇到的错误是

Exception in thread "main" java.lang.Error: Unresolved compilation problems:

tax cannot be resolved to a variable
tax cannot be resolved to a variable
tax cannot be resolved to a variable
tax cannot be resolved to a variable
tax cannot be resolved to a variable
tax cannot be resolved to a variable

at mysystem.taxAlone(mysystem.java:11)
at mysystem.main(mysystem.java:83)

根据要求提供完整代码

 import java.util.Scanner;


 public class taxsystem{
            public static void taxAlone(double inputwage){
                    for (int i = 0; i < tax.length; i++)
                            {
                                    // Check what tax bracket the value falls in to
                                    if ((inputwage >= tax[i][0]) && (inputwage <= tax[i][1]))
                                            {
                                                    // Declare the tax variable
                                                    double taxValue;
                                                    // do the calculation - amount taxable * taxrate + Cumulative value
                                                    taxValue = ( ( (inputwage - tax[i][0]) * tax[i][2]) + tax[i][3]);
                                                    //rounding up or down!
                                                    int finalTax = (int)Math.round(taxValue);
                                                    //Print out the result!

                                                    System.out.println("You will be taxed £ "+ finalTax);
                                            }

                            }
            }

            public static void main(String[] args)
                    {

                            double[][] tax = new double[6][4];
                            // First Tax Band
                            tax[0][0] = 0;
                            tax[0][1] = 100;
                            tax[0][2] = 0;
                            tax[0][3] = 0; // 0 to start
                            // Second Tax Band
                            tax[1][0] = 101;
                            tax[1][1] = 150;
                            tax[1][2] = 0.1;
                            tax[1][3] = 0; // 0* 100 = 0
                            // Third Tax Band
                            tax[2][0] = 151;
                            tax[2][1] = 200;
                            tax[2][2] = 0.2;
                            tax[2][3] = 4.9; // 100 * 0 + 49 * 0.1 = 4.9
                            // Fourth Tax Band
                            tax[3][0] = 201;
                            tax[3][1] = 300;
                            tax[3][2] = 0.4;
                            tax[3][3] = 14.7;
                            // Fifth Tax Band
                            tax[4][0] = 301;
                            tax[4][1] = 400;
                            tax[4][2] = 0.6;
                            tax[4][3] = 54.3;
                            // Sixth Tax Band
                            tax[5][0] = 401;
                            tax[5][1] = 10000; // Dummy Value - Program is only assumed to
                                                                 // takes values up to 1000 pounds
                            tax[5][2] = 1.2;
                            tax[5][3] = 113.7;

                            // Display instructions and ask for value
                            System.out.println("Please enter the income earned to calculate tax");

                            Scanner read = new Scanner(System.in);

                            double wage = read.nextDouble();

                            taxAlone(wage);



                    }
    }

【问题讨论】:

  • 显示完整的方法(或)至少方法语法。
  • 你的方法签名是什么样的?你在哪里定义了tax?粘贴更多代码可能很好。
  • 向我们展示您的纳税申报单。 :)
  • 请展示产生您的问题的完整代码示例。还包括完整的错误消息。如需建议,请查看here。

标签: java loops methods for-loop


【解决方案1】:

taxAlone 方法对 tax 变量不可见。可以通过以下两种方式之一解决:

  1. 将 tax 变量添加为静态变量,例如private static double[][] tax 并仅使用该副本
  2. 将 tax 变量作为参数添加到 taxAlone 方法,例如public static void taxAlone(double[][] tax, double inputwage) 并确保将构造的对象作为参数传递给方法。

【讨论】:

    【解决方案2】:

    您正在尝试在 taxAlone() 方法中使用名为 tax 的变量。但是,名为tax 的变量的唯一声明是在main() 中。 taxAlone() 对 main() 内部的变量一无所知。您很可能需要将 tax 变量作为参数从 main 传递给 taxAlone()。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-17
      • 1970-01-01
      • 2013-07-17
      • 1970-01-01
      • 2012-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多