java实现图形卡片排序游戏

2025-05-29 0 105

本文实例为大家分享了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

参考代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218
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<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;

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

原文链接:https://blog.csdn.net/qq_45028587/article/details/107419481

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 java实现图形卡片排序游戏 https://www.kuaiidc.com/119339.html

相关文章

发表评论
暂无评论