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

Asked

Viewed 109 times

0

Good, I think I have already achieved what I wanted to thank everyone my only doubt is whether a new number is generated every time the user enters 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 + "%");
  • But in the code if the user hits the first, only one question is asked due to the break, soon it will never be possible to have a case where it hit 2 as indicated in the question.

1 answer

0

For the success test you are taking, only one correct attempt will be accounted for due to the break that has in the for. This makes the calculation have to be done only considering the failed attempts previously.

You can make the calculation as follows:

sucesso = (total_tentativas - tentativas_falhadas) / total_tentativas

Example in the 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 is done 10.0 - resposta because the variable resposta indicates the amount of failed attempts previously, as each time a new iteration is done because the user failed.

See the example in Ideone

  • Okay. I just made an update to the code and I think my goal is fulfilled but my only doubt is if a new number is generated whenever the user enters a new guess. Here’s the new code:

  • @Phil What do you mean ? Every new "hunch" is a new attempt to hit the same number.

  • But I think what the teacher asks is that a new number be generated within the range whenever a new guess is given. Maybe with another while or for loop??

  • @Phil I advise you to be sure of the statement before attempting any resolution, otherwise you will not be making progress. Your amendment to the question already invalidates my answer to some extent, and apparently you are going to do one more. If I am sure which goal I will be willing to adjust my response accordingly.

  • I leave the statement here just to be sure, because after having re-read several times I still have doubts in the goals(Portuguese is not my strong, best in English): "Write a program that asks for a minimum value and a maximum value from the user. Then, , the program enters a cycle that repeats 10 times. In each interaction of this cycle, the program generates a random number and gives the player 5 attempts to guess the number. Each time the program indicates whether the user has hit or not. At the end, it shows how many times the user has hit and how many missed."

  • @Phil This statement seems to contradict itself. "repeats 10 times" (...) "and gives 5 attempts". Is it 10 times or 5? Anyway it is as it is in the question

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.