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