package?com.imooc;
public?abstract?class?Shape?{
???public?abstract?void?perimeter();//求周長
???public?abstract?void?area();//求面積
???
}
package?com.imooc;
import?java.util.Scanner;
public?class?Rectangle?extends?Shape?{
????
???
????
public?void?perimeter()?{
Scanner?input?=?new?Scanner(System.in);
System.out.println("請輸入矩形的寬:");
float?width?=?input.nextFloat();
System.out.println("請輸入矩形的長:");
float?length=input.nextFloat();
System.out.println("矩形的周長為:"+2*(width+length));
}
public?void?area()?{
Scanner?input?=?new?Scanner(System.in);
System.out.println("請輸入矩形的寬:");
float?width=input.nextFloat();
System.out.println("請輸入矩形的長:");
float?length=input.nextFloat();
?System.out.println("矩形的面積為:"+width*length);
}
}
package?com.imooc;
import?java.util.Scanner;
public?class?Circle?extends?Shape?{
public?void?perimeter()?{
Scanner?input?=?new?Scanner(System.in);
System.out.println("請輸入圓形的半徑:");
float?radius=input.nextFloat();
System.out.println("圓形的周長為:"+radius*2*3.14);
}
public?void?area()?{
Scanner?input?=?new?Scanner(System.in);
System.out.println("請輸入圓形的半徑:");
float?radius=input.nextFloat();
System.out.println("圓形的面積:"+3.14*radius*radius);?
}
}
package?com.imooc;
public?class?Initial?{
public?static?void?main(String[]?args)?{
Shape?s1=new?Rectangle();
s1.perimeter();
s1.area();
Shape?s2=new?Circle();
s2.perimeter();
s2.area();
}
}
2016-06-02
可以使用一個方法簡寫直接輸出周長和面積
public abstract class Shape {
? public abstract void get();//求周長和面積
}
import java.util.Scanner;
public class Rectangle extends Shape { ? ??
? ? public void get() {
? ? ? ? Scanner input = new Scanner(System.in);
? ? ? ? System.out.println("請輸入矩形的寬:");
? ? ? ? float width = input.nextFloat();
? ? ? ? System.out.println("請輸入矩形的長:");
? ? ? ? float length=input.nextFloat();
? ? ? ? System.out.println("矩形的周長為:"+2*(width+length));
? ? ? ? System.out.println("矩形的面積為:"+width*length);
?
? ? }
}
import java.util.Scanner;
public class Circle extends Shape {
? ? ?? ?public void get() {
? ? ? ? Scanner input = new Scanner(System.in);
? ? ? ? System.out.println("請輸入圓形的半徑:");
? ? ? ? float radius=input.nextFloat();
? ? ? ? System.out.println("圓形的周長為:"+radius*2*3.14);
? ? ? ? System.out.println("圓形的面積:"+3.14*radius*radius);
? ? }
}
public class Initial {?
? ? public static void main(String[] args) {
? ? ? ? Shape s1=new Rectangle();
? ? ? ? s1.get(); ? ? ? ? ? ? ? ? ? ? ??
? ? ? ? Shape s2=new Circle();
? ? ? ? s2.get();?
? ? }
}
2016-06-06
哥們感覺你少了input.close();這個語句,沒有這個語句會報錯在我這
2016-06-06
為什么我打?public class Rectangle extends Shape ?這個代碼時系統(tǒng)開始報錯啊