0

我正在使用 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

4 回答 4

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());
    }
}

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

于 2011-01-25T05:54:35.747 回答
0

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

mem1 和 mem2 函数也不向实例变量存储任何内容。一旦函数退出,“num1”和“num2”变量将消失。将“num1”和“num2”替换为storedNum1 或storedNum2。

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

于 2011-01-25T05:35:42.703 回答
0

看起来 number 是一个局部变量,在它的方法块之外是看不到的。

于 2011-01-25T05:36:15.020 回答
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 中,该变量将在退出函数时被销毁:不会存储任何内容。

于 2011-01-25T05:36:23.827 回答