[英]Passing value from one function to another function in Java
So i'm trying to pass value of X from function ActionPeformed to Function saveGame.所以我试图将 X 的值从函数 ActionPeformed 传递给函数 saveGame。 However the value of x when it reaches functions turns to 0. By the way here's how i should work, program will first initialize values of each player(there are four players) an each will have a X and Y values.然而,当它到达函数时 x 的值变为 0。顺便说一下,我应该如何工作,程序将首先初始化每个玩家的值(有四个玩家),每个玩家都有一个 X 和 Y 值。 and when Save button is clicked the it should take the value of X of each players and write it in a file.当点击保存按钮时,它应该获取每个玩家的 X 值并将其写入文件。
Here are my codes :这是我的代码:
Class MainPage:类主页:
import java.awt.*;
import javax.swing.*;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.awt.event.*;
import java.util.Random;
import java.lang.Math.*;
import static java.util.logging.Logger.global;
public class MainPage extends JFrame implements ActionListener{
Random rand = new Random();
.
.
protected Players[] player = new Players[5];//declare array for players
int[] dtrump = new int[5];
public MainPage(boolean savegame)
{
saveGame();
}
public MainPage(){
.
.
.
for(int i=1;i<=4;i++){ //instantiate players
player[i] = new Players();
}
public void actionPerformed(ActionEvent e){
String ac = e.getActionCommand();
String num[] = ac.split("/");
int row = Integer.parseInt(num[0]);
int col = Integer.parseInt(num[1]);
boolean sucess = false;
boolean crash = false;
int x = player[turn].getX();
int y = player[turn].getY();
.
.
for(int i=1;i<5;i++) {
System.out.println(" - ");
System.out.println(player[i].getX());
dtrump[i] = player[i].getX();
System.out.println(dtrump[i]);
}
}
.
.
.
public void saveGame()
{
for(int i=1;i<5;i++) {
System.out.println(" - ");
//System.out.println(player[i].getX());
// dtrump[i] = player[i].getX();
System.out.println(dtrump[i]);
}
try {
//System.out.println(player[2].getX());
File save = new File("save.txt");
if (!save.exists()) {
save.createNewFile();
}
FileWriter savefile = new FileWriter(save);
for (int i = 1; i < 5; i++) {
System.out.println(dtrump[i]);
savefile.write(dtrump[i] + System.getProperty("line.separator"));
// savefile.write( pyerY[k]+ System.getProperty("line.separator"));
}
savefile.close();
//JOptionPane.showMessageDialog(null,"Eggs are not supposed to be green.");
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
Class Players:班级选手:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Players{
private int x;
private int y;
.
.
.
public void setX(int x){
this.x = x; }
public int getX(){
return x; }
public void setY(int y){
this.y = y; }
public int getY(){
return y; }
}
Here is what my suspection.这是我的怀疑。 protected Players[] player
is an instance variable. protected Players[] player
是一个实例变量。 The objects created by the two constructors (the one with arguement, and the one with no arguement) will have a copy of their own(not the same instance variable).由两个构造函数创建的对象(一个有参数,一个没有参数)将拥有自己的副本(不是同一个实例变量)。 It seems saveGame
and actionPerformed
accessed from different objects.似乎saveGame
和actionPerformed
从不同的对象访问。 please try as below.请尝试如下。
protected static Players[] player = new Players[5];//declare array for players
protected static Players[] player = new Players[5];//为玩家声明数组
static int[] dtrump = new int[5];
If you want to pass a value from one method(that's what functions are referred to in Java) to another all you need to do is in the method declaration of the method you are wanting to pass X to do something like:如果要将值从一个方法(Java 中引用的函数)传递给另一个方法,则您需要做的就是在要传递 X 的方法的方法声明中执行以下操作:
public void saveGame(int aNumber){
the code you want to use X(passed and referred to as aNumber)...
}
Then when calling the savegame method you would pass X by putting:然后在调用 savegame 方法时,您将通过以下方式传递 X:
saveGame(X);
This will pass the value of X to saveGame.这会将 X 的值传递给 saveGame。 Voila!瞧!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.