2017-02-10 3 views
1

これは説明するのが難しいかもしれませんが、私のクラスでこの問題を抱える唯一の傾向にあるので、基本的にはJava今プログラミング。私は基本的に変数の値をスキャンしてその変数をwhileループに持っていなければならない問題があります。プログラムを起動するとwhileループを開始しないという問題があります。それは私が数字を入力し続けることができますが、それは問題です、それは数字をスキャンし続けますが、それは一度だけスキャンする必要があり、ループに入りません。 これは、プログラムJavaプログラミングでは、プログラムはスキャナをwhileループに渡すことはありません

import java.util.Scanner; 
public class Week05_NelsonPimentel_Assignment { 
    public static void main(String args[]){ 
     int veraq; 
     int times = 0; 
     Scanner input = new Scanner(System.in); 


     System.out.println("Please enter how many coins you have"); 

     veraq = input.nextInt(); 
     while(veraq>0){ 


      firstmachine(veraq); 
      howmanytimesplayed(times); 
      secondmachine(veraq); 
      howmanytimesplayed(times); 
      thirdmachine(veraq); 
      howmanytimesplayed(times); 

     } 


      System.out.println("You were able to play this many times before running out of quarters: " +times); 



     } 





    static int howmanytimesplayed(int times) 
    { 
     times++; 
     return times; 
    } 

    static int firstmachine(int veraq) 
    { 
     int times = 0; 


     if(times == 33){ 
      veraq = veraq + 24; 
      times = 0; 
      System.out.println("Congradulations! On machine number one you have won $6.25!! You have this many coins left: " +veraq); 
      return veraq; 
     } 
     else if(times != 33) 
     { 
      veraq = veraq - 1; 
      times++; 
      return veraq; 
     } 
     return 0; 
    } 

    static int secondmachine(int veraq) 
    { 
     int times = 0; 

     if(times == 99){ 
      veraq = veraq + 74; 

      times = 0; 
      System.out.println("Congradulations! On machine number two you have won $18.75!! You have this many coins left: " +veraq); 
      return veraq; 
     } 
     else if(times != 99) 
     { 
      veraq = veraq - 1; 
      times++; 
      return veraq; 
     } 

     return 0; 
    } 


    static int thirdmachine(int veraq) 
    { 
    int times = 0; 

     if(times == 9){ 
      veraq = veraq + 6; 
      times = 0;**enter code here** 
      System.out.println("Congradulations! On machine number three you have won $1.75!! You have this many coins left: " +veraq); 
      return veraq; 
     } 
     else if(times != 9) 
     { 
      veraq = veraq - 1; 
      times++; 
      return veraq; 
     } 
     return 0; 
    } 
} 
+0

0より大きい数字を入力すると、無限ループになります。 –

+0

現在表示されているテキスト出力(印刷メッセージ)は何ですか? –

+0

値で呼び出される内容と参照で呼び出される内容を理解する必要があります。次に、これらの概念がJavaのメソッドに引数を渡すことにどのように関係しているかを学ぶべきです – opensam

答えて

1

あなたのプログラムは、常に数字をスキャンされていない、あなたのプログラムではwhileループはすなわち、それは無限ループで実行している終了していないされているんです。

'int'は、Javaのプリミティブデータ型です。メソッドfirstmachine(veraq);を呼び出して、このメソッドを変数のveraqで操作すると、その変更はメインメソッドのスコープ内ののveraq変数に反映されません。したがって、のベラックに正の値を渡すと、基本的に0より小さくなることはなく、whileループは永遠に実行されます。さらに、あなたのプログラムは何も印刷されないように書かれているので、あなたのプログラムは常に数字をスキャンしているように見えます。

値で呼び出されるものと参照で呼び出されるものを知る必要があります。次に、これらの概念がJavaのメソッドに引数を渡すことにどのように関連するのかを理解する必要があります。幸運:

+0

助けを感謝します:) – zenon

+0

答えに満足すれば、 – opensam

関連する問題