Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Durchschnitt berechnen

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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 +".");
    }
    }

  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default 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.

  3. #3
    Member
    Join Date
    Oct 2013
    Location
    Saint-Petersburg, Russia
    Posts
    33
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default 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.