2016-11-19 35 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++; 
      } 
     }  
    } 
} 

編輯! 我忘了提一件很重要。一旦該系列被丟棄的循環會從頭再來,利用這個輸出例如

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

歡迎來到Stackoverflow。現在你在這裏,你應該學會如何以聰明的方式提出問題,以獲得有用答案的絕佳機會。首先,你的程序不工作的是什麼?理想和觀察行爲之間的具體區別是什麼? 「尋求調試幫助的問題(」爲什麼這個代碼不工作?「)必須包含所需的行爲,特定的問題或錯誤以及在問題本身中重現問題所需的最短代碼。沒有明確問題陳述的問題對其他讀者沒有用處。「 –

+0

要輸入20個數字,我認爲您需要'counter <20'或'counter <= 19'。 –

回答

0

您不應該在這裏使用嵌入式while,因爲它不是必需的。

只會停止要求更多數量,一旦他到達20個號碼中最大的 或當他輸入一個負數

while ((input = in.nextInt()) > 0 && counter < 19)條件是指循環 退出時,如果0以下值將被提交作爲輸入或變量counter達到19.
0不是一個負值,因爲您希望和此條件無法達到的最大數量20

boolean變量名是一種誤導:

while (invalid == true) { 

給我們期望有一個有效的輸入退出而這是不是你需要的感覺。

您應該使用傳達你的規則boolean變量名:

例如,對於這樣的規則:

我只希望使用該系列時的數字之和是50歲以下的 和有沒有比數15

你可以使用boolean變量mustKeepSerie優越。 並且要退出循環,您可以將boolean表達式置於while條件中。


在開始的時候,mustKeepSerie設置爲true因爲沒有違反規則的是可能作爲輸入尚未開始。
如果在任何時候,數字的總和超過50並且至少有一個超過15的數字,則mustKeepSerie設置爲false,但循環不會按照您的要求受到影響。

而在同時,我們有一個條件,以循環:

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(); 
}