本文实例为大家分享了java实现图形卡片排序游戏的具体代码,供大家参考,具体内容如下
掌握类的继承、多态性使用方法以及接口的应用。
输入格式:
首先,在一行上输入一串数字(1~4,整数),其中,1代表圆形卡片,2代表矩形卡片,3代表三角形卡片,4代表梯形卡片。各数字之间以一个或多个空格分隔,以“0”结束。例如: 1 3 4 2 1 3 4 2 1 3 0
然后根据第一行数字所代表的卡片图形类型,依次输入各图形的相关参数,例如:圆形卡片需要输入圆的半径,矩形卡片需要输入矩形的宽和长,三角形卡片需要输入三角形的三条边长,梯形需要输入梯形的上底、下底以及高。各数据之间用一个或多个空格分隔。
输出格式:
如果图形数量非法(小于0)或图形属性值非法(数值小于0以及三角形三边不能组成三角形),则输出Wrong Format。
如果输入合法,则正常输出,所有数值计算后均保留小数点后两位即可。输出内容如下:
排序前的各图形类型及面积,格式为图形名称1:面积值1图形名称2:面积值2 …图形名称n:面积值n,注意,各图形输出之间用空格分开,且输出最后存在一个用于分隔的空格;
排序后的各图形类型及面积,格式同排序前的输出;
所有图形的面积总和,格式为Sum of area:总面积值。
输入样例1:
在这里给出一组输入。例如:
1 5 3 2 0
输出样例1:
在这里给出相应的输出。例如:
Wrong Format
输入样例2:
在这里给出一组输入。例如:
4 2 1 3 0
3.2 2.5 0.4 2.3 1.4 5.6 2.3 4.2 3.5
输出样例2:
在这里给出相应的输出。例如:
The original list:
Trapezoid:1.14 Rectangle:3.22 Circle:98.52 Triangle:4.02
The sorted list:
Circle:98.52 Triangle:4.02 Rectangle:3.22 Trapezoid:1.14
Sum of area:106.91
输入样例3:
在这里给出一组输入。例如:
4 2 1 3 0
3.2 2.5 0.4 2.3 1.4 5.6 2.3 4.2 8.4
输出样例3:
在这里给出相应的输出。例如:
Wrong Format
参考代码如下:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Main {
//在Main类中定义一个静态Scanner对象,这样在其它类中如果想要使用该对象进行输入,则直接
//使用Main.input.next…即可(避免采坑)
public static Scanner input = new Scanner(System.in);
public static void main(String[] args){
ArrayList list = new ArrayList();
int num = input.nextInt();
while(num != 0){
if(num < 0 || num > 4){
System.out.println("Wrong Format");
System.exit(0);
}
list.add(num);
num = input.nextInt();
}
DealCardList dealCardList = new DealCardList(list);
if(!dealCardList.validate()){
System.out.println("Wrong Format");
System.exit(0);
}
dealCardList.showResult();
input.close();
}
}
class Card{
Shape shape;
Card(){
}
Card(Shape shape){
this.shape=shape;
}
public Shape getShape() {
return shape;
}
public void setShape(Shape Shape) {
this.shape=shape;
}
}
class DealCardList{
ArrayList cardList=new ArrayList();
DealCardList(){
}
DealCardList(ArrayList list){
for(int i=0;i
{
if(list.get(i)==1)
{
double r=Main.input.nextDouble();
Circle circle=new Circle(r);
Card card=new Card(circle);
card.getShape().setShapeName("Circle");
cardList.add(card);
}
if(list.get(i)==2) {
double a=Main.input.nextDouble();
double b=Main.input.nextDouble();
Rectangle rectangle=new Rectangle(a,b);
Card card=new Card(rectangle);
card.getShape().setShapeName("Rectangle");
cardList.add(card);
}
if(list.get(i)==3) {
double a=Main.input.nextDouble();
double b=Main.input.nextDouble();
double c=Main.input.nextDouble();
Triangle triangle=new Triangle(a,b,c);
Card card=new Card(triangle);
card.getShape().setShapeName("Triangle");
cardList.add(card);
}
if(list.get(i)==4) {
double a=Main.input.nextDouble();
double b=Main.input.nextDouble();
double c=Main.input.nextDouble();
Traperoid traperoid=new Traperoid(a,b,c);
Card card=new Card(traperoid);
card.getShape().setShapeName("Trapezoid");
cardList.add(card);
}
}
}
public boolean validate() {
for(int i=0;i
{if(!cardList.get(i).getShape().vaildate())
return false;}
return true;
}
public void cardSort() {
for(int k=0;k
for(int i=k+1;i
{
if(cardList.get(k).getShape().getArea()
Collections.swap(cardList, k, i);
}
}
public double getAllArea() {
double s=0;
for(int i=0;i
s=s+cardList.get(i).getShape().getArea();
return s;
}
public void showResult() {
System.out.println("The original list:");
for(int i=0;i
System.out.print(cardList.get(i).getShape().getShapeName()+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" ");
System.out.println();
System.out.println("The sorted list:");
cardSort();
for(int i=0;i
System.out.print(cardList.get(i).getShape().getShapeName()+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" ");
System.out.println();
System.out.println("Sum of area:"+String.format("%.2f",getAllArea()));
}
}
class Shape {
private String shapeName;
Shape(){
}
Shape(String shapeName){
this.shapeName=shapeName;
}
public String getShapeName() {
return shapeName;
}
public void setShapeName(String shapeName) {
this.shapeName=shapeName;
}
public double getArea() {
return 0.0;
}
public boolean vaildate() {
return true;
}
}
class Circle extends Shape{
private double radius;
Circle(){
}
Circle(double radius){
this.radius=radius;
}
public double getArea() {
return Math.PI*radius*radius;
}
public boolean vaildate() {
if(radius>0)
return true;
else return false;
}
}
class Rectangle extends Shape{
private double width,length;
Rectangle (double width,double length){
this.width=width;
this.length=length;
}
public double getArea() {
return width*length;
}
public boolean vaildate() {
if(width>0&&length>0)
return true;
else return false;
}
}
class Triangle extends Shape{
double side1,side2,side3;
Triangle(double side1,double side2,double side3){
this.side1=side1;
this.side2=side2;
this.side3=side3;
}
public double getArea() {
double c=(side1+side2+side3)/2;
double s=Math.sqrt(c*(c-side1)*(c-side2)*(c-side3));
return s;
}
public boolean vaildate() {
if(side1+side2>side3&&side1+side3>side2&&side2+side3>side1)
return true;
else
return false;
}
}
class Traperoid extends Shape{
private double topSide,bottomSide,height;
Traperoid(){
}
Traperoid(double topSide,double bottomSide,double height){
this.bottomSide=bottomSide;
this.height=height;
this.topSide=topSide;
}
public double getArea() {
return (topSide+bottomSide)*height/2;
}
public boolean validate() {
if(topSide>0&&bottomSide>0&&height>0)
return true;
else return false;
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持免费资源网。