0
方法適用範圍我對Java進行方法的類。基本上這個類接收一個整數數組,加上數字&也減去它們,返回和和減法。我在頂部聲明瞭變量(不是特定的方法)。當減法和加法完成他們分配到各自的變量(自動,當然),但在結束該方法做它的工作值被刪除,所以當我打電話減法/加法的結果的方法是0的變量(JAVA)
據我所知,因爲他們沒有一個方法中,但所有的方法外初始化的值不應是空的,所以範圍不應該已經結束。請幫忙嗎?
的方法
package chap3;
import javax.swing.JOptionPane;
/**
*
* @author jtech
*/
public class SimpleArithmeticMethods
{
int sum;
int subtraction;
public void sum_Difference(int[] nums)
{
int[] inpNums = nums;
sum = inpNums[0] + inpNums[1];
subtraction = inpNums[1] - inpNums[0];
}
public void getSum()
{
JOptionPane.showMessageDialog(null,"The sum is: "+sum, "Result.", JOptionPane.INFORMATION_MESSAGE);
}
public void getDifference()
{
JOptionPane.showMessageDialog(null,"The difference is: "+subtraction, "Result.", JOptionPane.INFORMATION_MESSAGE);
}
}
我從中運行
package chap3;
import javax.swing.JOptionPane;
/**
*
* @author jtech
*/
public class SimpleArithmetic
{
public static void main(String[] args)
{
String[] strInptNums = new String[2];
int[] inptNums = new int[2];
SimpleArithmeticMethods obtainSum = new SimpleArithmeticMethods();
SimpleArithmeticMethods obtainDifference = new SimpleArithmeticMethods();
SimpleArithmeticMethods workSum_Difference = new SimpleArithmeticMethods();
for (int counter = 0; counter <= 1; counter++)
{
strInptNums[counter] = JOptionPane.showInputDialog(null, "Input a number, smallest first", "Input Data.", JOptionPane.QUESTION_MESSAGE);
inptNums[counter] = Integer.parseInt(strInptNums[counter]);
}
workSum_Difference.sum_Difference(inptNums);
obtainSum.getDifference();
obtainDifference.getDifference();
}
}
郵政相關的代碼。 – kosa
請發佈我們的代碼 –
發佈代碼將有所幫助。我很好奇你的類的構造函數會發生什麼。 – jahroy