Home > Back-end >  Code optimization problem bosses look at 69.8 59.8 69.8 why using the encapsulation is equal to 199.
Code optimization problem bosses look at 69.8 59.8 69.8 why using the encapsulation is equal to 199.

Time:10-16

The class Student
{
Runs Backstage information=new runs Backstage ();//create a background search system
Void Access (String mation)//query information
{
Information. Cooking (mation);//inform mation query information
}
Void SaySorry ()
{
System. The out. Println (" buy record was not found ");
}
Public static void main (String [] args)
{
Student Reception=new Student ();
System. The out. Println (" commodity information in your shopping cart: ");
Reception. Access (" \ n ");//query information below
}
The class runs Backstage//background
{
String name;//set the variable
The String the user;
String monkey;
String jiage;
Runs Backstage defining variables ()//
{
This. Name="title:";
This. The user="author:";
This. Monkey="price:";
Enclosing jiage="total price";
}
Void cooking (String mation)//information
{
System. Out. Println (name + "" Java Web from entry to the master," "+" Java from entry to the master (third edition), "+" Java from entry to master (example), "+ mation);
System. Out. Println (user + "tomorrow's technology with" + "tomorrow's technology with" + "the future" + mation);
System. Out. Println (monkey + "69.8", "59.8" + "69.8" + mation);
System. The out. Println (jiage + (69.8 + 59.8 + 69.8) + mation);
}
}
}

The result of the running out was

Your shopping cart commodity information:
Title: "Java Web from entry to the master," Java from entry to the master (third edition), the Java from entry to master (instance),

Author: tomorrow's technology with tomorrow's technology with tomorrow's technology with

Price: 69.859.869.8

A total of 199.39999999999998
Two question 1: can use HashSet second: why is equal to 199.39999999999998 trouble

CodePudding user response:

Floating point Numbers using the binary system

CodePudding user response:

First problem: HashSet want to use it with no limit of you here is calculating the output directly to there is no need to use HashSet
Second question: has the precision of numerical calculation such as float and double types may be loss of accuracy will cause do you have the precision of the results of the above general advice first turn BigDecimal type calculation
BigDecimal shu1=new BigDecimal (" 69.8 ");
BigDecimal shu2=new BigDecimal (" 59.8 ");
BigDecimal shu3=new BigDecimal (" 69.8 ");
Shu1=shu1. Add (shu2). The add (shu3);
System. The out. Println (shu1 doubleValue ());

CodePudding user response:

You sure can't use this value type string playing, use BigDecimal, the somebody else all, packaging good mathematical calculation method, the first question is whatever you use

CodePudding user response:

The bosses how you look at the whole in the code is modified??

CodePudding user response:

refer to the second floor meet_zzs response:
first question: HashSet want to use it with no limit of you here is calculating the output directly to there is no need to use HashSet
Second question: has the precision of numerical calculation such as float and double types may be loss of accuracy will cause do you have the precision of the results of the above general advice first turn BigDecimal type calculation
BigDecimal shu1=new BigDecimal (" 69.8 ");
BigDecimal shu2=new BigDecimal (" 59.8 ");
BigDecimal shu3=new BigDecimal (" 69.8 ");
Shu1=shu1. Add (shu2). The add (shu3);
System. The out. Println (shu1 doubleValue ());

Okay, I'll go to try
  • Related