2016-12-11 6 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メソッド
  • に任意のパラメータを渡さないことはありません。私たちは、次のようにCountryクラスを変更し

(あなたが必要な場合は、あなたが行うことができます)setterメソッドからは何も返しません。

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を返すことができます。その場合、セッターメソッド - setcountryTotalは必要ありません。

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

@DavidWallaceに感謝します。私は彼の提案に従って自分の答えを更新しました。

+0

私は同意しましたが、 'countryTotal'が他の3つの整数フィールドの合計であることはわかりませんでした。私はあなたの提案に従って私の答えを更新しています。 –

関連する問題