2016.03.29///java Study Record ②

Source: Internet
Author: User

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/> title: How many meters to convert the height of 5 feet 7 inches (5+7/ *0.3048)


Read the title after the code, named ①, error:

package  height 5 feet 7 inches; import java.util.scanner;public class   Height 5 ft 7 inch  {public static void main (String[] args)  {scanner in =  new scanner (system.in); System.out.printf ("Please enter height ruler:");  string chi = in.nextline ();                             system.out.printf ("Please enter height inch:");  string cun = in.nextline (); float  ShenGao; shengao =  (float)   (( chi + cun / 12)  * 0.3048); System.out.println ("The person is tall as"  + shengao  + "M");}} 

Error:CUN/12 The operator/is undefined for the argument type (s) String, int


After consultation with the classmate will ① modify, named ②,② correct:

Package Height 5 ' 7 "Import java.util.scanner;public class height 5 ft 7 inch {public static void main (string[] args) {Scanner in = new Scan NER (system.in);                           System.out.printf ("Please enter height ruler:"); int Chi = In.nextint (); System.out.printf ("Please enter height inch:"); float Cun = In.nextfloat (); float Shengao; Shengao = (float) ((Chi + CUN/12) * 0.3048); System.out.println ("The person is tall" + Shengao + "M");}}

Please enter your height ruler: 5

Please enter the height of the inch: 7

The person is 1.7018001m tall.


Remove "float Shengao", simplify ②, name ③, correct:

Package Height 5 ' 7 "Import java.util.scanner;public class height 5 ft 7 inch {public static void main (string[] args) {Scanner in = new Scan NER (system.in);                           System.out.printf ("Please enter height ruler:"); int Chi = In.nextint (); System.out.printf ("Please enter height inch:"); float Cun = In.nextfloat (); System.out.println ("The person is Tall" + ((Chi + CUN/12) * 0.3048) + "M");}}

Please enter your height ruler: 5

Please enter the height of the inch: 7

The person is 1.7018000484466553m tall.


After watching the video (programming Primer-java Language (Onge)) will ③ modified, named ④, correct:

Package Height 5 ' 7 "Import java.util.scanner;public class height 5 ft 7 inch {public static void main (string[] args) {Scanner in = new Scan NER (system.in);                           System.out.printf ("Please enter height ruler:"); int Chi = In.nextint (); System.out.printf ("Please enter height inch:"); int Cun = In.nextint (); System.out.println ("The person is Tall" + ((Chi + cun/12.0) * 0.3048) + "M");}}

Please enter your height ruler: 5

Please enter the height of the inch: 7

The person is 1.7018m tall.



There is a video to see another code error, named ⑤:

Package Height 5 ' 7 "Import java.util.scanner;public class height 5 ft 7 inch {public static void main (string[] args) {Scanner in = new Scan NER (system.in);                           System.out.printf ("Please enter height ruler:"); int Chi = In.nextint (); System.out.printf ("Please enter height inch:"); int Cun = In.nextint (); System.out.println ("The person is Tall" + ((Chi + CUN/12) * 0.3048) + "M");}}

Please enter your height ruler: 5

Please enter the height of the inch: 7

The person is 1.524m tall.

"CUN/12" is an integer except for integers, Cun is less than 12, the result is fraction a few, because the int type is displayed, so zero.



②③④ is correct because one of the "CUN/12" two numbers is converted to a floating-point type, which is no longer an integral type except an integral type.

When a floating-point number is combined with an integer, Java converts the integer to a floating-point number and then the operation of the floating-point number.


What is the difference between double and float "from Baidu":
For programmers, the difference between double and float is a double with a high precision, a valid number of 16 bits, and a float precision of 7 bits. But double consumes twice times the memory is float, double operation speed is much slower than float, C language math function name double and float different, do not write wrong, can use single precision do not use double precision (to save memory, speed up the operation speed)


So the question comes, why ② final result results in 8 digits after the decimal point, ③ The final result of 16 digits after the decimal point, ④ the final result only 4 digits after the decimal point ? I'm still studying .



This article from "Parasol Ya" blog, reproduced please contact the author!

2016.03.29///java Study Record ②

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.