如何在方法显示中从方法数据库中打印y值? 包libb; 导入Java .util.Scanner;
public class Libb {
void database() //first method database
{
Scanner x=new Scanner(System.in);
System.out.println("enter the number");
int y=x.nextInt(); //value y in database
}
void display() //second method display
{
System.out.println("Number is" +y); //print y in another method ie. display
}
public static void main(String[] args) {
}
}
您只需调用该方法并将变量y传递为参数。
public class Libb {
void database() //first method database
{
Scanner x=new Scanner(System.in);
System.out.println("enter the number");
int y=x.nextInt(); //value y in database
///line added here call method and pass y at same time
display(y);
}
//parameter added here
void display(int y) //second method display
{
System.out.println("Number is" +y); //print y in another.
method ie. display
}
public static void main(String[] args) {
}
}
或者您也可以使用方法返回并在显示方法中调用数据库方法并将其分配给变量,因为它将返回一个值并在打印中使用该值。