简体   繁体   中英

New to programming and doubles wont add

I can't seem to get the double values to add what am i doing wrong?

import java.util.Scanner;

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

     //Values
     System.out.println("Enter the two numbers");
     double R = input.nextDouble();
     double T = input.nextDouble();

     //Calculate sum
     double sum = (R + T);

     //Calculate difference
     double difference = (R - T);

     //Calculate product
     double product = (R * T);

     //Display results
     System.out.println("The sum of R and T=" +R + +T);
     System.out.println("The difference of R and T=" +R - +T);
     System.out.println("The product of R and T=" +R * +T);
   }
} `

It is telling me the sum is 5.05.0 when i add 5 as the numbers and it says "-" isnt a valid operation when I try to subtract. I'm extremely new so try to take it easy on me

thanks

This can be a little bit trick, but basically, Java is converting each value to it's String representation and then adding them together as a single String

If R = 5 and T = 1 , then

System.out.println("The sum of R and T=" +R + +T);

Would output something like...

The sum of R and T=51

Instead, you need to wrap the calculation within a precedence group first, to allow them to be processed before been converted to a String , for example...

System.out.println("The sum of R and T=" + (R + T));

Which will not output something more like...

The sum of R and T=6

The same groups for your other statements...

System.out.println("The difference of R and T=" + (R - T));
System.out.println("The product of R and T=" + (R * T));

The brackets allow ensure that the result of the operation is carried out first, before it is applied as a whole...

That is for "The sum of R and T=" + (R + T) Java will resovle the R + T first then concatenrate that result to the preceding String value forming the final result...

It's concatenating the values to the output string. No calculation is performed.

If you want the calculation to be performed, you have to surround it with parentheses:

System.out.println("The sum of R and T=" + (R + T));

The problem is that you are adding them to a String. Java sees it as:

System.out.println( ( "The sum of R and T=" +R ) + +T);

When appending a result of a calculation to a string, you should manually use parenthesis to correct the order:

System.out.println("The sum of R and T=" + (R + +T));
System.out.println("The difference of R and T=" + (R - +T)); 
System.out.println("The product of R and T=" + (R * +T));

Additionally, the error occurs because this is not a valid operation:

"The difference of R and T=5.0" - 5.0

Also, firstly, you don't have to use the + in front of values in order to make them positive. And if you want to do that, your first + is actually consumed in the concatenation:

System.out.println("The sum of R and T=" + (+R + +T));

The compiler thinks that you are trying to combine Strings when you try to add the doubles. What you need to do is put parenthesis around the armithetic so it know that it is arthmetic. Like this:

//Display results
System.out.println("The sum of R and T=" + (R + T));
System.out.println("The difference of R and T=" + (R - T));
System.out.println("The product of R and T=" + (R * T));

The plus between the parenthesis and the main String tells the compiler to add the result of the arthemtic as a String to the String. You also don't need to put plus signs in front of positive numbers. The compiler assumes that variables without signs are positive.

This is because you are not actually doing the math the + sign is just acting as string concatenation

The reason you are getting the sum as 5.05.0 is because it is just adding the String Values of R and T. For the next two - and * are not defined for type String

Instead of what you have tried you should use following to calculate values

 double R=5.0;
 double T=2.0;
 System.out.println("The sum of R and T=" +(R + T));
 System.out.println("The difference of R and T=" +(R - T));
 System.out.println("The product of R and T=" +(R * T));

Output:

 The sum of R and T=7.0
 The difference of R and T=3.0
 The product of R and T=10.0

It is just concatenating the values to the output string. You might try storing the result in a variable and then print that variable, though it does not mean that you cannot perform calculation while printing them. Try performing the operation in parenthesis.

Please add more detail while asking questions.

To simplify all the answers here. System.out.println("The sum of R and T=" +sum); Since you are either way saving the sum to a variable. Replace in others too. Your error was becuase the compiler mistook your variables for strings where + meant concatenation but - is invalid.

Here is a working solution with minimum changes in your code. Also as others pointed out when you do anything like String(x) + (some other object) java calls toString method on the later object and then concatenates that with String x. import java.util.Scanner;

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

     //Values
     System.out.println("Enter the two numbers");
     double R = input.nextDouble();
     double T = input.nextDouble();

     //Calculate sum
     double sum = (R + T);

     //Calculate difference
     double difference = (R - T);

     //Calculate product
     double product = (R * T);

     //Display results
     System.out.println("The sum of R and T=" + sum);
     System.out.println("The difference of R and T=" + difference);
     System.out.println("The product of R and T=" + product);
   }
} `

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM