A solution to the input number of the console in Java

Source: Internet
Author: User
Solution | control

Console input has a System.in.read () in Java, but unlike Console.ReadLine () in C #, I don't know why, even if I use coercion type conversion in C #, I can't get the number I entered. That's because Java will enter the key to enter.

But there are solutions, and here's a small example of how to convert a gallon to a liter:

/*
* This is converte gallons to liters, users can input numbers.
*
* Call this program Galtolit2.java
*/
Import java.io.*;

Class GalToLit2
{
public static void Main (String args[])
Throws Java.io.IOException//use of System.in.read () required
{
double gallons;
Double liters;

SYSTEM.OUT.PRINTLN ("Change gallon to Liter");
The following is the processing of the input characters
BufferedReader in = new BufferedReader (new InputStreamReader (system.in));
System.out.print ("Please input gallons:");
gallons = double.parsedouble (In.readline ());

liters = gallons * 3.7854;

SYSTEM.OUT.PRINTLN (gallons + "gallons is" + liters + "liters.");
}
}




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.