How do I calculate the percentage of the number of correct answers?

0

Well, I think I already managed to accomplish what I wanted to thank everyone for my only doubt is if a new number is generated whenever the user inserts a new guess, here is the new code:

System . out . println("Indique um valor minimo");
        int min = scanner.nextInt();
        System . out . println("Indique um valor máximo");
        int max = scanner.nextInt();
        System . out . println("Vão agora ser gerados números entre " + min + " e " + max);
        double rand = Math.random();
        int entreMinEMax =(int) (min +(max - min + 1) * rand);
        int palpites = 0;
        int acertou = 0;
        int errou = 0;

        do
        {
            System . out . println("Indique o seu palpite:");
            int palpite = scanner.nextInt ();
            if (palpite == entreMinEMax)
            {
                acertou++;
            }else
            {
              errou++;  
            }
            palpites++;
        }while(palpites<10);

        System . out . println ("Acertou " + acertou + "     "   +  (acertou/10)*100 + "%");
        System . out . println ("Errou " + errou + "     "   +  (errou/10)*100 + "%");
    
asked by anonymous 27.10.2017 / 16:50

1 answer

0

For the success test you are doing, only one successful attempt will be counted because of break you have in for . This causes the calculation to be done only by considering the failed attempts previously.

You can perform the calculation as follows:

sucesso = (total_tentativas - tentativas_falhadas) / total_tentativas

Example in code:

for(int resposta=0; resposta < 10; resposta++)
{
    System . out . println("Indique o seu palpite:");
    int palpite = scanner.nextInt ();

    if(palpite == entreMinEMax)
    {
        double sucesso = (10.0 - resposta) / 10; //calculo de sucesso
        System.out.println("Parabéns!!Acertou!!");
        System.out.println("Percentagem de sucesso: " + (sucesso*100) + "%");
        break;
    }
}

In the calculation it is done 10.0 - resposta because the variable resposta indicates the number of failed attempts previously, since every time a new iteration is done it is because the user has failed.

See the Ideone example

    
27.10.2017 / 17:06