This question has already been answered:

There is a program with inheritance, consisting of Main, Skelet, Podklass. The program itself is very cumbersome, so I throw part of it.

There are 2 students. Displays an array of their names. Connect the Scanner, enter any of these names. For the selected student both his marks and the average score are displayed. Here only the average score returns the value rounded to an integer. How to make rounding at least to the tenth? I can not understand where the error, even though the rail and a lot.

Skeleton:

public class Skelet { String im; int ocenka1; int ocenka2; double SrBall; public Skelet(String im, int ocenka1, int ocenka2, double srBall) { this.im = im; this.ocenka1 = ocenka1; this.ocenka2 = ocenka2; this.SrBall = srBall; } public String getIm() { return im; } public int getOcenka1() { return ocenka1; } public int getOcenka2() { return ocenka2; } public double getSrBall() { return SrBall = (ocenka1 + ocenka2)/2; } } 

Subclass:

 public class Podklass extends Skelet { public Podklass(String im, int ocenka1, int ocenka2, double SrBall) { super(im, ocenka1, ocenka2, SrBall); } } 

Main:

 import java.util.Scanner; public class Main { public static void main (String[] args) { int i; double sb=0; Podklass uch1 = new Podklass("Π”ΠΌΠΈΡ‚Ρ€ΠΈΠΉ", 4,3, sb); Podklass uch2 = new Podklass("Максим", 5,4, sb); String[] mass = {uch1.getIm(), uch2.getIm()}; for (i=0; i<mass.length; i++){ System.out.println(mass[i]); // Π’Ρ‹Π²ΠΎΠ΄ΠΈΠΌ массив ΠΈΠ· ΠΈΠΌΠ΅Π½ } Scanner in = new Scanner(System.in); do { System.out.println(""); System.out.println("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ имя ΡƒΡ‡Π΅Π½ΠΈΠΊΠ°: "); String name = in.nextLine(); for (i = 0; i < mass.length; i++) { if (name.equals(mass[0])) { System.out.println(uch1.getIm() + " " + uch1.getOcenka1() + " " + uch1.getOcenka2() + " " + uch1.getSrBall()); break; } if (name.equals(mass[1])) { System.out.println(uch2.getIm() + " " + uch2.getOcenka1() + " " + uch2.getOcenka2() + " " + uch2.getSrBall()); break; } else { break; } } } while (mass.length==2); } } 

Reported as a duplicate by members of Artem Konovalov , Akina , temq , user194374, Denis Bubnov 10 Feb '17 at 6:45 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

    1 answer 1

    In this code snippet:

     (ocenka1 + ocenka2)/2 

    all operands are integer , so integer division takes place here. The result of integer division is an integer.

    If as a result of integer division a floating-point number is obtained, truncation of the private occurs - the fractional part is discarded.

    You can solve the problem by explicit type casting, for example, like this:

     (double) (ocenka1 + ocenka2)/2 

    or so:

     (ocenka1 + ocenka2)/(double) 2 

    or so:

     (ocenka1 + ocenka2)/2d 

    or in this way:

     (ocenka1 + ocenka2)/2. 

    By the way, the double type is redundant here, in this situation a float is enough.