# Durchschnitt berechnen

• November 2nd, 2013, 10:25 AM
Anais
Durchschnitt berechnen
Ich bin Programmiereinsteigerin und habe mittels eines Tutorials einen Code geschrieben, um den Durschnitt verschiedener Zahlen zu berechnen (Ich verwende Eclipse). Der Code funktioniert, aber es ist nicht möglich den Durschnitt von Dezimalzahlen zu berechnen. Ich wäre sehr froh, wenn jemand Tipps hätte.

Dies ist der Code:

public class main_class {
public static void main(String[] args){
String sentinel = "";
int sum = 0;
int counter = 0;
double mean = 0.0;
Scanner NumScanner = new Scanner(System.in);

System.out.println("Enter numbers to add. Enter \"d\" when done.");

System.out.print("Enter number: ");
sentinel = NumScanner.next();
System.out.println();

while(!sentinel.equals("d") && !sentinel.equals("D")) {
sum += Integer.parseInt(sentinel);
counter++;

System.out.print("Enter number: ");
sentinel = NumScanner.next();
System.out.println();
}

mean = (sum*1.0)/counter;

System.out.println();
System.out.println("The arithmetic mean is: " + mean +".");
}
}
• November 2nd, 2013, 10:39 AM
GregBrannon
Re: Durchschnitt berechnen
Please post your code in code tags and your description of the problem in English. Google translate? Read the Announcement topic at the top of the sub-forum for code posting instructions and other useful hints. If you're getting errors (it doesn't sound like it), post those too.
• November 3rd, 2013, 02:06 AM
rodiongork
Re: Durchschnitt berechnen
Surely you can't process floating point values with "Integer.parseInt" - replace it with "Double.parseDouble", a takzhe nujno zamenit vse peremennie tipa "int" na peremennie tipa "double".

I stoit obratit vnimanie chto texty na lubih yazikah otlichnih ot anglijskogo zatrudnitelny dlia chtenia.

By the way it looks you have stolen your code from here:
How to Write a Program in Java to Calculate the Mean - wikiHow

I recommend you to write your codes completely yourself if you want to learn anything at all.