0

こんにちは皆さん、合計が正しい値を表示せず、ゼロしか表示されないことを除いて、このプログラムはほぼ終了しました。私は何年にもわたって調べてきましたが、なぜこれが起こっているのかを見つけました。誰かがここで私を助けてくれますか? 合計は、一番下の getTotals() メソッドで計算されます。ありがとう!

   import java.util.Scanner;

    public class Assignment1Q2 {

    int age = 0;
    char gender;
    char show;
    char choice = 'y';
    int over30MY = 0, over30FY = 0, under30MY = 0, under30FY = 0;
    int over30MN = 0, over30FN = 0, under30MN = 0, under30FN = 0;
    int totalCalls = 0;
    int totalWatchers = 0;
    float perTotalWatchers = 0.0f;
    float perU30M = 0.0f, perU30F = 0.0f, perO30M = 0.0f, perO30F = 0.0f, perTotalF = 0.0f,  perTotalM = 0.0f;
    float perTotalU30 = 0.0f, perTotalO30 = 0.0f, perTotal = 0.0f;

    public static void main(String[] args) {       

        Assignment1Q2 a = new Assignment1Q2(); //creates an objects to call non-static methods

        a.Info(); //Prints student info

        System.out.println("");
        System.out.println("Thanks for your call\nPlease answer some questions");
        System.out.println("");

        while(a.choice == 'y') {

            a.collectData();
        } // end of while loop

        a.getTotals();
        a.printTotals();

    }//end of main

    public void Info() { //Prints student Info

        System.out.println("Name:");
        System.out.println("Student Id:");
        System.out.println("");
        System.out.println("Tutor:");
        System.out.println("Tutorial:");
    }

    public void collectData() {

        Scanner userInput = new Scanner(System.in); //collects data from user

        //ask questions
        System.out.println("What is your age?\n");
        age = userInput.nextInt();

        System.out.println("Male or Female (Enter M or Y)");
        gender = userInput.next().charAt(0);
        gender = Character.toLowerCase(gender);

        System.out.println("Do you watch the show regularly? (Enter Y or N)");
        show = userInput.next().charAt(0);
        show = Character.toLowerCase(show);

        System.out.println("Do you want to enter another person's details? (Enter Y or N)");
        choice = userInput.next().charAt(0);
        choice = Character.toLowerCase(choice);

        //store data
        if((age >= 30) && (gender == 'm') && (show == 'y')) {       
            over30MY++;             
        }
        else if((age >= 30) && (gender == 'f') && (show == 'y')) {
            over30FY++;
        }
        else if((age < 30) && (gender == 'm') && (show == 'y')) {
            under30MY++;
        }
        else if((age < 30) && (gender == 'f') && (show == 'y')) {
            under30FY++;
        }
        else if((age >= 30) && (gender == 'm') && (show == 'n')) {
            over30MN++;
        }
        else if((age >= 30) && (gender == 'f') && (show == 'n')) {
            over30FN++;
        }
        else if((age < 30) && (gender == 'm') && (show == 'n')) {
            under30MN++;
        }
        else if((age < 30) && (gender == 'f') && (show == 'n')) {
            under30FN++;

        }//end of if else

    }//end of collectData

        public void getTotals() {//calculate totals for printing

            totalCalls = over30MY + over30FY + under30MY + under30FY + over30MN + over30FN + under30MN + under30FN;

            totalWatchers = over30MY + over30FY + under30MY + under30FY;

            perTotalWatchers = ((totalWatchers / totalCalls) * 100); //rounds percentage to nearest whole number

            if(totalWatchers > 0) {

                perU30F = ((under30FY / totalWatchers) * 100); //Total Under 30 Females
                perU30M = (under30MY / totalWatchers) * 100; //Total Under 30 Males
                perO30F = ((over30FY / totalWatchers) * 100); //Total Over 30 Females
                perO30M = ((over30MY / totalWatchers) * 100); //Total Over 30 Males

                perTotalF = Math.round(perU30F + perO30F); //Total Females
                perTotalM = Math.round(perU30M + perO30M); //Total Males

                perTotalU30 = Math.round(perU30F + perU30M); //Total Under 30
                perTotalO30 = Math.round(perO30F + perO30M); //Total Over 30
                perTotal = Math.round(perTotalF + perTotalM); //Total 

            }
            else
            System.out.println("There was no-one who watched the show");

        }//end of getTotals

        public void printTotals () { //Prints the Totals

            System.out.println("Total People called is "+totalCalls);
            System.out.println("Number of people who watch the show regularly is "+totalWatchers);
            System.out.println("Percentage of those who watch the show is regularly is "+perTotalWatchers+"%");
            System.out.println("");

            final Object[][] table = new String[4][];
            table[0] = new String[] { "Gender", "%Under 30", "%30 or Over", "%Total" };
            table[1] = new String[] { "Female", " "+perU30F, " "+perO30F, " "+perTotalF };
            table[2] = new String[] { "Male", " "+perU30M, " "+perO30M, " "+perTotalM };
            table[3] = new String[] { "Total", " "+perTotalU30, " "+perTotalO30, " "+perTotal };

            for (final Object[] row : table) {
                System.out.format("%15s%15s%15s%15s\n", row);
            }

        }//end of printTotals

}// end of class
4

1 に答える 1

1

あなたは整数除算をしています。小数点以下の数値の切り捨てを避けるために、少なくとも 1 つのオペランドが浮動小数点数であることを確認してください。これを変える

perU30F = ((under30FY / totalWatchers) * 100);

perU30F = ((under30FY / (double)totalWatchers) * 100);

他の区分も同様

Java では、int/int常にint. 10 進数で結果を取得するには、2 つのオペランドのいずれか (または両方) が 10 進数でなければなりません。

于 2013-09-07T12:15:13.767 に答える