这里有很多语法问题。你不只有一个问题,你有很多。仅修复您询问的问题将导致大约 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());
}
}
你真的需要回到基础,从初学者教程开始。