鱼C论坛

 找回密码
 立即注册
查看: 542|回复: 2

[已解决]java的题

[复制链接]
发表于 2023-5-4 09:14:40 | 显示全部楼层 |阅读模式
60鱼币
修改给定的代码。如果指定了形状的速度,您可以通过添加一个额外的三个整数(所有这些都应该在0-255范围内)来设置颜色。换句话说,一个圆可以被指定为 圆x y r 或 用圆圈,用圆圈 或 圆圈,红色,绿色,蓝色 以类似的方式指定正方形。 请注意,如果不指定速度,就不能指定颜色。如果没有指定颜色,则形状应该为黑色。 在主班上完成这个练习。主类中必须包含更多的操作: 计算文件“practical8.txt”中的正方形和圆圈的数量。在解析文件时,程序需要确认文件中每行的第一个标记是“圆”或“正方形”。如果该行不符合要求,则请忽略它。将解析结果打印到控制台。 计算所有形状的总面积。将结果打印到控制台。

practical8.txt文件如下,剩余文件在压缩包里面
Circle  50 60 40 50 50 200 0 0
Circle  150 160 10 30 50
Square 250 260 45  -50 -50 100 20 100
Shape1 50 70 40 50 50 200 0 0

Shape2 60 50 20
Circle 120 50 20
Circle 180 50 20
Circle 240 50 20
Circle 300 50 20
Circle 360 50 20
Circle 420 50 20
Square 480 50 20
Square 540 50 20
Square 400 440 40

Shape3 10 460 5
Circle 20 460 5
Circle 30 460 5
Circle 40 460 5
Circle 50 460 5
Circle 20 470 5
Circle 30 470 5
Circle 40 470 5
Circle 50 470 5
Circle 20 480 5
Circle 30 480 5
Circle 40 480 5
Circle 50 480 5
Circle 20 490 5
Circle 30 490 5
Circle 40 490 5
Circle 50 490 5
最佳答案
2023-5-4 09:14:41
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.Scanner;

  4. public class Shape {
  5.     private final String type;
  6.     private final int x;
  7.     private final int y;
  8.     private final int size;
  9.     private final int[] color;
  10.     private final double area;

  11.     public Shape(String type, int x, int y, int size) {
  12.         this.type = type;
  13.         this.x = x;
  14.         this.y = y;
  15.         this.size = size;
  16.         this.color = new int[]{0, 0, 0};
  17.         if (type.equals("Circle")) {
  18.             area = Math.PI * Math.pow(size, 2);
  19.         } else {
  20.             area = Math.pow(size, 2);
  21.         }
  22.     }

  23.     public Shape(String type, int x, int y, int size, int r, int g, int b) {
  24.         this.type = type;
  25.         this.x = x;
  26.         this.y = y;
  27.         this.size = size;
  28.         this.color = new int[]{r, g, b};
  29.         if (type.equals("Circle")) {
  30.           area = Math.PI * Math.pow(size, 2);
  31.         } else {
  32.           area = Math.pow(size, 2);
  33.         }
  34.     }

  35.     public String getType() {
  36.         return type;
  37.     }

  38.     public int getX() {
  39.         return x;
  40.     }

  41.     public int getY() {
  42.         return y;
  43.     }

  44.     public int getSize() {
  45.         return size;
  46.     }

  47.     public int[] getColor() {
  48.         return color;
  49.     }

  50.     public double getArea() {
  51.         return area;
  52.     }

  53.     public static void main(String[] args) {
  54.         int circleCount = 0;
  55.         int squareCount = 0;
  56.         double totalArea = 0;

  57.         try {
  58.             File file = new File("practical8.txt");
  59.             Scanner scanner = new Scanner(file);

  60.             while (scanner.hasNextLine()) {
  61.                 String line = scanner.nextLine().trim();
  62.                 String[] tokens = line.split("\\s+");
  63.                 if (tokens.length < 4) {
  64.                     continue;
  65.                 }
  66.                 String type = tokens[0];
  67.                 int x = Integer.parseInt(tokens[1]);
  68.                 int y = Integer.parseInt(tokens[2]);
  69.                 intsize = integer.parseint(tokens[3]);

  70.                 shape shape;
  71.                 if (type.equals("circle")) {
  72.                     circlecount++;
  73.                     if (tokens.length == 7) {
  74.                         int r = integer.parseint(tokens[4]);
  75.                         int g = integer.parseint(tokens[5]);
  76.                         int b = integer.parseint(tokens[6]);
  77.                         shape = new shape(type, x, y, size, r, g, b);
  78.                     } else {
  79.                         shape = new shape(type, x, y, size);
  80.                     }
  81.                 } else {
  82.                     squarecount++;
  83.                     if (tokens.length == 7) {
  84.                         int r = integer.parseint(tokens[4]);
  85.                         int g = integer.parseint(tokens[5]);
  86.                         int b = integer.parseint(tokens[6]);
  87.                         shape = new shape(type, x, y, size, r, g, b);
  88.                     } else {
  89.                         shape = new shape(type, x, y, size);
  90.                     }
  91.                 }
  92.                 system.out.println("shape type: " + shape.gettype());
  93.                 system.out.println("position: (" + shape.getx() + ", " + shape.gety() + ")");
  94.                 system.out.println("size: " + shape.getsize());
  95.                 system.out.println("color: [" + shape.getcolor()[0] + ", " + shape.getcolor()[1] + ", " + shape.getcolor()[2] + "]");
  96.                 system.out.println("area: " + shape.getarea());

  97.                 totalarea += shape.getarea();
  98.             }
  99.             scanner.close();
  100.         } catch (filenotfoundexception e) {
  101.             system.out.println("file not found!");
  102.         }

  103.         system.out.println("number of circles: " + circlecount);
  104.         system.out.println("number of squares: " + squarecount);
  105.         system.out.println("total area of all shapes: " + totalarea);
  106.    }
  107. }
复制代码

Textfiles Exercises - Solution.zip

39.97 KB, 下载次数: 0

最佳答案

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-5-4 09:14:41 | 显示全部楼层    本楼为最佳答案   
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.Scanner;

  4. public class Shape {
  5.     private final String type;
  6.     private final int x;
  7.     private final int y;
  8.     private final int size;
  9.     private final int[] color;
  10.     private final double area;

  11.     public Shape(String type, int x, int y, int size) {
  12.         this.type = type;
  13.         this.x = x;
  14.         this.y = y;
  15.         this.size = size;
  16.         this.color = new int[]{0, 0, 0};
  17.         if (type.equals("Circle")) {
  18.             area = Math.PI * Math.pow(size, 2);
  19.         } else {
  20.             area = Math.pow(size, 2);
  21.         }
  22.     }

  23.     public Shape(String type, int x, int y, int size, int r, int g, int b) {
  24.         this.type = type;
  25.         this.x = x;
  26.         this.y = y;
  27.         this.size = size;
  28.         this.color = new int[]{r, g, b};
  29.         if (type.equals("Circle")) {
  30.           area = Math.PI * Math.pow(size, 2);
  31.         } else {
  32.           area = Math.pow(size, 2);
  33.         }
  34.     }

  35.     public String getType() {
  36.         return type;
  37.     }

  38.     public int getX() {
  39.         return x;
  40.     }

  41.     public int getY() {
  42.         return y;
  43.     }

  44.     public int getSize() {
  45.         return size;
  46.     }

  47.     public int[] getColor() {
  48.         return color;
  49.     }

  50.     public double getArea() {
  51.         return area;
  52.     }

  53.     public static void main(String[] args) {
  54.         int circleCount = 0;
  55.         int squareCount = 0;
  56.         double totalArea = 0;

  57.         try {
  58.             File file = new File("practical8.txt");
  59.             Scanner scanner = new Scanner(file);

  60.             while (scanner.hasNextLine()) {
  61.                 String line = scanner.nextLine().trim();
  62.                 String[] tokens = line.split("\\s+");
  63.                 if (tokens.length < 4) {
  64.                     continue;
  65.                 }
  66.                 String type = tokens[0];
  67.                 int x = Integer.parseInt(tokens[1]);
  68.                 int y = Integer.parseInt(tokens[2]);
  69.                 intsize = integer.parseint(tokens[3]);

  70.                 shape shape;
  71.                 if (type.equals("circle")) {
  72.                     circlecount++;
  73.                     if (tokens.length == 7) {
  74.                         int r = integer.parseint(tokens[4]);
  75.                         int g = integer.parseint(tokens[5]);
  76.                         int b = integer.parseint(tokens[6]);
  77.                         shape = new shape(type, x, y, size, r, g, b);
  78.                     } else {
  79.                         shape = new shape(type, x, y, size);
  80.                     }
  81.                 } else {
  82.                     squarecount++;
  83.                     if (tokens.length == 7) {
  84.                         int r = integer.parseint(tokens[4]);
  85.                         int g = integer.parseint(tokens[5]);
  86.                         int b = integer.parseint(tokens[6]);
  87.                         shape = new shape(type, x, y, size, r, g, b);
  88.                     } else {
  89.                         shape = new shape(type, x, y, size);
  90.                     }
  91.                 }
  92.                 system.out.println("shape type: " + shape.gettype());
  93.                 system.out.println("position: (" + shape.getx() + ", " + shape.gety() + ")");
  94.                 system.out.println("size: " + shape.getsize());
  95.                 system.out.println("color: [" + shape.getcolor()[0] + ", " + shape.getcolor()[1] + ", " + shape.getcolor()[2] + "]");
  96.                 system.out.println("area: " + shape.getarea());

  97.                 totalarea += shape.getarea();
  98.             }
  99.             scanner.close();
  100.         } catch (filenotfoundexception e) {
  101.             system.out.println("file not found!");
  102.         }

  103.         system.out.println("number of circles: " + circlecount);
  104.         system.out.println("number of squares: " + squarecount);
  105.         system.out.println("total area of all shapes: " + totalarea);
  106.    }
  107. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-6-30 07:19:19 | 显示全部楼层
这机翻
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 22:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表