java 如何从另一个类中的方法访问int变量?

sigwle7e  于 2023-03-28  发布在  Java
关注(0)|答案(2)|浏览(111)

我在一个名为Bus的类中有一个方法(public void AddBusses)。在方法中有一些int变量来跟踪一些ArrayList的大小。我希望能够在我的主类中使用arrayList的大小。
我尝试在类总线中声明int变量,但这只是使它们都为0,并且不会在方法中给予它们的值。
然后,我尝试使用get方法将它们获取到主类,但结果相同,返回0。
上面讨论的尝试位于Bus类中并被注解掉。

//      public int b1;
//      public int b2;
//      public int b3;
//      public int b4;
        
//      public int getB1(){
//          return b1;          
//      }
//      public int getB2(){
//          return b2;
//      }
//      public int getB3(){
//          return b3;
//      }

下面是主类中的相关代码:

import java.io.*;
import java.util.*;//Includes ArrayList

public class Bussin 
{
    public static void main (String []args)
    {
        
        //Instantiate 3 ArrayLists 
        
        //ArrayList<Student> StudentList = new ArrayList<Student>();
        List<Student> StudentList = Collections.synchronizedList(new ArrayList<Student>());
        //ArrayList<Bus> Bus = new ArrayList<Bus> ();
        List<Bus> BusList = Collections.synchronizedList(new ArrayList<Bus>());
        List<Bus2> BusList2 = Collections.synchronizedList(new ArrayList<Bus2>());
        //ArrayList<BusStop> BusStop = new ArrayList<BusStop>();
        List<BusStop> BusStopList = Collections.synchronizedList(new ArrayList<BusStop>());

File myFile_1_5 = null;
        FileWriter myWriter_1_5 = null;
        Scanner myReader_1_5 = null;

//1_5
        try {
              myFile_1_5 = new File("D:\\11th Grade Portfolio\\AP Computer Science A\\ArrayListSizes.txt");          
              if (myFile_1_5.createNewFile()) 
              {
                System.out.println("File created: " + myFile_1_5.getName());
              } else {
                System.out.println("File already exists.");
                myReader_1_5 = new Scanner(myFile_1_5); 
                  int ReadStudentListSize_1_5 = myReader_1_5.nextInt();
                  int ReadBusListSize_1_5 = myReader_1_5.nextInt();
                  int ReadBusStopListSize_1_5 = myReader_1_5.nextInt();
                  System.out.println(ReadStudentListSize_1_5);
                  System.out.println(ReadBusListSize_1_5);
                  System.out.println(ReadBusStopListSize_1_5);            
                      myReader_1_5.close();           
              }   
        } catch (IOException e) {
          System.out.println("An error occurred.");
          e.printStackTrace();
        }
        

        case 2:
                
                Scanner Reader2 = new Scanner(System.in);
                int R2 = 0;
                while(R2 != -1)
                {
                    System.out.println("\nBusses");
                    System.out.println("1. Add Busses\n2. Remove Busses\n3. Move Busses\n4. View Busses\n5. View Students\n6. See all Busses");
                    System.out.println("\n Enter 1-3 to make a selection, or -1 to quit:");
                    R2 = Reader2.nextInt();
                    System.out.println("__________________________________________________________________\n");
                    
                    Bus b = new Bus (BusList);  
                    switch (R2)
                    {
                        case -1:
                            System.out.println("Returning to main menu...");
                            break;
                        case 1:
                            
                            b.AddBusses(StudentList, BusList2, BusStopList);
                            
                            break;
                        case 2:
                            
                            b.RemoveBusses();
                            
                            break;
                        case 3: 
                            
                            b.MoveBusses();
                            
                            break;
                        case 4:
                            
                            b.ViewBusses();
                            
                            break;
                        case 5:
                            
                            b.ViewStudents();
                            
                            break;
                        case 6:
                            
//                          b.SeeAllBusses();                           
                            System.out.println("______________");
                            System.out.println("See all Busses");                                                       
//    Test                      int StudentListSize = b.b5;
//        with                  int BusListSize = b.b6;
//          b5,6,7.             int BusStopListSize = b.b7;
//      This                    int StudentListSize = b.getB1();
//          was the             int BusListSize = b.getB1();
//              second attempt.     int BusStopListSize = b.getB3();    
//      This                    int StudentListSize = b.b1;  
//        was the               int BusListSize = b.b2;
//               first attempt.         int BusStopListSize = b.b3;     
                            
                            try {                               
                            myWriter_1_5 = new FileWriter("D:\\11th Grade Portfolio\\AP Computer Science A\\ArrayListSizes.txt");
                            
                            myWriter_1_5.write(StudentListSize + "\n");//+ " " + BusListSize + " " + BusStopListSize);
                            myWriter_1_5.write(BusListSize + "\n");
                            myWriter_1_5.write(BusStopListSize + "\n");
                            
                            
                            System.out.println("Successfully wrote to the file.");
                            
                            myWriter_1_5.close();
                            
                            } catch (IOException e) {
                              System.out.println("An error occurred.");
                                  e.printStackTrace();
                            }
                            
                            //Read from the file
                            try {
                                
                                  myReader_1_5 = new Scanner(myFile_1_5);
                                  while (myReader_1_5.hasNextLine()) 
                                  {
                                    String data1_5 = myReader_1_5.nextLine();
                                    System.out.println(data1_5);
                                  }                                   
                                  myReader_1_5.close();
                            } catch (IOException e) {
                              System.out.println("An error occurred.");
                                  e.printStackTrace();
                                  
                                }                                                                                                                                                                                                   
  
                                                                                      try {
                            myWriter_2 = new FileWriter("D:\\11th Grade Portfolio\\AP Computer Science A\\Busses.txt");
                            
                                ListIterator<Student> l = StudentList.listIterator();
                                while(l.hasNext())
                                {
                                    myWriter_2.write(l.next().toString().toUpperCase()) ;       
                                }
                                
                                myWriter_2.write("\n");
                                
                                ListIterator<Bus2> m = BusList2.listIterator();
                                while(m.hasNext())
                                {
                                    myWriter_2.write(m.next().toString().toUpperCase()); 
                                }
                                
                                myWriter_2.write("\n");
                                
                                ListIterator<BusStop> n = BusStopList.listIterator();
                                while(n.hasNext())
                                {
                                    myWriter_2.write(n.next().toString().toUpperCase());
                                }
                            
                                                                                                                                                                                                                
                            System.out.println("Successfully wrote to the file.");
                            
                            myWriter_2.close();
                            } catch (IOException e) {
                              System.out.println("An error occurred.");
                                  e.printStackTrace();
                            }
                            
                            //Read from the file
                            try {
                                
                                  myReader_2 = new Scanner(myFile_2);
                                  while (myReader_2.hasNextLine()) 
                                  {
                                    String data = myReader_2.nextLine();
                                    System.out.println(data);
                                  }                                   
                                  myReader_2.close();
                            } catch (IOException e) {
                              System.out.println("An error occurred.");
                                  e.printStackTrace();
                                  
                                }
                            
                            
                            
                            break;
}
}

我忘了说主类中的大部分代码是嵌套菜单的一部分,所以我没有包括大菜单的开始,但你可以在上面的代码中看到一个嵌套菜单。
下面是总线类的相关代码:(对不起,我很困惑我自己:)

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.io.*;

public class Bus 
{
    
    
    //public String Bus;
    public List<Bus2> Bus_B;
    public List<Student> StudentList_B;
    public List<BusStop> BusStopList_B;
    
    //public Bus(List<Student> StudentList2, String Bus2, List<BusStop> BusStopList2)
    public Bus(List<Student> StudentList2, List<Bus2> BusList3, List<BusStop> BusStopList2)
    {
        
        StudentList_B = StudentList2;
        //Bus = Bus2;
        Bus_B = BusList3;
        BusStopList_B = BusStopList2;
    }
    

    //Stuff for all methods to use
    
        static Scanner Reader_B = new Scanner(System.in);
        
        private List<Bus> BusList;
        
        Bus(List<Bus> BusList)
        {
            this.BusList = BusList;
        }
        
//      public int b1;
//      public int b2;
//      public int b3;
//      public int b4;
        
//      public int getB1()
//      {
//          return b1;
//          
//      }
//      public int getB2()
//      {
//          return b2;
//          
//      }
//      public int getB3()
//      {
//          return b3;
//      }
        
        
        public int b5 = 69; // b5,6,& 7 were just tests to access the ints- these worked and 
        public int b6 = 420; // was able to access the value, but only b/c I initialized  
        public int b7 = 911; // the method.
        
        public void AddBusses (List<Student> StudentList2, List<Bus2> BusList3, List<BusStop> BusStopList2)
        {
            String Bus2;
            String Name2;
            String BusStop2;
            
            int StudentCount;
            int BusStopCount;
                    
            System.out.println("__________");
            System.out.println("Add Busses");
            
            
            System.out.println("Enter the number of the new Bus:");         
            Bus2 = Reader_B.nextLine();         
            
            Bus2 NewBus_B = new Bus2 (Bus2);
            
            BusList3.add(NewBus_B);
            
             int b2 = BusList3.size();
            
            System.out.println("The size of the array is " + b2 + ". The bus number is " + NewBus_B.Bus + ".");
            
            System.out.println("How many students ride Bus " + Bus2 + "?");         
            StudentCount = Reader_B.nextInt();          
            System.out.println("Enter the names of the " + StudentCount + " Students that ride Bus " + Bus2 + ".");
            
            Name2 = Reader_B.nextLine();
            
            for (int i = 0; i < StudentCount; i++)
            {
                System.out.println("Student Name:");
                Name2 = Reader_B.nextLine();
                
                Student NewStudent_B = new Student(Name2, Bus2);
                
                StudentList2.add(NewStudent_B); 
                
                int b1 = StudentList2.size();
                
                System.out.println("the size of the array is " + b1 + ". The student's name is " + NewStudent_B.Name + " and their bus number is " + NewStudent_B.BusNumber + ".");
                
            }
                    
            System.out.println("\nHow many Bus Stops does " + Bus2 + " stop at?");          
            BusStopCount = Reader_B.nextInt();          
            System.out.println("Enter the names of the " + BusStopCount + " Bus Stops that Bus " + Bus2 + " stops at.");
            
            BusStop2 = Reader_B.nextLine();
            
            for (int j = 0; j < BusStopCount; j++)
            {
                System.out.println("Bus Stop Name:");
                
                BusStop2 = Reader_B.nextLine();
                
                BusStop NewBusStop_B = new BusStop(BusStop2, Bus2);
                
                BusStopList2.add(NewBusStop_B);
                
                int b3 = BusStopList2.size();
                
                System.out.println("The size of the array is " + b3 + ". The name of the new Bus Stop is " + NewBusStop_B.BusStop + ".");
            }
            
            
            
            Bus NewBus = new Bus(StudentList2, BusList3, BusStopList2);
            
            BusList.add(NewBus);
        
            int b4 = BusList.size();
            
            System.out.println("The size of the array is " + b4 + ".");
                    
            
            
            
        }
cdmah0mi

cdmah0mi1#

从我在代码中看到的情况来看,你从来没有在一个方法中为b1、2、3或4 accept赋值,如果你运行AddBusses()方法,b1应该是一个有效值。

c3frrgcw

c3frrgcw2#

我可以解决我的问题!
在Bus类中,我在方法外部将int变量声明为 public static int,然后从方法中删除声明语句。
在Main类中,我通过调用类访问变量,然后访问变量。
下面是新旧代码的示例:
相关主类代码:之前(2种不同方式):

//                          int StudentListSize = b.b1;
//                          int BusListSize = b.b2;
//                          int BusStopListSize = b.b3;
//                          int StudentListSize = b.getB1();
//                          int BusListSize = b.getB1();
//                          int BusStopListSize = b.getB3();

之后:

int StudentListSize = Bus.b1;
                        int BusListSize = Bus.b2;
                        int BusStopListSize = Bus.b3;

相关总线类代码:之前(2次不同尝试):

//      public int b1;
//      public int b2;
//      public int b3;
//      public int b4;
        
//      public int getB1()
//      {
//          return b1;
//          
//      }
//      public int getB2()
//      {
//          return b2;
//          
//      }
//      public int getB3()
//      {
//          return b3;
//      }

内部方法:

//          int b1 = StudentList2.size();
//          int b2 = BusList3.size();
//          int b3 = BusStopList2.size();

之后:

public static int b1;
public static int b2;
public static int b3;

内部方法:

b1 = StudentList2.size();
b2 = BusList3.size();
b3 = BusStopList2.size();

相关问题