java矢量循环输入错误,直到正确为止

fjaof16o  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(392)

我需要在一个由用户选择输入数量,数字,只有在1和60之间,值小于1和大于60应该是无效的。。。要求提供新的信息。
问题是,我不知道如何重复这个问题,并在向量的平方中替换错误的值,到正确的值。
例句:它不断地要求价值观,忽略它是错误的,并在以后显示它。。。例如vector[6],末尾的值显示为:[45,34,23,22,11,99]或[45,55,34,99,99,99]
我的任务是:开发一个记录赌注的算法。玩家可以下注6到15个数字,介于1到60之间。然后,算法必须请求通知用户:
您下注的数字数量;
接受赌注(1到60);
显示下注的顺序和顺序;
显示要为此收取的金额(见下表)。
我的尝试:

Scanner teclado = new Scanner(System.in);
System.out.println("How many bets from 6 to 15?");
int qntd = teclado.nextInt();
int c;
int i = 0;
if ((qntd < 6) || (qntd > 15)){
    System.out.println("Only 6 to 15 values.");
} else {
    int[] bets = new int [qntd]; 
    for (c=0; c<=bets.length-1; c++ ){
        i++;
        System.out.println("Choose a number ");
        bets[c] = teclado.nextInt();               
        if ((bets[c] < 1) || (bets[c]>60)) {
            System.out.println("Only 1 to 60 values.");
        }    
    }    
    Arrays.sort(bets);
    System.out.print("Realized bets [");
    for (int valor: bets){
        System.out.print( " " + valor  );                         
    }
    System.out.println(" ]");
    System.out.println(" ]");
    String pr = null;   
    int vlr = qntd;                     
    switch (vlr){
        case 6 : 
            pr = "4,50.";
            break;
        case 7 : 
            pr = "31,50.";
            break;
        case 8 : 
            pr = "126,00.";
            break;
        case 9 : 
            pr = "378,00.";
            break;
         case 10 : 
            pr = "945,00.";
            break;
        case 11 : 
            pr = "2.079,00.";
            break;
        case 12 : 
            pr = "4.158,00.";
            break;
        case 13:
            pr = "7.722,00,";
            break;
        case 14 :
            pr = "13.513,50.";
            break;
        case 15 : 
            pr = "22.522,50.";                                 
    }
    System.out.println("Bets value is R$ " + pr);
}
3okqufwl

3okqufwl1#

您不必在数组中插入无效的输入。

int c=0;
while(c<bets.length){
    System.out.println("Choose a number ");
    int input = teclado.nextInt();
    if ((input < 1) || (input > 60)) {
        System.out.println("Only 1 to 60 values.");
        continue;
    }
    bets[c]=input;  
    c++; 
}

对于无效的输入,循环将继续,而不将输入添加到数组中。也 c 将仅对有效输入递增。

yjghlzjz

yjghlzjz2#

Scanner teclado = new Scanner(System.in);
  System.out.println("How many bets from 6 to 15?");
  int qntd = 0;
  int c;
  int i = 0;
  while(!(qntd >6 && qntd < 15))
  {
    qntd = teclado.nextInt();
    if(!(qntd >=6 && qntd <= 15))
    {
      System.out.println("Only 6 to 15 values.");
    }
  }
  int[] bets = new int [qntd];
  for (c=0; c<=qntd-1; c++ )
  {
    bets[c] = 0;
    while((bets[c] < 1) || (bets[c]>60))
    {
      bets[c] = teclado.nextInt();
      if ((bets[c] < 1) || (bets[c]>60)) 
      {
        System.out.println("Only 1 to 60 values.");
      }   
    }
  }
    Arrays.sort(bets);
                    System.out.print("Realized bets [");
               for (int valor: bets){
                 System.out.print( " " + valor  );

               }
               System.out.println(" ]");
               System.out.println(" ]");
            String pr = null;   
            int vlr = qntd;   

            switch (vlr){
        case 6 : 
            pr = "4,50.";
           break;
        case 7 : 
            pr = "31,50.";
            break;
        case 8 : 
            pr = "126,00.";
            break;
        case 9 : 
            pr = "378,00.";
            break;
        case 10 : 
            pr = "945,00.";
            break;
        case 11 : 
            pr = "2.079,00.";
            break;
        case 12 : 
            pr = "4.158,00.";
            break;
        case 13:
            pr = "7.722,00,";
            break;
        case 14 :
            pr = "13.513,50.";
            break;
        case 15 : 
            pr = "22.522,50.";

    }
    System.out.println("Bets value is R$ " + pr);
  }

相关问题