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

你在做整数除法。确保至少有一个操作数是浮点数,以避免截断小数点后的数字。改变这个

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

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

同样适用于其他部门

在 Java 中,int/int总是会返回一个int. 要获得带小数的结果,两个操作数之一(或两者)必须是十进制数。

于 2013-09-07T12:15:13.767 回答