Домашнее задание Процент голосов - PullRequest
0 голосов
/ 27 октября 2019

Проценты печатаются неправильно, а также, когда я нажимаю «Y», чтобы продолжать набирать больше голосов за Фредди, он идет вперед и подводит итоги, затем просит добавить больше за него.

Я попытался перевернуть процентное уравнение вместе с делением его на 100 и с запущенным кодом, я попытался добавить это check = true.

// ***************************************************************
//   Election_Day.java
//
//   This file contains a program that tallies the results of
//   an election.  It reads in the number of votes for each of
//   two candidates in each of several precincts.  It determines
//   the total number of votes received by each candidate, the
//   percent of votes received by each candidate, the number of 
//   precincts each candidate carries, and the 
//   maximum winning margin in a precinct.
// **************************************************************


public static void main (String[] args) {
    int votesForJason;   // number of votes for Jason in each precinct
    int votesForFreddy;  // number of votes for Freddie in each precinct
    int totalJason = 0;  // running total of votes for Jason
    int totalFreddy = 0; // running total of votes for Freddie

    String response; // answer (y or n) to the "more precincts" question
    boolean check = true;
    Scanner input = new Scanner(System.in);

    System.out.println("\nElection Day Vote Counting Program\n");

        while ((check)) {
            System.out.print("Enter votes for Jason");
        votesForJason = input.nextInt();
        System.out.print("are there more inputs to enter? (y or n)");
        response = input.next();

        if (response.equalsIgnoreCase("Y")) {

        } else {
            while ((check)) {
                System.out.print("Enter votes for Freddy");
                votesForFreddy = input.nextInt();
                System.out.print("are there more inputs to enter? (y or n)");
                response = input.next();

                if (response.equalsIgnoreCase("Y")) {
                    check = true;
                } else {
                    check = false;
                }
                System.out.println("Total votes for Jason = " + (totalJason += votesForJason));
                System.out.println("Total votes for Freddy = " + (totalFreddy += votesForFreddy));
                System.out.println("The Percentage for Jason = "+ ((totalJason+totalFreddy)/totalJason)+ "%");
                System.out.println("The Percentage for Freddy = " + ((totalJason+totalFreddy)/totalFreddy)+ "%");
            }       
            if (totalJason>totalFreddy) {
                System.out.println("Jason Wins!");
            } else {
                System.out.println("Freddy Wins!");
            }
        }
    }
    input.close();
}

Мне нужен код, чтобы дождаться, пока я нажму "N" для Фреддичтобы вычислить итоги, и мне нужно, чтобы проценты были правильными.

...