[Java错误]找不到任何名为“number”的内容

时间:2011-01-25 05:29:52

标签: java processing calculator

我正在使用Processing作为框架在Java中创建计算器。我正在编写一个存储数字输入的类,以便以后可以检索它。

//store numbers in memory
class memStorage {
  float storedNum1, storedNum2;

  //constructor
  void Memory(float num1, float num2){
    storedNum1 = num1;
    storedNum2 = num2;
  }

  //Store numbers and call them when needed
  //Store the first number
  void mem1(float num1){
    num1 = number;
    println("number 1 has been stored");
  }

  //Store the second number
  void mem2(float num2){
    num2 = number;
    println("number 2 has been stored");
  }

}

void processNumber(char number){
  //Instantiate memory storage class and execute method
  memStorage storedNum1 = new memStorage();
  storedNum1.mem1();
  //print keypressed numbers
  println(storedNum1);
}

当我在处理中运行草图时,它给出了一个错误,指出: Cannot find anything named "number"

我有点担心我应该做些什么才能让它发挥作用。任何建议都表示赞赏。

4 个答案:

答案 0 :(得分:2)

这里有很多语法问题。你没有一个问题,你有很多。只要修复你问到的那个就会导致大约5到10个。

我会先内联评论它们。

//This should probably be MemStorage. In Java classes start with a capital letter.
//It should also probably be public.
class memStorage {  
  float storedNum1, storedNum2;

  //constructor
  //This isn't a constructor. This is a method. It would be a constructor if it matched the name 
  //of the class AND didn't return a type of "void"
  void Memory(float num1, float num2){
    storedNum1 = num1;
    storedNum2 = num2;
  }

  //Store numbers and call them when needed
  //Store the first number
  void mem1(float num1){
    num1 = number; // The value of number is undeclared. This is the syntax error you ran into.
    // Also note that you didn't store in storedNum1.
    println("number 1 has been stored");
  }

  //Store the second number
  void mem2(float num2){
    num2 = number; // The value of number is undeclared. This is the syntax error you ran into.
    // Also note that you didn't store in storedNum2.
    println("number 2 has been stored");
  }

}

// This method isn't within the body of any class. Methods always belong inside of a class.
// The way you write this method, it looks like it should be the main method of another class
// You are using to hand test the MemStorage class
void processNumber(char number){
  //Instantiate memory storage class and execute method
  memStorage storedNum1 = new memStorage();
  storedNum1.mem1();
  //print keypressed numbers
  println(storedNum1); //This method doesn't exist. You probably mean System.out.println()
  // Furthermore, you didn't override toString(), so it wouldn't print anything meaningful.
}

以下是我如何清理并保留您的意图。

public class MemStorage {
    private float storedNum1;
    private float storedNum2;

    public MemStorage(float num1, float num2){
        this.storedNum1 = num1;
        this.storedNum2 = num2;
    }

    public void setNum1(float num1){
        this.storedNum1 = num1;
        System.out.println("Number 1 has been stored.");
    }

    public void setNum2(float num2){
        this.storedNum2 = num2;
        System.out.println("Number 2 has been stored.");
    }

    public float getNum1(){
        return this.storedNum1;
    }

    public float getNum2(){
        return this.storedNum2;
    }

    // Hand Test
    public static void main(String[] args){
        MemStorage memStorage = new MemStorage(0,0);
        memStorage.setNum1(1.23454f);
        System.out.println(memStorage.getNum1());
    }
}

你真的需要回到基础知识并从初学者教程开始。

答案 1 :(得分:0)

首先,将processNumber函数放在memStorage类中。您可能希望将'number'变量与storedNum1和storedNum2一起转换为实例变量。第三,在类内部创建一个main()函数,然后创建一个memStorage类的实例并调用processNumber()函数。第四,构造函数必须与类名完全相同。将其更改为“memStorage”。

mem1和mem2函数也不会将任何内容存储到实例变量中。函数退出后,'num1'和'num2'变量将消失。将'num1'和'num2'替换为storedNum1或storedNum2。

您需要学习和理解面向对象的范式概念。

答案 2 :(得分:0)

似乎数字是一个局部变量,并且在方法块之外无法看到它。

答案 3 :(得分:0)

如果你仔细看看

 num1 = number;

 num2 = number;

你会看到之前没有数字声明(除非你没有发布相关代码吗?)

我也认为你想做类似

的事情
private number = 0;       
void mem1(float num1){
   number = num1;
   println("number 1 has been stored in private field number");
}

你的代码将number的值复制到一个本地变量num1中,该变量将在退出函数时被销毁:不会存储任何内容。