简体   繁体   中英

How to pass the value of an integer from one class to another

I'm trying to write a program that adds the total number of males in a classroom to the total number of females and then divides to find the percentage of both. With what I have right now it finds the total number of students but then gives a value of 0%? What should I do to fix this?

Main Class:

public class MainClass {

    public static void main(String[] args) {
      TextHandler.textOne();
      ScanHandler.scanOne();
      ScanHandler.scanTwo();
      TextHandler.textSix();
    }

}

ScanHandler class:

import java.util.Scanner;

public class ScanHandler {
    //SCAN VARIABLES DECLARED
    private static Scanner input = new Scanner(System.in);
    private static int x;
    private static int y;
    private static int z;
    public static void scanOne(){
        //manages start up text
        String a = input.nextLine();
        if(a.equals("y")){
            TextHandler.textTwo();
        }else if(a.equals("n")){
            TextHandler.textThree();        
        }else{
            TextHandler.textFour();
        }
    }
    public static void scanTwo(){
        //collects variable values and computes math.
        int a, b, c;
        a = input.nextInt();
        TextHandler.textFive();
        b = input.nextInt();
        c = a + b;
        x = c;
        y = a / c;
        z = b / c;
    }
    public static int getx(){
        return x;
    }
    public static int gety(){
        return y;
    }
    public static int getz(){
        return z;
    }
}

TextHandler class:

public class TextHandler {
    private static void nextLine(){
        System.out.println("");
    }
    public static void textOne(){
        System.out.println("Hello, please take a moment of your time to fill out our breif survey!");
        nextLine();
        System.out.println("Y-ES / N-O");
        nextLine();
    }
    public static void textTwo(){
        System.out.println("Thank you!");
        nextLine();
        System.out.println("Please enter the total number of females in the class.");
    }
    public static void textThree(){
        System.out.println("Very well, have a nice day!");
        System.exit(0);
    }
    public static void textFour(){
        System.out.println("Please run again using y or n.");
        System.exit(0);
    }
    public static void textFive(){
        nextLine();
        System.out.println("Please enter the total number of males in the class.");
    }
    public static void textSix(){
        int type1, type2, type3;
        type1 = ScanHandler.getx();
        type2 = ScanHandler.gety();
        type3 = ScanHandler.getz();
        System.out.println("There is a total number of " + type1 + " students in the class.");
        nextLine();
        System.out.println("The percentage of females in the class is: " + type2 + "%.");
        nextLine();
        System.out.println("The percentage of males in the class is: " + type3 + "%.");
    }
}

Since z and y variables are integers, they'll both become 0. When you divide number of females by total number of students, the outcome is between 0 and 1. Integer type stores only whole number and gets rid of factorial part. So, for example 0.5 becomes 0. You could try setting males to 0 and then y will become 1.

In order to fix that, you have to set z and y as float or double.

private static float y;
private static float z;

public static float gety(){
    return y;
}
public static float getz(){
    return z;
}

Besides that dividing integer by integer will generate an integer. You have to cast the variables you divide to floats.

y = (float)a / c;
z = (float)b / c;

When you divide two integers, Java will chop off the decimal portion. So if we have, say, 4 boys and 6 girls, you will come up with 4/10, which is 0 once we chop off the decimal portion.

If you want to keep the decimal portion, you should make one of the numbers a double rather than an int. The easiest way to do this is by multiplying one of the numbers by 1.0:

y = 1.0 * a / c;

This will give you 1.0*4/10 = 4.0/10 = 0.4.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM