This question has already been answered:

Good day! Just started to learn JAVA, more precisely a new subject in Uni building informatics. So I got the first house. work

The bottom line is, you need to enter the variable int z = 5; then z divided by two and the answer is saved as a variable containing a real number (I assume this double ), but the answer still costs 2.0 instead of 2,5 . The task is to find a solution to this problem and bring the answer to the right and right kind. I really hope for your help!

 package anwendung; public class Project { public static void main(String[] args) { // TODO Auto-generated method stub // Aufgabe_1 // lokale Variablen int a = 5; int b = 23; //Berecnung von a+b System.out.println("a + b = " +(a + b)); //Berechnung b%a System.out.println("b % a= " + (a % b)); //Berechnung b/a System.out.println("b / a = " + (b/a)); //Berechnung a++ System.out.println("a++ = " + (a++)); //Berechnung b-- System.out.println("a * b = " + (a*b)); // Berechnung a+ System.out.println("a+ = b = " + (a+=b)); //Berechnung a*b++ System.out.println("a * b ++ = " + (a*b++)); //Berechnung a<=b System.out.println("a < + b =" + (a<=b)); //Berechnung a>b System.out.println("a > b =" + (a>b)); //Berechnung System.out.println("a == b =" + (a==b)); //Berechnung a!=b System.out.println("a ! = b =" + (a!=b)); //Aufgabe_2 //Aufgabe_3 //Neue Variable int c = 2147483647; System.out.println(" c + 1 = " + (c + 1)); // Variable "c" ist um 1 erhöht und Wert ist jetzt negativ, /* Wenn durch eine Operation der Wertebereich über- oder unterschritten wird, Programmiersprachen reagieren so dass, dass bei derAddition der Zahl 1 auf die größte positive Zahl des Wertebereichs die kleinste negative Zahl des Wertebereichs alsErgebnis ausrechnen */ //Aufgabe_4 int z = 5; double p = z / 2; System.out.println(p); } } 

Reported as a duplicate by participants Alexey Shimansky , post_zeew , Community Spirit October 25 '16 at 18:19 .

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 .

  • one
    double p = z / 2.0; - Alexey Shimansky
  • @Schweigert Vitalij From your question it is not clear what result you want to get: either 2.0, or 2.5. - Vlad from Moscow
  • I need to answer 2.5. But to achieve this, I can not. , - Schweigert Vitalij
  • Alexey, thank you very much! You helped me a lot! Have a good mood! - Schweigert Vitalij

2 answers 2

In this sentence with the declaration of a double type variable p

 double p = z / 2; 

integer expression is used as initializer

 z / 2 

since both operands of the operator / have an integer type. Therefore, as a result, you will get that the variable z will have the value 2.0 .

If you want this expression to be evaluated as a floating point expression, then at least one of the operands must be of type double

For example,

 double p = z / 2.0; 

or

 double p = z / 2d; 

Keep in mind that your program has typos, such as the following

 System.out.println("b % a= " + (a % b)); ^^^^^ 

Must be

 System.out.println("b % a= " + (b % a)); ^^^^^ 

Or between some operators, which consist of two characters, like, for example, this != , You insert a space, which can lead to a compilation error.

  • Vlad from Moscow thank you very much! Happiness to you! - Schweigert Vitalij
  • @SchweigertVitalij Not at all. Mark the best answer from your point of view, and your question will be answered. - Vlad from Moscow

You have integer division here.

When dividing integer a by integer b result is rounded down to an integer.

To get a floating-point value, you can explicitly convert one of the operands to a floating-point type, for example:

 double p = (double) z / 2; 

or so:

 double p = z / (double) 2; 

or like this:

 double p = z / 2.0;