【问题标题】:How to use method variables in main?如何在main中使用方法变量?
【发布时间】:2016-01-19 14:32:03
【问题描述】:

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

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;
}

基本上我需要做的就是将错误位置的值与方法中生成的随机数一起使用。我对 java 很陌生,不确定如何去做,甚至是否可能。

【问题讨论】:

  • 那么您到底需要什么帮助?
  • 怎么了?有什么错误吗?你不能打电话给move()?您不应该将ABug 对象传递给您的move() 方法以修改其位置吗?

标签: java variables methods main


【解决方案1】:

由于在java中对象是通过引用传递的,你可以将你的ABug对象传递给move函数并改变horpos、vertpos属性。所以

move(BugObj[i]);

public static void move(ABug bug){
        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;

        int originalHorpos = bug.horpos

        int originalVertpos = bug.vertpos

        // Now just change the attributes however you see fit. i am just adding r
        bug.horpos = originalHorpos + r;

        bug.vertpos = originalVertpos + r

        /*by the way, we dont need to use variables for the original values. something like this would also work 

           bug.horpos += r; 
           bug.vertpos += r;

 i just want to explain that in java when you pass objects, they are passed by reference and hence you have access to all of its members.

*/
        System.out.println(r);


    }

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

【讨论】:

  • 原始值不需要使用变量。
  • 嗯,不是所有的对象。但出于这个问题的目的,我认为如果我们将 ABug 对象传递给函数并更改属性,它将起作用。当然,这不适用于 Java 的核心数据类型,如 int、float、double 或 boolean。但是,所有其他对象确实是通过引用传递的。
  • 确切地说,谈论这些标量数据类型非常重要,这些数据类型经常被学生用来处理输入数据......并且对所有事情都不是以同样的方式工作!
猜你喜欢
  • 2015-03-21
  • 2012-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-13
  • 2011-03-23
  • 2011-06-19
  • 2023-04-03
相关资源
最近更新 更多