2016-10-16 4 views
0

を印刷するときは変更しないでください、私はメイン以内に私のメソッドを呼び出すためにしようとしていますが、私はそれらを印刷するとき、私のドルとセントを入力した後、値は変更しないでください値私はそれら

import java.text.DecimalFormat; 
import java.util.Scanner; 

public class ChangeMachine { 


public static void main(String[] args) { 

System.out.println("Change is good"); 

int dollars = 0; 
int cents = 0; 
int toonie = dollars/2; 
int loonie = dollars%2; 
int quarter = cents/25; 
int dime = (cents%25)/10; 
int nickel = ((cents%25)%10)/5; 

    ChangeMachine.getAmountFromUser(dollars, cents); 
    ChangeMachine.calculateCoins(dollars, cents); 
    ChangeMachine.displayInvoice(dollars, cents, toonie, loonie, quarter, dime, nickel); 

} 

方法ドルとセント は、ここで私は、金額を入力することができるが、私はコイン

public static void calculateCoins (int dollars, int cents){ 
DecimalFormat df = new DecimalFormat("#0.00"); 
double amount = dollars + cents/100.0; 
System.out.println("$"+df.format(amount)+" requires:"); 

//-----Bonus----- 
dollars=dollars+(cents+2)/100; 
cents=(cents+2)%100; 
//--------------- 

} 
を計算し、それを

public static void getAmountFromUser(int dollars, int cents) { 

Scanner input = new Scanner(System.in); 

    System.out.print("How many dollars? "); 
    dollars = input.nextInt(); 

    System.out.print("How many cents? "); 
    cents = input.nextInt(); 
    System.out.println(); 

    input.close(); 

} 

メソッドを表示しようとすると、それは表示されません3210

Javaはpass-あるので、コインが

public static void displayInvoice (int dollars, int cents, int toonie, int loonie, int quarter, int dime, int nickel){ 


System.out.println(toonie+" Toonies"); 
System.out.println(loonie+" Loonies"); 
System.out.println(quarter+" Quarters"); 
System.out.println(dime+" Dimes"); 
System.out.println(nickel+" Nickels"); 

} 

} 

答えて

2

を必要として表示するための方法は...私は彼らに

を印刷する際に値が変更されないはい、彼らは変更しないでください価値のある言語。メソッドに渡されるプリミティブ変数は変更されません(この変数の値だけを渡し、新しいローカル変数がメソッドに対して作成されます)。

どのように解決するには?我々が起動すると

:プリミティブ

EDITを保持するカスタムクラスを書い

  • ローカル変数を使用せずにインスタンス変数の上に操作を行う方法
  • から変更された値を返す

    1. メソッドから複数の値を返すことについて話していると、おそらく、私たちのメソッドは完全にうまく構築されていません(例えば、単一の責任の原則を破るなど)。その場合、私たちのメソッドをリファクタリングする必要があります。単一の責任を持つ方法(calculateDollars,calculateCentsdisplayCoin)を検討してください。コードを小さな論理的な部分に分け、より独立したものにするのに役立ちます。

      EDIT 2:今、あなたは、プログラムの最初に定義された変数と密接に結びついている

      。彼らは順番に、その時点で変数の値に束縛されています。それを修正する方法を見てみましょう:

      もっと良いですか?さんはdisplayInvoiceでそれを呼ぶことにしましょう:

      System.out.println(getLoonie(dollars) + " Loonies"); 
      

      またはより良いJavaでのパラメータの引数(ローカル変数)へ

      System.out.println(getLoonie(userCash.getDollars()) + " Loonies"); 
      
  • +0

    メソッドから複数の値を返すにはどうすればよいですか? – obonmarc

    +0

    ユーザーが入力したドルとセントを保持し、このクラスのインスタンスを返す単純なクラス "Input"を作成できます。 –

    +0

    @obonmarcまたは簡単にするためにintの配列を返すことができます。しかし、クラスをお勧めします。次に、そのクラスを他のメソッドに対する単一の引数引数として使用できます(値を変更または変更できると仮定して)。 –

    1

    参照することによって価値渡すのではなく、参照渡しされています。

    各メソッドには、独自のローカル変数参照セットがあります。新しい値をローカル変数参照に再割り当てすると、この変更はそのメソッドにとってローカルになります。

    編集:

    は、ここでそれを行うための一つの方法です:

    import java.text.DecimalFormat; 
    import java.util.Scanner; 
    
    public final class ChangeMachine { 
        private ChangeMachine() { 
    
        } 
    
        public static Cash getAmountFromUser() { 
         try(final Scanner input = new Scanner(System.in)) { 
          System.out.print("How many dollars? "); 
    
          final int dollars = input.nextInt(); 
    
          System.out.print("How many cents? "); 
    
          final int cents = input.nextInt(); 
    
          return new Cash(dollars, cents); 
         } 
        } 
    
        public static void calculateCoins(final Cash cash) { 
         final DecimalFormat decimalFormat = new DecimalFormat("#0.00"); 
    
         final double amount = cash.getDollars() + cash.getCents()/100.0D; 
    
         System.out.println("$" + decimalFormat.format(amount) + " requires:"); 
    
         cash.setDollars(cash.getDollars() + (cash.getCents() + 2)/100); 
         cash.setCents((cash.getCents() + 2) % 100); 
        } 
    
        public static void displayInvoice(final Cash cash) { 
         System.out.println(cash.calculateToonies() + " Toonies"); 
         System.out.println(cash.calculateLoonies() + " Loonies"); 
         System.out.println(cash.calculateQuarters() + " Quarters"); 
         System.out.println(cash.calculateDimes() + " Dimes"); 
         System.out.println(cash.calculateNickels() + " Nickels"); 
        } 
    
        public static void main(final String[] args) { 
         final Cash cash = getAmountFromUser(); 
    
         calculateCoins(cash); 
         displayInvoice(cash); 
        } 
    
        private static final class Cash { 
         private int cents; 
         private int dollars; 
    
         public Cash(final int dollars, final int cents) { 
          this.dollars = dollars; 
          this.cents = cents; 
         } 
    
         public int calculateDimes() { 
          return this.cents % 25/10; 
         } 
    
         public int calculateLoonies() { 
          return this.dollars % 2; 
         } 
    
         public int calculateNickels() { 
          return this.cents % 25 % 10/5; 
         } 
    
         public int calculateQuarters() { 
          return this.cents/25; 
         } 
    
         public int calculateToonies() { 
          return this.dollars/2; 
         } 
    
         public int getCents() { 
          return this.cents; 
         } 
    
         public int getDollars() { 
          return this.dollars; 
         } 
    
         public void setCents(final int cents) { 
          this.cents = cents; 
         } 
    
         public void setDollars(final int dollars) { 
          this.dollars = dollars; 
         } 
        } 
    } 
    
    0

    は、以前にこのコードをコンパイルできましたか?私はChangeMachine、getAmountFromUser、CalculateCoinsメソッドをチェックして、Notepad ++でそれらをチェックしていて、いくつかのエラーが出ました。 getAmountFromUser、System.out.println();および 'input.close();'のコードの最後の2行は、必要ではないようです。

    その他のエラーについては、私はclass, interface, or enum expectedエラーの多くを見ています。