Code:
import java.io.*;
import java.lang.*;
import java.math.*;
import java.text.*;
public class Election {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
if (args.length != 6) {
System.out.println("Bitte geben Sie die Stimmen von 6 Parteien an!! Programm terminiert");
System.exit(1);
}
String EingabeWert;
int GesamtZahl = 0, x = 0, y = 0;
int[] WerteArray = new int[6];
double[] ProzeArray = new double[6];;
// TODO code application logic here
System.out.println("Eingabeauswertung hat begonnen");
for (x = 0; x < WerteArray.length; x++) {
EingabeWert = args[x];
if (Kontrolle(EingabeWert) == false) {
System.exit(1);
}
WerteArray[x] = Integer.parseInt(args[x]);
GesamtZahl += WerteArray[x];
}
for (x = 0; x < ProzeArray.length; x++) {
//MakroAufruf
ProzeArray[x] = roundEvenexp2((WerteArray[x] * 100.00 ) / GesamtZahl);
//Debugzeile
System.out.println(ProzeArray[x]);
}
}
//RoundMakro
static double roundEvenExp2 (double d) {
return round (d, 2, HALF_EVEN, FormatType.exp);
}
/**
* @param d der zu rundende Gleitkommawert.
* @param scale die Anzahl der Nachkommastellen, falls type = fix,
* die Anzahl der tragenden Stellen - 1, falls type = exp.
* scale sollte >= 0 sein (negative Werte werden auf 0 gesetzt).
* @param mode die Rundungsart: einer der Rundungsarten von BigDecimal,
* seit 1.5 in java.math.RoundingMode.
* @param type ein Element von "enum FormatType {fix, exp}" gibt an,
* auf welche Stellen sich die Rundung beziehen soll.
* FormatType.exp ('Exponential') steht für tragende Stellen,
* FormatType.fix ('Fixkomma') steht für Nachkommastellen.
* @return der gerundete Gleitkommawert.
*
Anmerkung: Für die Werte <tt>double</tt> NaN und &Infinity
* liefert round den Eingabewert unverändert zurück.
*/
public static double round (double d, int scale, RoundingMode mode, FormatType type) {
if (Double.isNaN(d) || Double.isInfinite(d)) return d;
scale = Math.max(scale,0); // Verhindert negative scale-Werte
BigDecimal bd = BigDecimal.valueOf(d);
if (type == FormatType.exp) {
BigDecimal bc = new BigDecimal(bd.unscaledValue(),bd.precision()-1);
return ((bc.setScale(scale, mode)).
scaleByPowerOfTen(bc.scale()-bd.scale())).doubleValue();
}
return (bd.setScale(scale, mode)).doubleValue();
}
/**
* Wenn die mitgelieferten Parameter nicht einer Zahl entsprechen,
* dann endet das Programm normal durch try/ catch Anweisungsblöcke
*/
static boolean Kontrolle (String A) {
int i;
try
{
i = Integer.parseInt(A);
if (i >= 0) return true;
else return false;
}
catch (Exception e) {
System.out.println("Wert " +A +" wurde nicht akzeptiert");
return false;
}
}
}
Kann mir jemand sagen warum ich im Compiler immer nur cant´t find FormatType, rountEvenexp2 oder HALF_EVEN lesen muss und warum es nicht tut. Gibts kleinere Methoden zum runden auf 2 Nachkommastellen.