2016-12-11 39 views
-2

我想在這個程序中使用getter和setter方法,但不能編譯這個文件。在哪裏和如何在java中使用getter和setter方法?

class mini4 
{ 
public static void main (String [] args) 
{ 

    String nane =""; 
    int Gold = 0; 
    int Silver = 0; 
    int Bronze = 0; 
    int total = 0; 


    int totalG = 0; 
    int totalS = 0; 
    int totalB = 0; 
    int sumofall = 0; 

    country c1 = new country(); 


    c1 = setcountryname("United Kindom"); 
    c1 = setcountryGold(27); 
    c1 = setcountrySilver(23); 
    c1 = setcountryBronze(17); 
    c1 = setcountryTotal(67); 




    String messagetoprint = ""; 
    String choosemedals = ""; 

    int totalGold = calculateG(c1,totalG, Gold); 
    int totalSilver = calculateS(c1, totalS, Silver); 
    int totalBronze = calculateB(c1,totalB, Bronze); 
    sumofall = calculateall(totalGold,totalSilver,totalBronze,sumofall); 


    System.out.println("     G S B Total"); 
    System.out.println(" Great Britain "+totalGold+"  " +totalSilver +"  "+totalBronze +"  "+ sumofall); 


    String which = inputString ("which medal total do you want to know? (Gold, Silver or Bronze)"); 
    w(which,totalGold,totalSilver,totalBronze); 


    String again = inputString ("DO you want to see the data again? (Yes or No)"); 
    a(again,totalGold,totalSilver,totalBronze,sumofall); 

    String oldnew = inputString ("What do you want to see old or new?"); 
    o(oldnew,Gold,Silver,Bronze,totalGold, totalSilver, totalBronze,total,sumofall); 

System.exit(0); 
} 



public static int calculateG(country c1,int totalG,int Gold) 
{ 
    Gold = getcountryGold(c1); 
    int G = inputInt(" How many people won a Gold ?"); 
    totalG = Gold + G; 
    return totalG; 
    } 


public static int calculateS(country c1, int totalS, int Silver) 
{ 
    Silver = getcountrySilver(c1); 
    int S = inputInt(" How many people won a silver ?"); 
    totalS = Silver + S; 
    return totalS; 
} 

public static int calculateB(country c1,int totalB, int Bronze) 
{ 
    Bronze = getcountryBronze(c1); 
    int B = inputInt(" How many people won a bronz ?"); 
    totalB = Bronze + B; 
    return totalB; 
} 

    public static int calculateall(int totalGold,int totalSilver,int totalBronze, int sumofall) 
{ 
    int k = 0; 
    k = totalGold + totalSilver + totalBronze; 
    sumofall = k; 
    return sumofall; 
    } 


public static void w(String which, int totalGold, int totalSilver, int totalBronze) 
{ 
    if (which.equals("Gold") ||which.equals("gold")) 
    { 
     System.out.println(totalGold); 

    } 
    else if(which.equals("Silver") || which.equals("silver")) 
    { 
     System.out.println(totalSilver); 
    } 
    else if(which.equals("Bronze") ||which.equals("bronze")) 
    { 
     System.out.println(totalSilver); 
    } 
    else 
    { 
     System.out.println(" Can you check spelling?"); 
     System.exit(0); 
    } 


} 
public static String inputString(String message) 
{ 
    Scanner scanner = new Scanner(System.in); 
    String answer; 

    System.out.println(message); 
    answer = scanner.nextLine(); 

    return answer; 
} 

public static void a(String again, int totalGold, int totalSilver, int totalBronze, int sumofall) 
{ 
    if (again.equals("Yes") || again.equals("yes")) 
    { 
     System.out.println("     G S B Total"); 
     System.out.println(" Great Britain "+totalGold+"  " +totalSilver +"  "+totalBronze +"  "+ sumofall); 

    } 
    else if(again.equals("No") || again.equals("no")) 
    { 
     System.out.println("This is your choose"); 
    } 

    else 
    { 
     System.out.println(" Can you check spelling?"); 
     System.exit(0); 
    } 


    } 
     public static void o (String oldnew, int Gold, int Silver, int Bronze,int totalGold, int totalSilver, int totalBronze, int sumofall, int total) 
    { 
     if (oldnew.equals("Old") || oldnew.equals("old")) 
     { 
      int tt = Gold+Silver+Bronze; 
      System.out.println("     G S B Total"); 
      System.out.println(" Great Britain "+Gold+"  " +Silver +"  "+Bronze +"  "+ tt); 

     } 
     else if(oldnew.equals("New") || oldnew.equals("new")) 
     { 
      int newtotal = total-sumofall; 
      int gg = totalGold-Gold; 
      int ss = totalSilver-Silver; 
      int bb = totalBronze-Bronze; 
      System.out.println("     G S B Total"); 
      System.out.println(" Great Britain "+gg+"  " +ss +"  "+bb +"  "+ newtotal); 
    } 

     else 
    { 
     System.out.println(" Can you check spelling?"); 
      System.exit(0); 
    } 


    } 


    public static int inputInt(String message) 
    { 
     return Integer.parseInt(input(message)); 
    } 




     public static String input(String message) 
    { 
     Scanner scanner =new Scanner(System.in); 
     String answer; 

     System.out.println(message); 
     answer = scanner.nextLine(); 

     return answer; 
    } 

     public static void print(String message) 
    { 
     System.out.println(message); 

    } 

    public static String getcountryname (country c1) 
    { 
     return countryname; 
}  
     public static int getcountryGold (country c1) 
     { 
     return countryGold; 
    }  
     public static int getcountrySilver (country c1) 
    { 
     return countrySilver; 
}  
public static int getcountryBronze (country c1) 
{ 
    return countryBronze; 
    }  


    public static country setcountryname ( String newname) 
    { 
     countryname = newname; 
     return countryname; 
    } 
    public static country setcountryGold (int newGold) 
    { 
    countryGold = newGold; 
    return countryGold; 
    } public static country setcountrySilver (int newSilver) 
    { 
     countrySilver = newSilver; 
     return countrySilver; 
     } public static country setcountryBronze ( int newBronze) 
    { 
     countryBronze = newBronze; 
    return countryBronze; 
    } 
    public static country setcountryTotal (int newTotal) 
    { 
    countryTotal = newTotal; 
    return countryTotal; 
    } 

    } 
class country 
{ 
    String countryname; 
    String countryGold; 
    String countrySilver; 
    String countryBronze; 
    String countryTotal; 

     } 

後,我編譯這一點,有多處錯誤,在這裏我要問有什麼錯我的getter和setter方法以及如何使用getter和setter方法正確方法?

+1

你得到什麼類型的錯誤? –

+3

簡短的回答 - 你的獲得者和制定者都是錯誤的。他們需要與他們獲得和設置的領域處於同一個班級。然而,這個程序還有很多其他的錯誤,並且我會誠實地建議你在嘗試進一步深入之前通過一個基本的Java教程來工作。 –

+0

明確指出你面臨的問題。不要給出整個代碼,給你的代碼片斷,你得到編譯錯誤。 –

回答

0

你並不需要做到以下幾點:

  • 不要使吸氣,setter方法靜態
  • 一般來說,我們不要將任何參數傳遞到getter方法
  • 一般來說,我們不從setter方法返回任何東西(如果你需要,那麼你可以做)

修改您的Country類,如下所示:

class Country { 

    private String countryname; 
    private int countryGold; 
    private int countrySilver; 
    private int countryBronze; 
    private int countryTotal; 

    public String getcountryname() { 
     return countryname; 
    } 

    public int getcountryGold() { 
     return countryGold; 
    } 

    public int getcountrySilver() { 
     return countrySilver; 
    } 

    public int getcountryBronze() { 
     return countryBronze; 
    } 

    public int getcountryTotal() { 
     return countryTotal; 
    } 

    public void setcountryname(String newname) { 
     this.countryname = newname; 
    } 

    public void setcountryGold(int newGold) { 
     this.countryGold = newGold; 
    } 

    public void setcountrySilver(int newSilver) { 
     this.countrySilver = newSilver; 
    } 

    public void setcountryBronze(int newBronze) { 
     this.countryBronze = newBronze; 
    } 

    public void setcountryTotal(int newTotal) { 
     this.countryTotal = newTotal; 
    } 

} 

除了保留countryTotal的附加字段,您可以編寫如下方法返回countryTotal。在這種情況下,你不需要setter方法 - setcountryTotal

public int getcountryTotal() { 
    return countryGold + countrySilver + countryBronze; 
} 

感謝@DavidWallace的評論。我根據他的建議更新了我的答案。

+0

我同意,但我不知道'countryTotal'是其他3個整數字段的總和。根據您的建議,我正在更新我的答案。 –