0

我正在尝试返回一个变量“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)更改为新的充值金额。

4

1 回答 1

0

如果您将 int i 设为 public 其他类可以使用它,并且如果您将其设为静态,如果构造函数或其他东西更改它,它会更改整个程序的值 尝试使 int ia
public static int i

于 2013-11-11T15:21:40.150 回答