Home > Mobile >  Wrong Answer catcher
Wrong Answer catcher

Time:09-21

im here this time to ask you how to make a controller for this little program.

Basicaly i shuffle the array list and use a sysout, than i ask to put it in order and i use a do-while with an if to catch the answer.

what i would like to do now is to catch if any of two wrong answers are present.

Like, if the number 5 or 6 is present it tells there is an error. But i do not udnerstand how to do it.

here the code:

public class Test {     
    
    public static void main(String[] args) {
    
        java.util.Random randomGenerator = new java.util.Random();
        Scanner scan = new Scanner(System.in);
    
        List<String> coffeeList = Arrays.asList("1 pick it","2 drink it","3 cook","4 put it in a cup","5 salt it","6 dream it");
        Collections.shuffle(coffeeList);
        
        for(String value : coffeeList)
            System.out.println(value);      
        
        System.out.println("\n"   "ora metti in ordine la lista indicando i numeri");  
        int rispostaGiusta = 0;
        boolean flag = false;
        rispostaGiusta  = scan.nextInt();  
        do {
            
            if(rispostaGiusta == 1342) {
                System.out.println("\n"   "la risposta è giusta");   
                flag = true;
            }
            
            else {
                System.out.println("\n"   "la rispsota non è giusta ritenta");   
                rispostaGiusta  = scan.nextInt();
            }
       
        } while(!flag);
    }        
}    

CodePudding user response:

The mean of the "rispostaGiusta == 1342" numerically rispostaGiusta equals to one thousand three hundred and forty-two. So you need to use that for check numbers greater or smaller than 4 and 1 :

 if(rispostaGiusta > 4 || rispostaGiusta < 1) {

                System.out.println("\n"   "la rispsota non è giusta ritenta");   
                 rispostaGiusta  = scan.nextInt();
            }
            
            else {
                 System.out.println("\n"   "la risposta è giusta");   
                 flag = true;
            }

The upper code block is check the "is number between 4 and 1" (1 < rispostaGiusta < 4)

Or rather, as you use it, only for the control of certain 4 numbers:

 if(rispostaGiusta == 1 || rispostaGiusta == 2 || rispostaGiusta == 3 || rispostaGiusta == 4) {
                 System.out.println("\n"   "la risposta è giusta");   
                 flag = true;
            }
            
            else {
                 System.out.println("\n"   "la rispsota non è giusta ritenta");   
                 rispostaGiusta  = scan.nextInt();
            }

In this block, check the scanned number is 1 or 2 or 3 or 4

CodePudding user response:

if I understand your question correctly, try it as below. if not, please leave a comment.

public class Test {     
    
    public static void main(String[] args) {

        // ...

        rispostaGiusta  = scan.nextInt();  
        do {
            if (invalid(rispostaGiusta) {
                throw new IllealArgumentException();
            } else if (correct(rispostaGiusta)) {
                System.out.println("\n"   "la risposta è giusta");   
                flag = true;
            } else {
                System.out.println("\n"   "la rispsota non è giusta ritenta");   
                rispostaGiusta  = scan.nextInt();
            }
        } while(!flag);

    }

    private static boolean correct(int input) {
        return input == 1342;
    }

    private static boolean invalid(int input) {
        while (input > 0) {
            int number = input % 10;
            if (number == 5 || number == 6) {
                return true;
            }
            input /= 10;
        }

        return false;
    }
}

CodePudding user response:

    public class Test {     
        
        public static void main(String[] args) {
            String rightOrde = '1342'
            java.util.Random randomGenerator = new java.util.Random();
            Scanner scan = new Scanner(System.in);
        
            List<String> coffeeList = Arrays.asList("1 pick it","2 drink it","3 cook","4 put it in a cup","5 salt it","6 dream it");
            Collections.shuffle(coffeeList);
            
            for(String value : coffeeList)
                System.out.println(value);      
            
            System.out.println("\n"   "ora metti in ordine la lista indicando i numeri");  
            int rispostaGiusta = 0;
            int i =0;
            boolean flag = false;
            rispostaGiusta  = scan.nextInt();  
            do {
                
                if(rispostaGiusta.toString() == rightOrder[i]) {
                    System.out.println("\n"   "la risposta è giusta");   
                    flag = true;
                }
                
                else {
                    System.out.println("\n"   "la rispsota non è giusta ritenta"); 
                    if(rightOrder.length()==i-1){
                            flag=true;
             } 
              else{
                  rispostaGiusta  = scan.nextInt();
             }
                   
                }
           i  ;
            } while(!flag);
        }        
    }    
      

I made few changes to your code as I understood this will give you the expected answer.

  • Related