簡體   English   中英

java有多個類的麻煩

[英]java having trouble with multiple classes

所以我試圖使用一個開關,以便當我單擊1到4的范圍時,每個開關都指向一個類並執行其功能。 選擇“ 1”應要求用戶輸入ID,名稱,其他名稱和標記,然后計算其平均值。 然后,第二堂課應該顯示所有信息,但我不確定該怎么做。

這是我的主要代碼:

public class lab3q1 {

    public static void main (String args[]){

        Scanner sc = new Scanner(System.in);

        Entries entriesobject = new Entries(); //object declaration
        display displayobject = new display(); //object declaration
        displayall displayallobject = new displayall(); //object declaration
        sortdata sortdataobject = new sortdata();

        System.out.println("1. Add new entries: ");
        System.out.println("2. Display an entry: ");
        System.out.println("3. Display all entries: ");
        System.out.println("4. Sort Data: ");
        System.out.println("5. Exit: ");
        int s = sc.nextInt();

        switch(s){

                case 1:{
                    if(s==1)
                    try{
                        entriesobject.method0();
                    }
                    catch(Exception e){
                        System.out.println("You can't do that");

                    }
                }
                case 2:{
                    if(s==2){
                        try{
                        displayobject.method();
                        }
                        catch(Exception e){
                            System.out.println("You can't do that");
                        }
                    }

                }

                case 3:{
                    if(s==3){
                        try{
                        displayallobject.method2();
                    }
                        catch(Exception e){
                            System.out.println("You can't do that");
                        }   
                    }
                }
                case 4:{
                    if(s==4){
                        try{
                            sortdataobject.method3();
                        }
                            catch(Exception e){
                                System.out.println("You can't do that");
                    }   
                    }
                }
                case 5:{
                    if(s==5){break;}
                }               
   }
  }
}

這是頭等艙:

public class Entries {    
    public void method0(){          
        int total=0,total2;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the student id: ");
        int id = sc.nextInt();
        sc.nextLine();
        System.out.println("Enter the student name: ");
        String name = sc.nextLine();
        System.out.println("Enter the student other names: ");
        String othername = sc.nextLine();

        for(int i=1;i<=4;i++){

        System.out.println("Enter the student marks" +(i));
        int mark = sc.nextInt();
        total += mark;
        total2 =total/4;
        System.out.println("The average marks is: "+total2); 
        }           
    }       
}

這是我的第二堂課:

public class display {
  public void method() {            
        int n;          
        Scanner sc = new Scanner(System.in); 
        System.out.println("Here is the student id: ");         
    }    
}

如您所見,我似乎無法鏈接它們。

要回答您的問題:當不同的類想要了解其他類(也稱為其他對象的字段)中的數據時,您需要訪問這些數據的方法,例如:

public class Student {
  private int id;
  ... methods to put a value into id

  public int getId() { return id; }

然后可以再使用其他具有一個Student對象的其他類。

除此之外:您將“關注點分離”弄錯了。 您應該擁有一個使用掃描器從用戶“收集”數據的類。 此類創建其他各種對象。 並將來自用戶的數據放入這些對象中。

您的所有其他類都沒有/不需要掃描程序對象。 他們獲取數據,例如作為構造函數的參數。

System.out.println("The id is: " + someStudentObject.getId());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM