Write no more than two lines of code to read in a primary of integer from the keyboard and store it in an integer variable count assume all import statement have been properly stated in java

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
icon
Concept explainers
Question
Write no more than two lines of code to read in a primary of integer from the keyboard and store it in an integer variable count assume all import statement have been properly stated in java
Expert Solution
Step 1

float and int are 32-digit wide information types, the float has a higher reach than the whole number crude worth. Since a float is greater than int, you can change a float over to an int by essentially down-projecting it for example (int) 4.0f will give you whole number 4. Incidentally, you should recall that pigeonholing simply dispose of anything after the decimal point, they don't play out any adjusting or ground surface procedure on the worth. So assuming your float esteem is 3.999, down projecting to a number will deliver 3, not 4. In the event that you really want adjusting, think about utilizing the Math.round() technique, which converts float to its closest number by adding +0.5 and afterward shortening it.

Incidentally, Math.random() is over-burden for both float and twofold, so you can involve this for changing twofold over to long also. We should see an instance of changing a float esteem over to int in Java.

float to int utilizing type projecting
Assuming that your necessity is to change over a drifting point number to a number simply by disposing of those fragmentary qualities after the decimal point then, at that point, down-projecting is the most effective way to get it done. It's basic, productive and exquisite, Here is an instance of projecting float to int in Java:
int esteem = (int) 3.14f;
int score = (int) 3.99f;

 float to int utilizing Math.round()
This is the correct method for changing float over to int on the off chance that you are keen on adjusting before transformation. Math.round() changes over a drifting point number to the closest whole number by first adding 0.5 and afterward shortening esteem after the decimal point.
int a = Math.round(3.14f);
int b = Math.round(3.99f);
int c = Math.round(3.5f);

 

 

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Control Structure
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education