vendredi 18 décembre 2020

Java Wrong Input For Vector Loop Until Correct

thanks for your time and patience.

I'm a beginner student on programming, first year, I use NetBeans for my tasks. But recently I've come upon a problem I'm having a hard time solving...

Straight to the point, I need to place in a selected by User input quantity, numbers that are only in between 1 and 60, values less than 1 and more than 60 should be invalid... And asked for a new input.

Thing is, I don't know how to repeat the question and replace in the vector's square where the wrong value was inserted, to the correct one.

Ex: It keeps asking for the values, ignoring in regards of it being wrong and showing it later... Such as vector[6], the values in the end shows like: [45, 34, 23, 22, 11, 99] or [45, 55, 34, 99, 99, 99]

My task: Develop an algorithm that records bets. The player can perform bets of 6 to 15 numbers, between 1 and 60. The algorithm must then request that the inform user:

  1. the quantity of numbers in your bet;
  2. receiving bets (between 1 and 60);
  3. show the bet placed in order and;
  4. show the amount to be charged for it (following a table I was given).

My attempt:

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

}

}

Aucun commentaire:

Enregistrer un commentaire