如何返回变量的值,以便另一个类可以使用该值来访问数组中的值

时间:2013-11-11 15:17:55

标签: arrays

我试图返回一个变量'i',它将值的位置存储在数组中。我想返回'i'的值,以便其他类可以使用该值来访问数组中的另一个值(这可能听起来有点愚蠢)。我该怎么做?

    import java .io.*;
    public class Login{
    public int i;
    public  void Menu1()throws IOException
    {
      int flag=0;
      int goal=0;
      String p;
      String Username[]={"Mohit","Rahul","Mehul","Kevin","Tony"};
      String MNumber[]={"8720765181","8659133560","9869206216","9767445692","9967129878"};
      String Password[]={"1234","5678","9012","3456","7890"};
      int Balance[]={20,124,256,67,512};
      InputStreamReader isr = new InputStreamReader(System.in);
      BufferedReader br = new BufferedReader(isr);
      do
      {

 System.out.println("Enter your Username");
 String username=br.readLine();
 for(i=0;i<5;i++)
 {
     if(username.equals(Username[i])){
     flag=1;
     break;
    }}


    if(flag==1)
    {
         System.out.println("Enter your password");
         p=br.readLine();

         if(p.equals(Password[i]))
         {
         goal=1;
        }

        if(goal==1)
         {
             System.out.println("Password matched");
             System.out.println("Welcome Mr."+Username[i]+".Your Registired Mobile number is "+MNumber[i]);
             System.out.println("It has a balance of Rs."+Balance[i]+" which will expire on 20th August 2025");
             goal=1;
             Second_Menu sm=new Second_Menu();
             sm.main();
            }
            else
            System.out.println("Incorrect password,please retry");
        }
        else
        System.out.println("incorrect username,please retry");
    }while(goal!=1);
}


}

这是我想要调用该值的类。

       import java.io.*;
       public class Recharge
       {

     public  void recharge()throws IOException {

     int Balance[]={20,124,256,67,512};
     int chcea;
     double recAmt=0;

     double u;
     int i=0;

     InputStreamReader isr =new InputStreamReader(System.in);
     BufferedReader br=new BufferedReader(isr);
     System.out.println("Choose an option");
     System.out.println("1.Recharge your account");
     System.out.println("2.Return to Main Menu");
     chcea=Integer.parseInt(br.readLine());
     System.out.println("You choose option number "+chcea+".Proceed(y or n)");
     String answ=br.readLine();
     switch(answ)
     {
         case "y":
         switch(chcea)
         {
             case 1:System.out.println("For the first time India a Mobile Service Provider gives you the freedom to recharge with a coustom amount");
                    System.out.println("Please enter a recharge amount");
                    recAmt=Double.parseDouble(br.readLine());
                    System.out.println("Your entered amount is Rs."+recAmt+".Proceed(y or n)");
                    String optyn=br.readLine();
                    switch(optyn)
                    {
                        case "y":System.out.print("Please wait");
                        for(int wait=0;wait<=5;wait++);
                        {for(int tmp=0;tmp<=999999999;tmp++){}
                            System.out.print(".");
                        }
                        System.out.println("Your account has been successfully recharged.");
                        System.out.println("Your new Balance is Rs."+(Balance[i]+recAmt));
                        break;
                        case "n":break;
                        default:System.out.println("Please enter a valid choice");
                               break;

                            }
                            case 2 :Second_Menu sm= new Second_Menu();
                            sm.main();
                            break;

                        }
                 case"n":break;


                        }

                    }
                    }  

我想从第一个类返回'i'的值,以便第二个类可以使用它来访问正确用户的数组'Balance []'中的值。我还希望在重新充值之后该用户的余额(i)变为新的充值金额。

1 个答案:

答案 0 :(得分:0)

如果你使int公开其他类可以使用它,如果你使它静态,如果构造函数或其他东西改变它,它会改变整个程序的值 尝试使int i成为 public static int i