public class maathclass {
public void someNumbers() {
int z = 4;
System.out.println(z);
}
}
//Trying to take variable z to add with answer from maathclass.java
import java.util.Scanner;
public class input {
public static void main (String args[]) {
double fnum, snum, answer;
Scanner userInput= new Scanner(System.in);
maathclass mathObject = new maathclass();
mathObject.someNumbers();
System.out.println("Enter first number:");
fnum = userInput.nextDouble();
System.out.println("Enter second number:");
snum = userInput.nextDouble();
// i want to add the variable z to the answer
answer = fnum + snum;
System.out.println("Answer:" + answer);
}
}
答案 0 :(得分:1)
你可以做到这一点
<强> maathclass.java 强>
public class maathclass {
private int z = 4
public void setZ(int z) {
this.z = z;
}
public int getZ(){
return z;
}
}
<强> input.java 强>
import java.util.Scanner;
public class input {
public static void main (String args[]) {
double fnum, snum, answer;
Scanner userInput= new Scanner(System.in);
maathclass mathObject = new maathclass();
mathObject.setZ(4);
System.out.println("Enter first number:");
fnum = userInput.nextDouble();
System.out.println("Enter second number:");
snum = userInput.nextDouble();
// i want to add the variable z to the answer
answer = fnum + snum + maathclass.getZ();
System.out.println("Answer:" + answer);
}
}
或者,如果您希望从z
设置maathclass
的值,则只需使用maathclass
构造函数中的值对其进行初始化。
希望这会有所帮助。