2016-09-27 3 views
0

私は、教室の男性の総数を女性の総数に加え、次に分けて両方の割合を求めるプログラムを作成しようとしています。私が今持っているものでは、生徒の総数を見いだしてから、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 + "%."); 
    } 
} 
+0

「y = a/c;」と「z = b/c;」という計算を手作業で試してみてください。これらの変数は整数であることに注意してください。 – quamrana

答えて

1

zy変数は整数であるので、それらは0になります両方あなたは女性の数を総学生数で割る結果は0と1の間です。整数型は整数だけを格納し、階乗部分を取り除きます。したがって、たとえば0.5のためにあなたが0に男性を設定してみてください、その後y可能性は0でこれを修正するために1

になるだろうとなり、あなたはfloatまたはdoubleとしてzyを設定する必要があります。

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; 
+0

あなたは私の奇怪な英雄です。なぜそれは私の心を滑ったのか分からない –

1

2つの整数を分割すると、Javaは小数部を切り捨てます。だから、もし4人の男の子と6人の女の子がいるなら、小数点以下を切り落とすと0になる4/10を思いつきます。

小数点以下を保持する場合は、数値の1つをintではなく2倍にする必要があります。これを行うための最も簡単な方法は、1.0で数字の1を乗じてある:

y = 1.0 * a/c;

これはあなたに* 4/10 = 4.0/10 = 0.4〜1.0を与えるだろう。

+0

良い答え。また、[[BigDecimal'](https://docs.oracle.com/javase/8/docs/api/java/math/BigDecimal.html)を使用すると、小数点以下の桁数が[浮動小数点型](https://en.wikipedia.org/wiki/Floating_point)の数値は、 'float' /' Float'や 'double' /' Double'などです。 –

関連する問題