2016-11-19 1 views
0

私の目標は、ユーザーに一連の番号を尋ねることで、最大20個の番号に達するか、負の数を入力すると、 。しかし、私は数字の合計が50未満で15以上の数字がない場合にのみシリーズを使いたいと思っています。シリーズが有効になるまで一連の番号を尋ねることを繰り返す

これは私がこれまでに書いたもので、なぜ動作しないのか分かりません。

import java.util.Scanner; 

class numbersSeries { 
    public static void main(String[] args) { 
     Scanner in = new Scanner(System.in); 

     int[] numbers = new int[20]; 

     boolean invalid = true; 
     int sum = 0, input = 0, counter = 0; 

     while (invalid == true) { 
      System.out.print("Series "); 
      while ((input = in.nextInt()) > 0 && counter < 19) { 
       numbers[counter] = input; 
       sum += numbers[counter]; 
       boolean hasbignumber = false; 

       if(numbers[counter] > 15) { 
        hasbignumber = true; 
       } 
       if(sum < 50 && hasbignumber == false) { 
        invalid = false; 
       } 
       counter++; 
      } 
     }  
    } 
} 

EDIT !! 私は何か非常に重要なことに言及するのを忘れてしまった。シリーズはループがすべての繰り返しを開始します破棄されると、例えば

Series 1 2 4 30 -3    //this series will be discarded because the fourth value surpasses the maximum of 20 
Series 4 9 8 8 3 8 6 1 15 15 -2 //this one too because the sum is over 50 
Series 3 1 9 0 -2     //only this one is going to be used because it fits all conditions 

をこの出力を取るだから私が望むすべてが有効なものが導入されるまで、私はそれを使用することができますように、新しいシリーズを求めて保持するようにコンパイラのためであります後方。

+0

ようこそ。ここであなたはここにいます。役立つ答えを得るための賢い方法を質問する方法を学ぶべきです。まず、あなたのプログラムは何ではなかったのですか?望ましい行動と観察される行動の具体的な違いは何ですか? "デバッグの助けを求める質問(「なぜこのコードは動作しないのですか?」)には、目的の動作、特定の問題またはエラー、および質問自体にそれを再現するのに必要な最短コードが含まれていなければなりません。明確な問題文がない質問は他の読者には役に立たない」 –

+0

20の数字を入力するには、「counter <20」または「counter <= 19」のいずれかが必要です。 –

答えて

0

埋め込み型は使用しないでください。whileはここでは使用しないでください。

それだけで彼が最大 20の数字に到達したか、彼が入力したとき0以下値が提出された場合に負の数

while ((input = in.nextInt()) > 0 && counter < 19)条件がループ 終了を意味したら、以上の数字を求めて停止しますまたは変数counterが19に達すると、
0は負の値ではなく、この条件では20の最大数に到達できません。

boolean変数名が紛らわしいです:

while (invalid == true) { 

は、我々はそれがあなたの必要性はないながら、出口への有効な入力を持つことを期待感を与えます。このルールのために例えば

あなたのルールを伝えるboolean変数名を使用する必要があります

私は唯一の数字の合計が50 下にあるときシリーズを使用したいと15よりも優れた数字はありません。

変数mustKeepSerieを使用できます。 ループを終了するには、boolean式をwhileの条件に入れることができます。冒頭


入力がまだ開始されていないとして、ルールの違反が可能ではないとして、mustKeepSerietrueに設定されています。
いつでも、数値の合計が50よりも優れており、少なくとも15よりも優れた数字がある場合、mustKeepSeriefalseに設定されますが、尋ねられたとおりにループに影響はありません。

そしてしばらく、私たちはループへの単一の条件があります。

while ((input = in.nextInt()) >= 0 && counter < 20) 

に対応:

それだけで、彼は20の最大 に達するとそれ以上の数字を求めて停止しますまたは負の数を入力したときに発生します。

public static void main(String[] args) { 
    Scanner in = new Scanner(System.in); 

    int[] numbers = new int[20]; 

    boolean mustKeepSerie = true; 
    int sum = 0, input = 0, counter = 0; 

    System.out.print("Series "); 
    while ((input = in.nextInt()) >= 0 && counter < 20) { 
     numbers[counter] = input; 
     sum += numbers[counter]; 

     // mustKeepSerie condition 
     boolean hasbignumber = false; 
     if (numbers[counter] > 15) { 
      hasbignumber = true; 
     } 
     if (sum > 50 || hasbignumber) { 
      mustKeepSerie = false; 
     } 
     counter++; 
    } 

    if (mustKeepSerie) { 
     // do your processing  
    } 

    } 

編集コメント

public static void main(String[] args) { 
    Scanner in = new Scanner(System.in); 

    int[] numbers = new int[20]; 

    // boolean mustKeepSerie = true; 
    int sum = 0, input = 0, counter = 0; 

    System.out.print("Series "); 

    boolean isAcceptableSerie = false; 

    boolean isCurrentSerieValid = true; 

    while (!isAcceptableSerie) { 

    input = in.nextInt(); 

    // condition exit or loop again if input <0 
    if (input < 0) { 

     if (isCurrentSerieValid) { 
     isAcceptableSerie = true; 
     } 

     else { 
     printDiscardedSerie(numbers,counter); 
     sum = 0; 
     counter = 0; 
     isCurrentSerieValid=true; 
     } 
     continue; 
    } 

    numbers[counter] = input; 
    sum += numbers[counter]; 

    if (sum > 50 || numbers[counter] > 15) { 
     isCurrentSerieValid = false; 
    } 

    // condition exit or loop again if counter==19 
    if (counter == 19) { 
     if (isCurrentSerieValid) { 
      isAcceptableSerie = true; 
     } 
     else { 
      printDiscardedSerie(numbers,counter); 
      sum = 0; 
      counter = 0; 
      isCurrentSerieValid=true; 
     } 
     continue; 
    } 
    counter++; 
    } 
} 


private static void printDiscardedSerie(int[] numbers, int counter) { 
    System.out.print("Series "); 
    for (int i=0; i<counter;i++){ 
    if (i>=1){ 
     System.out.print(" "); 
    } 
    System.out.print(numbers[i]); 
    } 
} 
+0

ありがとう!しかし私は正しく自分自身を説明していないので、あなたがそれを見て、ごめんなさいありがとうと、質問を編集しました:) –

+0

私は更新された質問の別のバージョンを追加しました。 – davidxxx

+0

それは本当に良いです、ありがとう!それは、破棄されたシリーズごとにストリング "シリーズ"を表示する単純な機能がないだけです。それを実装する方法はありますか? –

0
I strongly recommend you to use function/methods whatever you call it for all this, that will help you visualize the code a lot better. 


import java.util.Scanner; 

class numbersSeries 
{ 
    public static void main(String[] args) { 
    Scanner in = new Scanner(System.in); 
    int[] numbers = new int[20]; 

    //boolean invalid = true; // not needed 
    int sum = 0, input = 0, counter = 0; 

    String finalMSG = ""; // just visual console details. 

    while(true) 
    { 
     if(counter >= 20) 
     { 
      finalMSG = "Serie is not Valid!"; 
      break; 
     } 

     System.out.print("Enter number #" + (counter + 1) + ": "); 
     input = in.nextInt(); 

     if(input < 0) 
     { 
      counter = 0; 
      sum = 0; 
      numbers = new int[20]; 
      finalMSG = "Serie is not Valid!"; 
      break; 
     } // if negative    

     numbers[counter] = input; 
     counter++;   
     }//while 


     for (int i = 0; i < numbers.length; i++) 
     { 
     if(numbers[i] > 15) 
     { 
      finalMSG = "Serie is not Valid! There's a number > 15."; 
      break; 
     } 

     else if(sum >= 50) 
     { 
      finalMSG = "Serie is not Valid!"; 
      break; 
     } 

     else 
     { 
      finalMSG = "Serie is Valid!"; 
      sum+= numbers[i]; 
     } 
     }//for 

     System.out.println(finalMSG + " sum is: " + sum); 
     System.exit(0); 
    } 
} 
+0

あなたの答えをありがとう!まず、私はこの方法でメソッドを使いたいとは思っていません。そして、私は自分自身を説明しませんでしたので、私はそれを編集しましたが、基本的に、コンパイラは有効なものが導入されるまで、それは文字列 "シリーズ"を表示します。私は自分自身を正しく説明できないのは残念ですが、私はまだここにいます。 –

0

後の新しいソリューションではなく、whileループ自体の内部ロジックをやろうとするよりも、whileループの中にすべての条件を入れて。

input = in.nextInt(); 
while(input<15 && counter < 19 && sum < 50) { 
    numbers[counter]=input; 
    sum+=numbers[counter]; 
    counter++; 
    input = in.nextInt(); 
} 
関連する問題