キーボードから入力したテストの結果から、合計・平均・順位(バブルソート)分散・標準偏差を導出するプログラムを作っています。
具体的には
生徒数を入力
↓
生徒数の応じた点数を入力
↓
使いたい機能を番号で選択
↓
結果の表示
みたいな感じです。
キーボード入力したテストの結果を配列化し、その配列を別のクラスで利用する方法を教えていただきたいです。現在の状況としては機能の選択まではスムーズに動作するのですが、結果の表示のフェイズで終了してしまいます。配列が引数として定義されていないのが原因なのでしょうか。
import java.io.*; class Main { public static void main(String[] args) throws IOException{ new Main(); } Main() { String ss=null; int t=0,j=0,ninzu=0,tmp=0; BufferedReader k=new BufferedReader(new InputStreamReader(System.in)); System.out.println("こんにちは!"); System.out.println("ここではテスト結果の合計・平均、順位、分散、標準偏差を算出できます。"); System.out.println("はじめに生徒数を教えてね。"); try{ //生徒数の確認 ss=k.readLine(); ninzu=Integer.parseInt(ss); }catch(IOException e) { System.out.println("error"); } System.out.println("人数分の点数を入力してね。"); //入力した結果を配列化する try{ for(int i=0;i<ninzu;i++){ ss=k.readLine(); int[] score=new int[ninzu];//scoreの配列を宣言 tmp=Integer.parseInt(ss); score[i]=tmp; } }catch(Exception e) { System.out.println("エラーが発生しました"); } System.out.println("次にどの機能を使うのか教えてね。"); System.out.println("合計・平均なら1、順位なら2、分散・標準偏差なら3と入力してね"); //機能の選択 try{ ss=k.readLine(); t=Integer.parseInt(ss); }catch(IOException e) { System.out.println("error"); } TestResult result =new TestResult(); while(true) { switch(t) { case 1: System.out.println("合計は"+result.getTotal()+","+"平均は"+result.getAverage()); break; case 2: System.out.println("順位は"+result.getSortedResult()); break; case 3: System.out.println("分散は"+result.getVar()+","+"標準偏差は"+result.getStddev()); break; } } } }
class TestResult { private static int[] result; private static int[] sortedResult; private static int total; private static double ave, var, stddev; public TestResult() { this.result = result; total = computeTotal(); ave = computeAverage(); var = computeVar(); stddev = computeStddev(); } public static int getTotal() { return total; } public static double getAverage() { return ave; } public static double getVar(){ return var; } public static double getStddev(){ return stddev; } public static int[] getResult(){ return result; } public static int[] getSortedResult(){ return sortedResult; } // Private Methods private static int computeTotal(int[] x) { for(int i=0;i<x.length;i++){ total=total+x[i]; } return total; } private static double computeAverage(int[] x) { for(int i=0;i<x.length;i++){ total=total+x[i]; } return (total/x.length); } private static double computeVar(int[] x){ for (int j=0; j<x.length; j++) { total = total+x[j]; } double e=0; ave=(total/x.length); for(int j=0; j<x.length; j++) { e= (ave-x[j])*(ave-x[j]); } var=(e/x.length); return var; } private static double computeStddev(int[] x){ double e=0; for (int j=0; j<x.length; j++) { total = total+x[j]; } ave=(total/x.length); for(int j=0; j<x.length; j++) { e= (ave-x[j])*(ave-x[j]); } var=(e/x.length); stddev=Math.sqrt(var); return stddev; } private static int[] sortedResult(int[] x){ int tmp=0; for(int j=x.length-1;j>0;j--){ for(int l=0;l<j;l++){ if(x[l]<x[l+1]){ tmp=x[l]; x[l]=x[l+1]; x[l+1]=tmp; } } } return sortedResult; } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/08/14 08:12