Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在main中使用方法变量?_Java_Variables_Methods_Main - Fatal编程技术网

Java 如何在main中使用方法变量?

Java 如何在main中使用方法变量?,java,variables,methods,main,Java,Variables,Methods,Main,对于我的学校,我需要创建一种方法,将bug移动到任何方向。我有以下代码: package Test; //imports import java.util.Scanner; import java.util.Random; public class test { public static void main(String[] args) { Scanner reader = new Scanner(System.in); ABug[] BugObj

对于我的学校,我需要创建一种方法,将bug移动到任何方向。我有以下代码:

package Test;

//imports
import java.util.Scanner;
import java.util.Random;

public class test {
    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        ABug[] BugObj = new ABug[4]; //Creating object BugObj of class ABug
        int loop = 1;
        int i = 0;
        do {
            BugObj[i] = new ABug();  //creating instance
            System.out.println("Please enter the name of the bug:");   
            BugObj[i].name = reader.next(); 
            System.out.println("Please enter the species of the bug:");   
            BugObj[i].species = reader.next(); 
            System.out.println("Please enter the horizontal position of the bug:");   
            BugObj[i].horpos = reader.nextInt();
            System.out.println("Please enter the vertical postion of the bug:");   
            BugObj[i].vertpos = reader.nextInt();   

            System.out.println("_______________ Bug " +(+i+1) + " _______________\n" );
            System.out.println("Name: " + BugObj[i].name);           //Printing bug information out
            System.out.println("Species: " + BugObj[i].species);
            System.out.println("Horizontal Position: " + BugObj[i].horpos);
            System.out.println("Vertical Postion: " + BugObj[i].vertpos + "\n\n");
            move();

            i++;
            System.out.println("Would you like to enter another bug? \n 0-No,  1-Yes\n");
            loop = reader.nextInt();
        } while(loop == 1);
    }

    public static void move() {
        Scanner reader = new Scanner(System.in);
        System.out.println("Would you like this bug to move?\n 0-No,  1-Yes\n");
        if (reader.nextInt() == 0) {
            System.exit(0);
        }
        int r = (int) (Math.random() * (2- -2)) + -2;
        System.out.println(r);
    }
}

class ABug {                 //ABug class
    int horpos, vertpos, energy, id;
    char symbol;
    String species, name;
}

基本上,我需要做的就是使用bug位置的值和方法中生成的随机数。我对java真的很陌生,不知道如何做,甚至不知道它是否可行。

因为在java中对象是通过引用传递的,所以您可以将ABug对象传递给move函数并更改horpos、vertpos属性。所以

move(BugObj[i]);


此外,不需要在move函数中再次声明Scanner对象。您也可以将其传递给move函数,然后根据需要读取任意次数。

因为在java中对象是通过引用传递的,所以您只需将ABug对象传递给move函数并更改horpos、vertpos属性即可。所以

move(BugObj[i]);


此外,不需要在move函数中再次声明Scanner对象。您也可以将其传递给移动函数,然后根据需要读取任意次数。

那么,您到底需要什么帮助?出了什么问题?有错误吗?您不能调用
move()
?你不应该把
ABug
对象传递给你的
move()
方法来修改它的位置吗?那么你到底需要什么帮助?怎么了?有错误吗?您不能调用
move()
?是否应该将
ABug
对象传递给
move()
方法来修改其位置?不需要对原始值使用变量。嗯,不是所有对象。但是对于这个问题,我认为如果我们将ABug对象传递给函数并更改属性,它就会工作。当然,这不适用于Java的核心数据类型,如int、float、double或boolean。然而,所有其他对象确实是通过引用传递的。确切地说,谈论这些标量数据类型非常重要,学生经常使用这些标量数据类型来处理输入数据。。。不要对每件事都这么做!不需要为原始值使用变量。嗯,不是所有对象。但是对于这个问题,我认为如果我们将ABug对象传递给函数并更改属性,它就会工作。当然,这不适用于Java的核心数据类型,如int、float、double或boolean。然而,所有其他对象确实是通过引用传递的。确切地说,谈论这些标量数据类型非常重要,学生经常使用这些标量数据类型来处理输入数据。。。不要对每件事都这么做!