Raphael Eid
Raphael Eid

Reputation: 21

variable "double" giving me error, and program is doing infinte loop?

I need to ask the user how many times he want to put his notes, then do a loop how many times he need to put his note and finally calculate the moyenne, but im putting that double a = a+n; means that it calculate the notes number, and finally in s.o.p, im putting to divide the notes number on how much he asked first. java is giving me error, any help?

Here is my code:

package minmax;

import java.util.Scanner;

public class MinMax {

  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    int x, y, z;
    System.out.println("Combien de notes vous avez? ");
    x = in .nextInt();

    for (y = 0; y < x; y++) {
      do {
        System.out.println("Mettez votre note :");
        z = in .nextInt();
      }
      while (z < 20 || z > 0); {
        double a = a + n;
      }
    }

    System.out.println("Votre moyenne est : " + (a / x));
  }
}

Upvotes: 0

Views: 61

Answers (2)

tevemadar
tevemadar

Reputation: 13195

Fixes and suggestions:

  1. Variables have to be initialized first (strictly without referring themselves), "updates" (referring themselves, the a=a+something, a++, a+=something kind of things) can happen only afterwards
  2. In Java you usually bring variable declaration and usage close to each other, and remember that you can declare and initialize a variable in a single statement.
  3. In the case of using a do-while loop, do not bracket+indent lines following the while(...);, such following lines are on the same level as the do-while loop itself
  4. Whitespace preceding . looks strange, unless you break an expression into several lines

Put them together:

package minmax;

import java.util.Scanner;

public class MinMax {

  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    System.out.println("Combien de notes vous avez? ");
    int x = in.nextInt();

    double a = 0;

    for (int y = 0; y < x; y++) {
      int z;
      do {
        System.out.println("Mettez votre note :");
        z = in.nextInt();
      }
      while (z < 0 || z > 20);
      a = a + z;
    }

    System.out.println("Votre moyenne est : " + (a / x));
  }
}

(Plus a=a+n became a=a+z for the obvious reason of z containing the number from the user, and the comparison directions had to be swapped - assuming that you want numbers between 0...20)

Upvotes: 1

GhostCat
GhostCat

Reputation: 140427

Simple:

double a = a + n;

You can't define a variable and initialize it with itself.

Meaning: it is not possible to declare a, but also assign a value to a that requires a.

In other words: the code you wrote really makes no sense. Maybe you should simply put: double a = 0 somewhere above that statement, and then only do: a = a + n further down.

And of course: also use real names. a, n, those names mean nothing. Use something that tells the human reader about the intent of these variables.

Upvotes: 1

Related Questions