在方法中使用for循环增量

时间:2018-10-20 03:09:39

标签: java methods

我正在尝试使用一种使用增量多次打印随机数的方法。问题是我似乎不太了解方法。否则我没有问题,但是当我尝试通过调用某个方法来做到这一点时,我不会得到任何回报,而我的控制台仍然一片空白。这是我到目前为止的内容:

package WayBack;

import java.util.Random;

public class Review {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
    }

    {
        method1();
    }

    public String method1()
    {
        String rv = "";
        for(int i = 0; i <= 4; i++)
        {
            Random r = new Random();
            int number = r.nextInt((100) - 0) * 100;
            System.out.println("your number is " + number);
        }
        return rv;
    }
}

任何帮助将不胜感激 谢谢

5 个答案:

答案 0 :(得分:1)

在您的代码中,您没有调用方法method1。它没有包装在Main中,而是包装在一组浮动花括号中。

package WayBack;

import java.util.Random;

public class Review {

public static void main(String[] args) {
    method1(); // put it here

}

// This block is not executed
{
method1();
}

public String method1()
{
String rv = "";
for(int i = 0; i <= 4; i++)
{
Random r = new Random();
int number = r.nextInt((100) - 0) * 100;
System.out.println("your number is " + number);
}    return rv;
}
}

答案 1 :(得分:1)

您需要在method1中调用main函数。但是您的method1函数是一个实例方法。因此,您应该新建一个Review类的实例,然后调用method1函数。

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Review instance = new Review();
    instance.method1();
}

或者,您可以将method1声明为static,以便可以直接在main方法中调用它。

public static void main(String[] args) {
    // TODO Auto-generated method stub
    method1();
}
public static String method1()
{
    String rv = "";
    for(int i = 0; i <= 4; i++)
    {
        Random r = new Random();
        int number = r.nextInt((100) - 0) * 100;
        System.out.println("your number is " + number);
    }
    return rv;
}

另一种方法是,您可以在constructor类的Review中调用method1。并且,如果您创建类的实例,则该类的constructor中的方法将被自动调用。这样,您就不必在main中调用您的方法。

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Review instance = new Review(); // it will call the method1 automatically
}
public Review() {
    // invoke your method
    method1();
}

答案 2 :(得分:0)

正如每个人都指出的那样,您需要在Committer中包含method1()。 这样做的原因是因为Java从此处启动main中的所有程序,因此它一次只能在代码块中读取一行。代码块定义为“ {”和“}”中的内容,通常称为大括号。

由于public static void main(String[] args)方法始终是静态的,并且您不能在静态方法内引用非静态内容,因此您需要将main更改为也是静态的,或者创建一个可以参考该方法。稍后您将了解这一点。

现在,

method1

答案 3 :(得分:0)

您的rv变量返回一个空值。

您可以创建一个类(MyClass),将来可以在其中添加其他方法:  例如,您可以执行以下操作:

package WayBack; 
import java.util.Random; 

public class Review {

  public static void main(String[] args) { 
   // TODO Auto-generated method stub

   public MyClass work=new MyClass();

    work.method1();

  } 
 } 
//---create an other file and write this one    
 Public Class MyClass{

   private String rv;
   private int number;
   private Random r;

  //constructor
  public MyClass(){

    this.number=0;
    this.rv="";
    this. r= new Random();

   }

  public String method1() { 

    for(int i = 0; i <= 4; i++) { 

     number = r.nextInt((100) - 0) * 100;
     System.out.println("your number is "+number);
      rv=rv+number+","; } 
      return rv; } 
}//end MyClass

答案 4 :(得分:0)

由于在块中调用了“ method1”,因此将在创建对象时执行。

{
    method1();
}

您只需要在main方法中创建对象。无需将方法设为静态。

尝试一下

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Review review = new Review();
}