在java程序中使用方法

在java程序中使用方法,java,methods,printwriter,Java,Methods,Printwriter,当涉及到将我的程序划分为方法时,我遇到了麻烦(特别是主方法和完成所有计算的另一种方法等)。我不确定如何正确地分割现有代码来创建新方法。我的程序也会写入文件 当我编译代码时,我得到一个错误 文件:F:\COMPSCI 12\java1.java[行:37] 错误:F:\COMPSCI 12\java1.java:37:缺少返回语句 但我已经有了一份回报声明 我是否正确使用了这些方法?还是怎么了? 谢谢 没有方法的原始代码 import java.io.*; public class ja

当涉及到将我的程序划分为方法时,我遇到了麻烦(特别是主方法和完成所有计算的另一种方法等)。我不确定如何正确地分割现有代码来创建新方法。我的程序也会写入文件

当我编译代码时,我得到一个错误

文件:F:\COMPSCI 12\java1.java[行:37] 错误:F:\COMPSCI 12\java1.java:37:缺少返回语句

但我已经有了一份回报声明

我是否正确使用了这些方法?还是怎么了? 谢谢

没有方法的原始代码

    import java.io.*;

public class java1
{
  public static void main (String [] args) throws IOException
  {
    //int variables are declared
    int numpoints = 100, dimension = 2, length = 100;//numpoints is set to 100, dimension is set to 2, length is set to 100

    PrintWriter fileOut = new PrintWriter (new FileWriter ("arrayNumPoints.txt"));

    //arays are declared/initialized
    double [] lengthscale = new double [dimension];
    double [][] locations = new double [numpoints][dimension];

    for (int a = 0; a < dimension; a++){//for loop runs while a is less than dimension
      lengthscale[a] = length;//stores array
    }//end for loop

    for (int x=0; x < numpoints; x++){//for loop runs while x is less than numpoints
      for (int y=0; y < dimension; y++){//nested for loop runs while y is less than dimension
        locations [x][y]= (2 * Math.random() - 1) * lengthscale[y];//creates the range and choses random point within it

        fileOut.println( locations[x][y] + ", ");//prints out coordinate

      }//end nested for loop
    }//end for loop

    fileOut.close ();
  }//end main method
}//end cass
import java.io.*;
公共类java1
{
公共静态void main(字符串[]args)引发IOException
{
//声明int变量
int numpoints=100,维度=2,长度=100;//numpoints设置为100,维度设置为2,长度设置为100
PrintWriter fileOut=新的PrintWriter(新的FileWriter(“arrayNumPoints.txt”);
//aray被声明/初始化
双[]长度刻度=新双[尺寸];
双精度[][]位置=新的双精度[numpoints][dimension];
for(int a=0;a
相同的代码,但使用方法

 import java.io.*;

public class J4_2_MultiDimensionalArray7
{
  public static void main (String [] args) throws IOException
  {
    int numpoints = 100, dimension = 2, length = 100;//numpoints is set to 100, dimension is set to 2, length is set to 100

    //arrays are initializewd and declared
    double [] lengthscale = new double [dimension];
    double [][] locations = new double [numpoints][dimension];

    PrintWriter fileOut = new PrintWriter (new FileWriter ("arrayNumPoints.txt"));


    for(int m=0; m <length; m++){//for loop
      fileOut.println(java.util.Arrays.toString(locations[m]) + ", ");
    }
  }//end main

    public static Double writefile(Double locations[][], Double lengthscale[], int dimension, int numpoints, Double length)throws IOException
    {


    for (int a = 0; a < dimension; a++){//for loop runs while a is less than dimension
      lengthscale[a] = length;//stores array
    }//end for loop

    for (int x=0; x < numpoints; x++){//for loop runs while x is less than numpoints
      for (int y=0; y < dimension; y++){//nested for loop runs while y is less than dimension
        locations [x][y]= (2 * Math.random() - 1) * lengthscale[y];//creates the range and choses random point within it

      return locations[x][y];//returns the value of locations
      }//end nested for loop

    }//end for loop

    fileOut.close ();//close file
  }//end writefile methos
}//end cass
import java.io.*;
公共类J4_2_多维阵列7
{
公共静态void main(字符串[]args)引发IOException
{
int numpoints=100,维度=2,长度=100;//numpoints设置为100,维度设置为2,长度设置为100
//数组被初始化并声明
双[]长度刻度=新双[尺寸];
双精度[][]位置=新的双精度[numpoints][dimension];
PrintWriter fileOut=新的PrintWriter(新的FileWriter(“arrayNumPoints.txt”);

对于(int m=0;m,该方法是错误的。您将返回值声明为Double,但您正试图返回一个Double数组。此外,在循环的第一次迭代期间将调用return语句,因此它将停止在那里

public static Double writefile(Double locations[][], Double lengthscale[], int dimension, int numpoints, Double length)throws IOException
    {   

    for (int x=0; x < numpoints; x++){
      for (int y=0; y < dimension; y++){
        locations [x][y]= (2 * Math.random() - 1) * lengthscale[y];

        return locations[x][y];  <------------ this would be called in the first iteration;
      }//end nested for loop

    }//end for loop

    fileOut.close ();//close file
  }
public static Double writefile(双位置[],双长度刻度[],int维,int numpoints,双长度)引发IOException
{   
对于(int x=0;x返回位置[x][y];该方法错误。您将返回值声明为Double,但尝试返回一个Double数组。此外,将在循环的第一次迭代期间调用return语句,因此它将在此停止

public static Double writefile(Double locations[][], Double lengthscale[], int dimension, int numpoints, Double length)throws IOException
    {   

    for (int x=0; x < numpoints; x++){
      for (int y=0; y < dimension; y++){
        locations [x][y]= (2 * Math.random() - 1) * lengthscale[y];

        return locations[x][y];  <------------ this would be called in the first iteration;
      }//end nested for loop

    }//end for loop

    fileOut.close ();//close file
  }
public static Double writefile(双位置[],双长度刻度[],int维,int numpoints,双长度)引发IOException
{   
对于(int x=0;x返回位置[x][y];假设
numpoints==0
。您的代码会到达return语句吗

在另一种情况下,如果函数确实返回,将调用
fileOut.close();

Java认识到可能存在无法到达return语句的情况,并将其视为没有return语句。要解决这一问题,应该在函数末尾使用“默认”return语句,以处理循环未进入的边缘情况

我不确定如何正确地分割现有代码来创建新方法

这实际上取决于您和代码所做的工作,但有一些指导原则:

  • 方法太长而无法理解?请将其分解为几个方法
  • 你在写“重复代码”吗?也许这应该放在一个方法中
  • 像写入文件这样的东西是一个离散的操作单元。换句话说,与程序的其他部分的逻辑分离。因此,它应该作为自己的方法分离
  • 等等

假设
numpoints==0
。您的代码会到达return语句吗

在另一种情况下,如果函数确实返回,将调用
fileOut.close();

Java认识到可能存在无法到达return语句的情况,并将其视为没有return语句。要解决这一问题,应该在函数末尾使用“默认”return语句,以处理循环未进入的边缘情况

我不确定如何正确地分割现有代码来创建新方法

这实际上取决于您和代码所做的工作,但有一些指导原则:

  • 方法太长而无法理解?请将其分解为几个方法
  • 你在写“重复代码”吗?也许这应该放在一个方法中
  • 像写入文件这样的东西是一个离散的操作单元。换句话说,与程序的其他部分的逻辑分离。因此,它应该作为自己的方法分离
  • 等等

    • 其他人指出了几点


      我认为这里最重要的一般原则是。在您的特殊情况下,在一个位置计算某些内容和将数据持久化到一个文件中是两个截然不同的、明确的问题。

      其他人指出了几点

      我认为这里最重要的一般原则是。在您的特殊情况下,在一个位置计算某些内容和将数据持久化到一个文件中是两个截然不同的、明确的问题。