여기에는 수많은 구문 문제가 있습니다. 너는 단 한가지 문제 만 가지고 있지 않다. 당신이 물었던 것을 고치는 것만으로도 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());
}
}
정말 기본으로 돌아가서 초보자 안내서로 시작해야합니다.
이미 답변되었지만 코드에 큰 결함이 있습니다. 메서드가'constructor'으로 잘못 언급되었습니다. 메소드가 공개적으로 표시되지 않습니다. 사용자가 '기본'액세스 가능성을 갖기를 바랍니다. 'mem1'과'mem2' 메쏘드에서 클래스/로컬 변수에 할당하기 위해 인수 _of_를 사용하는 대신에 ** value _to_ argument를 할당합니다. ** 마지막으로 모든 사람들이 말했듯이, '액세스 가능하고 여러 메소드에 의해 수정 가능하려면 클래스 변수로 만드십시오. – Nishant
모든 의견과 비판을 읽으려는 시도는 자바를 사용하는 두 번째 프로젝트 일뿐입니다. 마음에 계속 나는 또한 프레임 워크로 처리를 사용하고 있습니다. 모든 사람의 조언을 통해 최선을 다해 배웁니다. 도와 주셔서 감사합니다. – nnash