Java 如何将整数的值从一个类传递到另一个类
我试图写一个程序,把教室里的男生总数和女生总数相加,然后除以两者的百分比。根据我现在的数据,它可以找到学生总数,但给出的值是0%?我该怎么做才能解决这个问题 主要类别:Java 如何将整数的值从一个类传递到另一个类,java,Java,我试图写一个程序,把教室里的男生总数和女生总数相加,然后除以两者的百分比。根据我现在的数据,它可以找到学生总数,但给出的值是0%?我该怎么做才能解决这个问题 主要类别: public class MainClass { public static void main(String[] args) { TextHandler.textOne(); ScanHandler.scanOne(); ScanHandler.scanTwo(); T
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;
}
}
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 + "%.");
}
}
TextHandler类:
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;
}
}
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 + "%.");
}
}
由于
z
和y
变量都是整数,它们都将变为0。将女性人数除以学生总数,结果介于0和1之间。整数类型只存储整数,不存储阶乘部分。例如,0.5变为0。您可以尝试将男性设置为0,然后y
将变为1
为了解决这个问题,您必须将z
和y
设置为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;
由于
z
和y
变量都是整数,它们都将变为0。将女性人数除以学生总数,结果介于0和1之间。整数类型只存储整数,不存储阶乘部分。例如,0.5变为0。您可以尝试将男性设置为0,然后y
将变为1
为了解决这个问题,您必须将z
和y
设置为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。当您除以两个整数时,Java将切掉小数部分。如果我们有,比如说,4个男孩和6个女孩,你会得到4/10,也就是0,只要我们去掉小数部分
如果要保留小数部分,应将其中一个数字设置为双精度而不是整数。最简单的方法是将其中一个数字乘以1.0:
y=1.0*a/c代码>
这将为您提供1.0*4/10=4.0/10=0.4。您应该尝试进行计算:y=a/c代码>和z=b/c代码>手动查看您得到了什么。请记住,这些变量是整数。您应该尝试进行以下计算:y=a/c代码>和z=b/c代码>手动查看您得到了什么。记住这些变量都是整数,你是我的英雄。不知道为什么我忘了你是我的英雄。我不知道为什么我忘记了这个好答案。或者,如果您希望避免小数中经常出现的与数字无关的数字,例如浮点
/浮点
和双精度
/双精度
,请使用。回答得好。或者,如果您希望避免小数中经常出现的与数字无关的数字,例如浮点
/浮点
和双精度
/双精度
,请使用。