重复询问一系列数字,直到系列有效

时间:2016-11-19 19:06:38

标签: java loops input

我的目标是向用户询问一系列数字,只有在达到最多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

所以我想要的是让编译器继续询问新系列,直到引入一个有效系列,这样我才能在后面使用它。

3 个答案:

答案 0 :(得分: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]);
  }
}

答案 1 :(得分: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);
  }
}

答案 2 :(得分:0)

将所有条件放入while循环中,而不是尝试在while循环内部执行逻辑。

input = in.nextInt();
while(input<15 && counter < 19 && sum < 50) {
  numbers[counter]=input;
  sum+=numbers[counter];
  counter++;
  input = in.nextInt();
}