The concept of streaming in Java

Source: Internet
Author: User
Tags readline stdin

Yesterday dormitory classmate asked me a question about Java flow, after the answer is very touching, I think all beginners may have such a problem, so summed up the issue, hehe. For your reference:

That, the classmate asks my question, mention actually I also did not listen to understand what meaning, because he expresses not to please, but one thing I was clear, is he wants from the keyboard input number (the console's kind), then calculates the rectangular area. Similar to this problem. Oh, in fact, this problem is very simple, in C + + only cin OK, but in Java, is not so simple, he would have used System.in.read (); But this function is returned in the form of int, but it is actually ASCII code, such as input 1, So the return is 51 (I can't remember the ASC code for 1), instead of 1, of course you can convert him to char but Char has no way int, oh, in short, very troublesome, I also can not tell, so I changed a method. So I changed a method, the method is as follows, I think probably a lot of new people are not very good, So give the source code as follows:

package test;
Import java.io.BufferedReader;
import java.io.IOException;
Import Java.io.InputStreamReader;
public class Testinout {
/**
* @param args
*/
public static void Main (string[] args) {
//T   Odo automatic generation Method stub
Test test=new Test ();
BufferedReader stdin = new BufferedReader (new InputStreamReader (system.in));
System.out.print ("Please enter the length of the rectangle:");    
try {
Test.seta (float.parsefloat (Stdin.readline ());
System.out.print ("Please enter the width of the rectangle:");
Test.setb (Float.parsefloat (Stdin.readline ()));
Test.setarea ();
System.out.print ("The area of the rectangle is:");
System.out.println (Test.getarea ());   
} catch (IOException e) {
//TODO automatically generates catch blocks
E.printstacktrace ();
 
}
}
class test{
Private float A;
private float B;  
Public Float Getb () {
return B;
 
public void Setb (float b) {
this.b = b;

public void SetA (float a) {
This.a=a;
 
Public Float Geta () {
return A;

Private float area;  
Public Float Getarea () {
return area;
 
public void Setarea () {
area=a*b;
}
}

Execution results:

Please enter the length of the rectangle: 3

Please enter the width of the rectangle: 4

The area of the rectangle is: 12.0

Related Article

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.