Java 如何以及在何处添加要运行的主方法?
因此,我觉得代码已经完成,可以运行了,但是我在基础知识方面遇到了问题,完全忘记了将主要方法放在哪里以及放在其中的内容。 我的类叫做“Cell”,里面有一些方法,现在我想运行它,如果我没有提供足够的细节,很抱歉,希望你们都能理解。 代码:Java 如何以及在何处添加要运行的主方法?,java,main,Java,Main,因此,我觉得代码已经完成,可以运行了,但是我在基础知识方面遇到了问题,完全忘记了将主要方法放在哪里以及放在其中的内容。 我的类叫做“Cell”,里面有一些方法,现在我想运行它,如果我没有提供足够的细节,很抱歉,希望你们都能理解。 代码: 公共类单元格{ //我们需要一个单元格数组和一个规则数组。 公共int[]单元格=新int[9]; 公共int[]规则集={0,1,0,1,1,0,1,0}; //计算下一代。 public void generate() { //除中心单元格的状态为1外,所有
公共类单元格{
//我们需要一个单元格数组和一个规则数组。
公共int[]单元格=新int[9];
公共int[]规则集={0,1,0,1,1,0,1,0};
//计算下一代。
public void generate()
{
//除中心单元格的状态为1外,所有单元格均以状态0开始。
对于(int i=0;i
非常简单
public static void main(String[] args)
{
Cell cell = new Cell();
cell.generate();
}
将其添加到单元格类本身中您可以在类声明中的任何位置编写main方法:
//this is your class declaration for class Cell!
public class Cell {
//We need an array for the cells and one for the rules.
public int[] cells = new int[9];
public int[] ruleset = {0,1,0,1,1,0,1,0};
//You can write main method here!
//Compute the next generation.
public void generate()
{
//code
}
//Or you can write main method here!
//Look up a new state from the ruleset.
public int rules (int a, int b, int c)
{
//code
}
//Or, heck, you can write main method here:
public static void main(String args[])
{
//sample code:
Cell cell = new Cell();
cell.generate();
//loop through updated array list.
for(int i = 0; i<cell.cells.length; i++)
{
System.out.println("cells[" + i + "]: " + cell.cells[i]);
}
System.out.println("main method complete!");
}
}
//这是您对类单元格的类声明!
公共类单元{
//我们需要一个单元格数组和一个规则数组。
公共int[]单元格=新int[9];
公共int[]规则集={0,1,0,1,1,0,1,0};
//你可以在这里写main方法!
//计算下一代。
public void generate()
{
//代码
}
//或者你可以在这里写main方法!
//从规则集中查找新状态。
公共整数规则(整数a、整数b、整数c)
{
//代码
}
//或者,见鬼,您可以在此处编写main方法:
公共静态void main(字符串参数[])
{
//示例代码:
单元格=新单元格();
cell.generate();
//循环浏览更新的数组列表。
对于(int i=0;我阅读了本教程:任何带有公共静态void main(String[]args)
的类都可以。您可以将main方法放入单元格
类本身或为此编写一个专用类。我认为公共int规则(int a、int b、int c)
可以优化将a、b和c分配给规则集索引是互补的二进制数字(a、b、c)。例如,(1,1,0)->(0,0,1)=0*4+0*2+1*1=1,(0,0,0)->(1,1,1)=1*4+1*2+1*1=7。遵循惯例并使用字符串[]改为args
。谢谢您提供的信息。问题是,当我运行主方法代码时,什么都没有发生。是因为Generate()是空的,不能返回任何内容吗?Generate()
内部更新int[]cells
。因此,是的,该方法无效且不返回任何内容。但它有副作用。您应该在调用generate()
之前和之后尝试System.out.println(cell.cells)
。问题是,当我运行主方法代码时,什么都不会发生。是因为generate()是否为空且无法返回任何内容?是的,但我假设您要打印cells[]数组?更新了主方法内的代码。我认为这正是您需要的。哇,这正是我需要的。谢谢!没问题!我建议创建一个方法来打印整个cells数组,这样可以使主方法看起来更干净。
//this is your class declaration for class Cell!
public class Cell {
//We need an array for the cells and one for the rules.
public int[] cells = new int[9];
public int[] ruleset = {0,1,0,1,1,0,1,0};
//You can write main method here!
//Compute the next generation.
public void generate()
{
//code
}
//Or you can write main method here!
//Look up a new state from the ruleset.
public int rules (int a, int b, int c)
{
//code
}
//Or, heck, you can write main method here:
public static void main(String args[])
{
//sample code:
Cell cell = new Cell();
cell.generate();
//loop through updated array list.
for(int i = 0; i<cell.cells.length; i++)
{
System.out.println("cells[" + i + "]: " + cell.cells[i]);
}
System.out.println("main method complete!");
}
}