簡體   English   中英

如何將整數的值從一類傳遞到另一類

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

我正在嘗試編寫一個程序,將教室中的男性總數與女性總數相加,然后除以找到兩者的百分比。 根據我現在所擁有的信息,它可以找到學生的總數,但是卻給出0%的值? 我應該怎么做才能解決這個問題?

主類:

public class MainClass {

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

}

ScanHandler類:

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類:

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 + "%.");
    }
}

由於zy變量是整數,因此它們都將變為0。用女性總數除以學生總數,結果將在0到1之間。整數類型僅存儲整數,並且除去階乘部分。 因此,例如0.5變為0。您可以嘗試將males設置為0,然后y將變為1。

為了解決這個問題,您必須將zy設置為float或double。

private static float y;
private static float z;

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

除了那個整數除以整數將生成一個整數。 您必須將划分的變量強制轉換為浮點數。

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

當您將兩個整數相除時,Java將切掉小數部分。 因此,如果我們有4個男孩和6個女孩,您將得出4/10,一旦我們將小數部分切掉,該數字將為0。

如果要保留小數部分,應將其中一個數字設為雙精度而不是整數。 最簡單的方法是將數字之一乘以1.0:

y = 1.0 * a / c;

這將為您提供1.0 * 4/10 = 4.0 / 10 = 0.4。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM