最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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

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

相關(guān)文章

  • SpringBoot用配置影響B(tài)ean加載@ConditionalOnProperty

    SpringBoot用配置影響B(tài)ean加載@ConditionalOnProperty

    這篇文章主要為大家介紹了SpringBoot用配置影響B(tài)ean加載@ConditionalOnProperty示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • Java實(shí)現(xiàn)抽獎(jiǎng)算法的示例代碼

    Java實(shí)現(xiàn)抽獎(jiǎng)算法的示例代碼

    這篇文章主要為大家詳細(xì)介紹了如何利用Java語(yǔ)言實(shí)現(xiàn)抽獎(jiǎng)算法,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Java有一定幫助,需要的可以參考一下
    2022-04-04
  • spring boot starter actuator(健康監(jiān)控)配置和使用教程

    spring boot starter actuator(健康監(jiān)控)配置和使用教程

    這篇文章主要介紹了spring-boot-starter-actuator(健康監(jiān)控)配置和使用教程,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-06-06
  • JAVA流控及超流控后的延遲處理實(shí)例

    JAVA流控及超流控后的延遲處理實(shí)例

    這篇文章主要介紹了JAVA流控及超流控后的延遲處理,以實(shí)例形式較為詳細(xì)的分析了Java進(jìn)行流量控制的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2014-12-12
  • Windows Zookeeper安裝過程及啟動(dòng)圖解

    Windows Zookeeper安裝過程及啟動(dòng)圖解

    這篇文章主要介紹了Windows Zookeeper安裝過程及啟動(dòng)圖解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-12-12
  • JAVA日常開發(fā)中一些常見問題歸納講解

    JAVA日常開發(fā)中一些常見問題歸納講解

    這篇文章主要給大家介紹了JAVA日常開發(fā)中一些常見問題的相關(guān)資料,包括語(yǔ)法錯(cuò)誤、數(shù)據(jù)類型問題、面向?qū)ο缶幊虇栴}、集合類問題以及文件操作問題,通過詳細(xì)的分析和示例,幫助程序員提高代碼的健壯性和可維護(hù)性,需要的朋友可以參考下
    2024-12-12
  • 解決安裝mysqlclient的時(shí)候出現(xiàn)Microsoft Visual C++ 14.0 is required報(bào)錯(cuò)

    解決安裝mysqlclient的時(shí)候出現(xiàn)Microsoft Visual C++ 14.0 is required報(bào)錯(cuò)

    這篇文章主要介紹了解決安裝mysqlclient的時(shí)候出現(xiàn)Microsoft Visual C++ 14.0 is required報(bào)錯(cuò)問題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-11-11
  • Java IO之File 類詳解

    Java IO之File 類詳解

    這篇文章主要為大家介紹了vue組件通信的幾種方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-01-01
  • Spring Cloud Ribbon負(fù)載均衡器處理方法

    Spring Cloud Ribbon負(fù)載均衡器處理方法

    這篇文章主要介紹了Spring Cloud Ribbon負(fù)載均衡器處理方法,看看是如何獲取服務(wù)實(shí)例,獲取以后做了哪些處理,處理后又是如何選取服務(wù)實(shí)例的,需要的朋友可以參考下
    2018-02-02
  • 如何關(guān)閉 IDEA 自動(dòng)更新

    如何關(guān)閉 IDEA 自動(dòng)更新

    這篇文章主要介紹了如何關(guān)閉 IDEA 自動(dòng)更新,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12

最新評(píng)論

牙克石市| 繁昌县| 深水埗区| 定州市| 天峨县| 永城市| 泗阳县| 临江市| 安达市| 灵武市| 伊吾县| 新丰县| 馆陶县| 阿克| 启东市| 荔波县| 永年县| 新昌县| 广丰县| 江达县| 沁水县| 英超| 辉南县| 尼勒克县| 公安县| 冷水江市| 徐闻县| 揭阳市| 孟州市| 讷河市| 舒兰市| 合阳县| 南澳县| 呼和浩特市| 祁连县| 鹿泉市| 博爱县| 三明市| 吴旗县| 嘉善县| 东山县|