【发布时间】:2014-06-07 12:43:36
【问题描述】:
在方法void stock() 中,如何从方法main(); 访问变量int c; 和quan[c]; 和objIn[c];
请告诉我如何在其他方法中访问在main 方法中声明的变量?
package inventry;
import java.util.Scanner;
public class Inventry {
public void Stock()
{
System.out.println("-------------------------------");
System.out.println("-------STOCK AVAILABLE---------");
System.out.println("-------------------------------");
for( int c=0;c<objIn.length;c++)
{
System.out.println(objIn[c]+"="+quan[c]);
}
}
public static void main(String[] args)
{
System.out.println("_____________________________");
System.out.println("WELCOME TO INVENTRY SYSTEM");
System.out.println("_____________________________");
System.out.println("");
System.out.println("PLEASE ENTER NO OF OBJECTS FOR INVENTRY=");
Scanner Inp = new Scanner(System.in);
int i,j,c;
i=Inp.nextInt();
Inp.nextLine();
String objIn[]= new String[i];
String quan[]= new String[i];
System.out.println("PLEASE ENTER NAMES OF OBJECTS");
for(j=0;j<objIn.length;j++)
{
System.out.println("OBJECT NO."+j+"=");
objIn[j]=Inp.nextLine();
}
System.out.println("PLEASE ENTER QUANTITY OF OBJECTS");
for(j=0;j<objIn.length;j++)
{
System.out.print(objIn[j]+"=");
quan[j]=Inp.nextLine();
}
}
}
【问题讨论】:
-
请将您的代码缩进四 (04) 个空格以使其显示为代码。
-
将它们作为全局声明的参数提供。