欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java實現(xiàn)圖形卡片排序游戲

 更新時間:2020年07月20日 08:40:58   作者:Chen大強(qiáng)  
這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)圖形卡片排序游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了java實現(xiàn)圖形卡片排序游戲的具體代碼,供大家參考,具體內(nèi)容如下

掌握類的繼承、多態(tài)性使用方法以及接口的應(yīng)用。
輸入格式:
首先,在一行上輸入一串?dāng)?shù)字(1~4,整數(shù)),其中,1代表圓形卡片,2代表矩形卡片,3代表三角形卡片,4代表梯形卡片。各數(shù)字之間以一個或多個空格分隔,以“0”結(jié)束。例如: 1 3 4 2 1 3 4 2 1 3 0
然后根據(jù)第一行數(shù)字所代表的卡片圖形類型,依次輸入各圖形的相關(guān)參數(shù),例如:圓形卡片需要輸入圓的半徑,矩形卡片需要輸入矩形的寬和長,三角形卡片需要輸入三角形的三條邊長,梯形需要輸入梯形的上底、下底以及高。各數(shù)據(jù)之間用一個或多個空格分隔。
輸出格式:
如果圖形數(shù)量非法(小于0)或圖形屬性值非法(數(shù)值小于0以及三角形三邊不能組成三角形),則輸出Wrong Format。
如果輸入合法,則正常輸出,所有數(shù)值計算后均保留小數(shù)點后兩位即可。輸出內(nèi)容如下:
排序前的各圖形類型及面積,格式為圖形名稱1:面積值1圖形名稱2:面積值2 …圖形名稱n:面積值n,注意,各圖形輸出之間用空格分開,且輸出最后存在一個用于分隔的空格;
排序后的各圖形類型及面積,格式同排序前的輸出;
所有圖形的面積總和,格式為Sum of area:總面積值。
輸入樣例1:
在這里給出一組輸入。例如:

1 5 3 2 0

輸出樣例1:
在這里給出相應(yīng)的輸出。例如:

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:
在這里給出相應(yīng)的輸出。例如:

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:
在這里給出相應(yīng)的輸出。例如:

Wrong Format

參考代碼如下:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {
 //在Main類中定義一個靜態(tài)Scanner對象,這樣在其它類中如果想要使用該對象進(jìn)行輸入,則直接
 //使用Main.input.next…即可(避免采坑)
 public static Scanner input = new Scanner(System.in);
 public static void main(String[] args){
 ArrayList<Integer> list = new ArrayList<Integer>();
 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<Card> cardList=new ArrayList<Card>();
 DealCardList(){
 
 }
 DealCardList(ArrayList<Integer> list){
 for(int i=0;i<list.size();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<cardList.size();i++)
 {if(!cardList.get(i).getShape().vaildate())
  return false;}
 return true;
 }
 public void cardSort() {
 for(int k=0;k<cardList.size();k++)
 for(int i=k+1;i<cardList.size();i++)
 {
 if(cardList.get(k).getShape().getArea()<cardList.get(i).getShape().getArea())
  Collections.swap(cardList, k, i);
 }
 
 
 }
 public double getAllArea() {
 double s=0;
 for(int i=0;i<cardList.size();i++)
 s=s+cardList.get(i).getShape().getArea();
 return s;
 }
 public void showResult() {
 System.out.println("The original list:");
 for(int i=0;i<cardList.size();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<cardList.size();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;
 }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論