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

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

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

  1. Scanner teclado = new Scanner(System.in);
  2. System.out.println("How many bets from 6 to 15?");
  3. int qntd = teclado.nextInt();
  4. int c;
  5. int i = 0;
  6. if ((qntd < 6) || (qntd > 15)){
  7. System.out.println("Only 6 to 15 values.");
  8. } else {
  9. int[] bets = new int [qntd];
  10. for (c=0; c<=bets.length-1; c++ ){
  11. i++;
  12. System.out.println("Choose a number ");
  13. bets[c] = teclado.nextInt();
  14. if ((bets[c] < 1) || (bets[c]>60)) {
  15. System.out.println("Only 1 to 60 values.");
  16. }
  17. }
  18. Arrays.sort(bets);
  19. System.out.print("Realized bets [");
  20. for (int valor: bets){
  21. System.out.print( " " + valor );
  22. }
  23. System.out.println(" ]");
  24. System.out.println(" ]");
  25. String pr = null;
  26. int vlr = qntd;
  27. switch (vlr){
  28. case 6 :
  29. pr = "4,50.";
  30. break;
  31. case 7 :
  32. pr = "31,50.";
  33. break;
  34. case 8 :
  35. pr = "126,00.";
  36. break;
  37. case 9 :
  38. pr = "378,00.";
  39. break;
  40. case 10 :
  41. pr = "945,00.";
  42. break;
  43. case 11 :
  44. pr = "2.079,00.";
  45. break;
  46. case 12 :
  47. pr = "4.158,00.";
  48. break;
  49. case 13:
  50. pr = "7.722,00,";
  51. break;
  52. case 14 :
  53. pr = "13.513,50.";
  54. break;
  55. case 15 :
  56. pr = "22.522,50.";
  57. }
  58. System.out.println("Bets value is R$ " + pr);
  59. }
3okqufwl

3okqufwl1#

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

  1. int c=0;
  2. while(c<bets.length){
  3. System.out.println("Choose a number ");
  4. int input = teclado.nextInt();
  5. if ((input < 1) || (input > 60)) {
  6. System.out.println("Only 1 to 60 values.");
  7. continue;
  8. }
  9. bets[c]=input;
  10. c++;
  11. }

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

yjghlzjz

yjghlzjz2#

  1. Scanner teclado = new Scanner(System.in);
  2. System.out.println("How many bets from 6 to 15?");
  3. int qntd = 0;
  4. int c;
  5. int i = 0;
  6. while(!(qntd >6 && qntd < 15))
  7. {
  8. qntd = teclado.nextInt();
  9. if(!(qntd >=6 && qntd <= 15))
  10. {
  11. System.out.println("Only 6 to 15 values.");
  12. }
  13. }
  14. int[] bets = new int [qntd];
  15. for (c=0; c<=qntd-1; c++ )
  16. {
  17. bets[c] = 0;
  18. while((bets[c] < 1) || (bets[c]>60))
  19. {
  20. bets[c] = teclado.nextInt();
  21. if ((bets[c] < 1) || (bets[c]>60))
  22. {
  23. System.out.println("Only 1 to 60 values.");
  24. }
  25. }
  26. }
  27. Arrays.sort(bets);
  28. System.out.print("Realized bets [");
  29. for (int valor: bets){
  30. System.out.print( " " + valor );
  31. }
  32. System.out.println(" ]");
  33. System.out.println(" ]");
  34. String pr = null;
  35. int vlr = qntd;
  36. switch (vlr){
  37. case 6 :
  38. pr = "4,50.";
  39. break;
  40. case 7 :
  41. pr = "31,50.";
  42. break;
  43. case 8 :
  44. pr = "126,00.";
  45. break;
  46. case 9 :
  47. pr = "378,00.";
  48. break;
  49. case 10 :
  50. pr = "945,00.";
  51. break;
  52. case 11 :
  53. pr = "2.079,00.";
  54. break;
  55. case 12 :
  56. pr = "4.158,00.";
  57. break;
  58. case 13:
  59. pr = "7.722,00,";
  60. break;
  61. case 14 :
  62. pr = "13.513,50.";
  63. break;
  64. case 15 :
  65. pr = "22.522,50.";
  66. }
  67. System.out.println("Bets value is R$ " + pr);
  68. }
展开查看全部

相关问题