简体   繁体   中英

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. 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. and when Save button is clicked the it should take the value of X of each players and write it in a file.

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. 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. please try as below.

protected static Players[] player = new Players[5];//declare array for players

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:

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

This will pass the value of X to saveGame. Voila!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM