메소드 용으로 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);
}
}
I 당신은 하나의 객체에 sum_Difference()
메소드를 호출하고
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