gpt4 book ai didi

Java 代码编辑帮助

转载 作者:行者123 更新时间:2023-12-02 08:34:52 25 4
gpt4 key购买 nike

好吧,我有一个内存系统包...我有类内存、MemEl 和测试...我需要一些帮助来编辑我的代码...我无法弄清楚它出了什么问题。因此,如果您能给我一些指示或帮助我编辑我的代码,这将非常有帮助...谢谢...还有我的 MemEl 我正在尝试为 MemEl(Int)、MemEl(long) 和 MemEl(long) 制作一个构造函数MemEl(String)...我已经完成了 MemEl()、MemEl(byte) 和 MemEl(short)...我可以像处理 Short 和 byte 一样处理 Int 和 Long 吗?

现在,当我尝试编译 Memory 或 MemEl 时,出现错误

javac MemEl.java
MemEl.java:9:缺少返回语句
}
^
MemEl.java:13:缺少返回语句
}
^
MemEl.java:17:缺少返回语句
}
^

那么我该如何解决这个问题呢?我所做的就是尝试创建将整数转换为字节、将整数转换为短裤等的构造函数,所以我对这个错误有点困惑。

这是我的 Memory.java 代码

class Memory{

//Here are my instance variables, MemEl[] which is an array of memory elements and I h\
ave my interger size, which is the amount of memory locations available.//

private MemEl[] memArray;
private int size;

//Here are my Constructors for my Memory system.//

public Memory(int s)
{size = s;
memArray = new MemEl[s];
for(int i = 0; i < s; i++)
memArray[i] = new MemEl();
}
public void write (int loc,int val)
{if (loc >=0 && loc < size)
memArray[loc].write(val);
else
System.out.println("Index Not In Domain");
}
public MemEl read (int loc)
{return memArray[loc];
}
public void dump()
{
for(int i = 0; i < size; i++)
if(i%1 == 0)
System.out.println(memArray[i].read());
else
System.out.print(memArray[i].read());

}
}

Here is my MemEl file code


class MemEl{

//MemEl is a memory element with all the characteristics of memory. It can write some val\
to some address and read some address and return the "elements" or value that are located\
at that memory location.//

private int elements;
public Memory MemEl()
{
elements = 0;
}
public Memory MemEl(byte b)
{
elements = b;
}
public Memory MemEl(short s)
{
elements = s;
}
public int read()
{
return elements;
}
public void write(int val)
{
elements = val;
}

}




Here is my code for Test

class Test{
public static void main(String[] args)
{

int size = 100;
Memory mymem;
mymem = new Memory(size);
mymem.write(98,44);
mymem.write(96,7);
MemEl x;
x = mymem.read(98);
System.out.println(mymem);
mymem.dump();
}
}

最佳答案

javac Memory.java
./MemEl.java:6: missing method body, or declare abstract
public Memory MemEl();

您不小心在方法的实际代码(也称为主体)之前结束了该方法。在第 6 行,删除末尾的分号。你的方法是

public Memory MemEl();
{
elements = 0;
}

但应该是

public Memory MemEl()
{
elements = 0;
}

关于Java 代码编辑帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2196206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com