2017-10-08 11 views
-3

これは私が学ぶようになっている技術なので、間違ったことをお詫びします。私は2つのJavaファイルの火災とシムを持っています。私はそれらの間で変数を交換しようとしていますが、それは計算したり、私が望む値を交換することに失敗するようです。Javaファイル間の基本的な交換変数

火災:

import java.util.Scanner; 

public class Fire { 

public static void main(String[] args) { 
    if (args.length != 3) { 
     return; 
    } 
    try { 
     Integer width = Integer.parseInt(args[1]); 
     Integer height = Integer.parseInt(args[2]); 
    } catch (Exception e) { 
     return; 
    } 

    Scanner keyboard = new Scanner (System.in); 

    Integer seed = Integer.parseInt(args[0]); 
    Integer width = Integer.parseInt(args[1]); 
    Integer height = Integer.parseInt(args[2]); 

    Integer addFire = Sim.add; 
    System.out.println("Test below"); 
    System.out.println(addFire); 
    } 
} 

シム:

public class Sim { 

public int width; 
public int height; 
public static int add; 

public Sim(int widthSim, int heightSim) { 
    this.width = widthSim; 
    this.height = heightSim; 
} 

public static void main(String [] agrs) { 
    Integer FirebotWidth = Fire.width; 
    Integer FirebotHeight = Fire.height; 

    Integer add = FirebotWidth + FirebotHeight; 
    } 
} 

何の目標は、あることにそれらを与える(引数1及び2からの)幅と高さの変数を取ることですシミュレーションのファイル "Integer add = FirebotWidth + FirebotHeight;"を実行し、その結果をFireコンソールで印刷します。しかし、私は唯一の出力として0を得る:

出力:

Test 
0 

すべてのヘルプは大歓迎しました。ありがとうございました。

+1

おそらく2つの主な方法を持つことを意味しません。 – Steampunkery

+0

Javaの本、特にオブジェクト指向の概念とJavaに重点を置く優れたイントロを読んでください。 –

答えて

0

ここ

Fire.java以下の説明であなたの刷新コードです:

import java.util.Scanner; 

public class Fire { 

public static Integer width; 
public static Integer height; 

public static void main(String[] args) { 
    if (args.length != 3) { 
     return; 
    } 
    try { 
     width = Integer.parseInt(args[1]); 
     height = Integer.parseInt(args[2]); 
     Integer seed = Integer.parseInt(args[0]); 
    } catch (Exception e) { 
     return; 
    } 

    Scanner keyboard = new Scanner (System.in); 

    Integer addFire = Sim.add(); 
    System.out.println("Test below"); 
    System.out.println(addFire); 
    } 
} 

Sim.java:

public class Sim { 

public int width; 
public int height; 

public Sim(int widthSim, int heightSim) { 

    this.width = widthSim; 
    this.height = heightSim; 
} 

public static Integer add() { 
    Integer FirebotWidth = Fire.width; 
    Integer FirebotHeight = Fire.height; 

    return FirebotWidth + FirebotHeight; 
    } 
} 

私はあなたので、あなたのシムクラスからmainメソッドを削除したが、 1つのエントリポイントだけが必要です。 Simクラスのadd()というメソッドを作成し、変数をFireから返すようにしました。あなたはFireクラスにインスタンス変数とクラス変数widthheightを持っていましたが、クラス変数を持つことを意味していたと思います。 parseInt()を呼び出しているため、シード変数の初期化をtry/catchの内部に移動しました。私はそれを正しく実行するためにすべてを行いましたが、あなたのコードにはもっと間違いがあります。私はa)Javaでクラスを取るか、b)基本的なJava OOPを理解できるように、本の徹底したチュートリアルを読んでおくことをお勧めします。