[英]Is there a way to access variables in another method?
I'm new to java and I was wondering if there was a way to access a variable from one method in another method.我是 java 的新手,我想知道是否有办法从另一种方法中的一种方法访问变量。 While writing a program using methods, I realized that I cannot just take one variable from one method and use it in another method.
在使用方法编写程序时,我意识到我不能只从一种方法中取出一个变量并在另一种方法中使用它。 So I was wondering if there was a way to do this.
所以我想知道是否有办法做到这一点。
Here is my code so far到目前为止,这是我的代码
import java.util.Scanner;
public class program{
public static void mass(){
double a,e,p,v;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the volume: ");
v=scanner.nextDouble();
p=0.8;
System.out.println("Enter the alcohol volume:(in percents) ");
e=scanner.nextDouble();
e=e/100;
a=v*e*p;
System.out.println("mass is: "+a);
}
public static void Concentration(){
double w,r,m;
String person;
System.out.println("Enter the person: m for male, f for female, j for teenager ");
Scanner sc=new Scanner(System.in);
person=sc.nextLine();
switch(person){
case "m": r=0.7;
break;
case "f": r=0.6;
break;
case "j": r=0.5;
break;
}
System.out.println("Enter person's weight: ");
m=sc.nextDouble();
//w=a/(m*r); //a from the method mass
}
public static void main(String[]args){
mass();
Concentration();
/*If(w>=0.5){ //w from the method concentration
System.out.println("You cannot drive!");
Else{
System.out.println("You can drive");
}
} */
}
}
There are two kinds of variables:有两种变量:
See: https://www.geeksforgeeks.org/variable-scope-in-java/参见: https://www.geeksforgeeks.org/variable-scope-in-java/
So, if you want to reuse a variable accross multiple methods, then you will need to convert it from a local variable into a member variable.所以,如果你想在多个方法中重用一个变量,那么你需要将它从局部变量转换为成员变量。 In your case you only use
static
methods, so a
would be declared outside your methods as在您的情况下,您只使用
static
方法,因此a
将在您的方法之外声明为
static double a;
and avoid declaring it inside your mass
method, so your declaration line would be changed to并避免在您的
mass
方法中声明它,因此您的声明行将更改为
double e,p,v;
Note that the a
is missing to avoid variable shadowing .请注意,缺少
a
以避免变量阴影。
You may want to change your methods to instance-level methods, in which case you can declare a
without the static
keyword, depending on your plans and needs.您可能希望将您的方法更改为实例级方法,在这种情况下,您可以根据您的计划和需要声明
a
不带static
关键字的方法。
Also, a well-known approach is to implement getters and setters in order to make sure that whenever you get or set a value, if there are common operations, then they are implemented only once instead of code repeating .另外,一个众所周知的方法是实现getter 和 setter ,以确保无论何时获取或设置一个值,如果有共同的操作,那么它们只被实现一次而不是代码重复。
Below you see the simplest changes to your code to achieve the goal you have specified:下面您将看到为实现您指定的目标而对代码进行的最简单的更改:
import java.util.Scanner;
public class program{
static double a;
public static void mass(){
double e,p,v;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the volume: ");
v=scanner.nextDouble();
p=0.8;
System.out.println("Enter the alcohol volume:(in percents) ");
e=scanner.nextDouble();
e=e/100;
a=v*e*p;
System.out.println("mass is: "+a);
}
public static void Concentration(){
double w,r,m;
String person;
System.out.println("Enter the person: m for male, f for female, j for teenager ");
Scanner sc=new Scanner(System.in);
person=sc.nextLine();
switch(person){
case "m": r=0.7;
break;
case "f": r=0.6;
break;
case "j": r=0.5;
break;
}
System.out.println("Enter person's weight: ");
m=sc.nextDouble();
w=a/(m*r); //a from the method mass
}
public static void main(String[]args){
mass();
Concentration();
/*If(w>=0.5){ //w from the method concentration
System.out.println("You cannot drive!");
Else{
System.out.println("You can drive");
}
} */
}
}
Finally, you could also use a
as a return value, like:最后,您还可以使用
a
作为返回值,例如:
import java.util.Scanner;
public class program{
public static double mass(){
double a,e,p,v;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the volume: ");
v=scanner.nextDouble();
p=0.8;
System.out.println("Enter the alcohol volume:(in percents) ");
e=scanner.nextDouble();
e=e/100;
a=v*e*p;
System.out.println("mass is: "+a);
return a;
}
public static void Concentration(double a){
double w,r,m;
String person;
System.out.println("Enter the person: m for male, f for female, j for teenager ");
Scanner sc=new Scanner(System.in);
person=sc.nextLine();
switch(person){
case "m": r=0.7;
break;
case "f": r=0.6;
break;
case "j": r=0.5;
break;
}
System.out.println("Enter person's weight: ");
m=sc.nextDouble();
//w=a/(m*r); //a from the method mass
}
public static void main(String[]args){
Concentration(mass());
/*If(w>=0.5){ //w from the method concentration
System.out.println("You cannot drive!");
Else{
System.out.println("You can drive");
}
} */
}
}
To make a variable accessible in all functions of the class you can static the variable in question in the current class要使变量在 class 的所有函数中都可访问,您可以 static 当前 class 中的相关变量
package javaapplication6; package java应用程序6;
import java.util.Scanner;导入 java.util.Scanner;
// @author Vulembere // @author Vulembere
public class JavaApplication6 {公共 class JavaApplication6 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
mass();
Concentration();
if (w >= 0.5) {
System.out.println("You cannot drive!");
} else {
System.out.println("You can drive");
}
}
static double w;
public static void mass() {
double a, e, p, v;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the volume: ");
v = scanner.nextDouble();
p = 0.8;
System.out.println("Enter the alcohol volume:(in percents) ");
e = scanner.nextDouble();
e = e / 100;
a = v * e * p;
System.out.println("mass is: " + a);
}
public static void Concentration() {
double r, m;
String person;
System.out.println("Enter the person: m for male, f for female, j for teenager ");
Scanner sc = new Scanner(System.in);
person = sc.nextLine();
switch (person) {
case "m":
r = 0.7;
break;
case "f":
r = 0.6;
break;
case "j":
r = 0.5;
break;
}
System.out.println("Enter person's weight: ");
m = sc.nextDouble();
//w=a/(m*r); //a from the method mass
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.