= 创作分享 =
崩溃解答
AI帮我写了4个代码都有问题
InquestiorMai

AI帮我写了4个代码都有问题

InquestiorMai 于 2023-12-22 20:13 ( 4月前 ) [复制链接] [只看楼主] [打印]
285 1
30RF
如题,让AI帮我写了4个代码,结果都无法编译
  1. import java.util.Scanner;  
  2.   
  3. public class QuadraticRoots {  
  4.     public static void main(String[] args) {  
  5.         Scanner scanner = new Scanner(System.in);  
  6.         System.out.println("请输入一元二次方程的系数:");  
  7.         System.out.print("a = ");  
  8.         double a = scanner.nextDouble();  
  9.         System.out.print("b = ");  
  10.         double b = scanner.nextDouble();  
  11.         System.out.print("c = ");  
  12.         double c = scanner.nextDouble();  
  13.   
  14.         double discriminant = b * b - 4 * a * c;  
  15.   
  16.         if (discriminant > 0) {  
  17.             double root1 = (-b + Math.sqrt(discriminant)) / (2 * a);  
  18.             double root2 = (-b - Math.sqrt(discriminant)) / (2 * a);  
  19.             System.out.println("方程的根为:" + root1 + " 和 " + root2);  
  20.             if (root1 < 0 && root2 < 0) {  
  21.                 System.out.println("不等式的方向为:<");  
  22.             } else if (root1 > 0 && root2 > 0) {  
  23.                 System.out.println("不等式的方向为:>");  
  24.             } else {  
  25.                 System.out.println("不等式的方向为:无解");  
  26.             }  
  27.         } else if (discriminant == 0) {  
  28.             double root = -b / (2 * a);  
  29.             System.out.println("方程的根为:" + root);  
  30.             if (root < 0) {  
  31.                 System.out.println("不等式的方向为:<");  
  32.             } else if (root > 0) {  
  33.                 System.out.println("不等式的方向为:>");  
  34.             } else {  
  35.                 System.out.println("不等式的方向为:无解");  
  36.             }  
  37.         } else {  
  38.             System.out.println("此方程没有实数根。");  
  39.         }  
  40.     }  
  41. }
复制代码
  1. import java.util.Scanner;  
  2.   
  3. public class DistanceToLine {  
  4.     public static void main(String[] args) {  
  5.         Scanner scanner = new Scanner(System.in);  
  6.         System.out.println("请输入直线的两个端点的坐标(x1, y1)和(x2, y2):");  
  7.         double x1 = scanner.nextDouble();  
  8.         double y1 = scanner.nextDouble();  
  9.         double x2 = scanner.nextDouble();  
  10.         double y2 = scanner.nextDouble();  
  11.         System.out.println("请输入定点的坐标(x0, y0):");  
  12.         double x0 = scanner.nextDouble();  
  13.         double y0 = scanner.nextDouble();  
  14.   
  15.         double numerator = Math.abs((y2 - y1) * x0 - (x2 - x1) * y0 + x2 * y1 - y2 * x1);  
  16.         double denominator = Math.sqrt(Math.pow(y2 - y1, 2) + Math.pow(x2 - x1, 2));  
  17.         double distance = numerator / denominator;  
  18.         System.out.println("定点到直线的距离为:" + distance);  
  19.     }  
  20. }
复制代码
  1. import java.awt.geom.Point2D;  
  2. import java.util.Arrays;  
  3.   
  4. public class TriangleAreaCalculator {  
  5.   
  6.     public static void main(String[] args) {  
  7.         // 定义三角形的三个顶点  
  8.         Point2D.Double[] trianglePoints = {  
  9.             new Point2D.Double(0, 0),  
  10.             new Point2D.Double(5, 0),  
  11.             new Point2D.Double(2.5, 3)  
  12.         };  
  13.   
  14.         // 使用海伦公式计算三角形面积  
  15.         double area = calculateTriangleArea(trianglePoints);  
  16.         System.out.println("三角形的面积为: " + area);  
  17.     }  
  18.   
  19.     /**  
  20.      * 使用海伦公式计算三角形的面积。  
  21.      * @param trianglePoints 包含三角形三个顶点的数组  
  22.      * [url=home.php?mod=space&uid=238747]@Return[/url] 三角形的面积  
  23.      */  
  24.     public static double calculateTriangleArea(Point2D.Double[] trianglePoints) {  
  25.         double semiPerimeter = calculateSemiPerimeter(trianglePoints);  
  26.         double area = Math.abs(semiPerimeter * (trianglePoints[0].x + trianglePoints[1].x + trianglePoints[2].x) / 3);  
  27.         return area;  
  28.     }  
  29.   
  30.     /**  
  31.      * 计算三角形的半周长。  
  32.      * @param trianglePoints 包含三角形三个顶点的数组  
  33.      * @return 三角形的半周长  
  34.      */  
  35.     public static double calculateSemiPerimeter(Point2D.Double[] trianglePoints) {  
  36.         double semiPerimeter = 0;  
  37.         for (int i = 0; i < trianglePoints.length; i++) {  
  38.             Point2D.Double currentPoint = trianglePoints[i];  
  39.             Point2D.Double nextPoint = trianglePoints[(i + 1) % trianglePoints.length];  // 使用模运算确保循环遍历数组  
  40.             semiPerimeter += currentPoint.distance(nextPoint);  
  41.         }  
  42.         return semiPerimeter / 2;  
  43.     }  
  44. }
复制代码
  1. import java.util.*;  
  2.   
  3. public class FourColorTheorem {  
  4.     private static final int MAX_COLORS = 4;  
  5.     private static final int UNCOLORED = 0;  
  6.     private static final int[] COUNT = new int[MAX_COLORS + 1];  
  7.     private static final int[][] GRAPH = new int[MAX_COLORS][MAX_COLORS];  
  8.     private static final int[][] EDGE = new int[MAX_COLORS][MAX_COLORS];  
  9.     private static final int[] COLOR = new int[MAX_COLORS];  
  10.     private static final boolean[] VISITED = new boolean[MAX_COLORS];  
  11.   
  12.     public static void main(String[] args) {  
  13.         int[][] map = {  
  14.             {0, 1, 3, 3},  
  15.             {1, 0, 2, 3},  
  16.             {3, 2, 0, 1},  
  17.             {3, 3, 1, 0}  
  18.         }; // 一个简单的地图表示。此处的值代表了各区域相邻的区域颜色。例如,值为1表示与该区域相邻的区域颜色为1。  
  19.         colorize(map);  
  20.         System.out.println("Map colored with " + (countColors(map) - 1) + " colors"); // -1是因为我们没有为未涂色的区域染色。  
  21.     }  
  22.   
  23.     public static void colorize(int[][] map) {  
  24.         Arrays.fill(COUNT, 0);  
  25.         for (int i = 0; i < map.length; i++) {  
  26.             if (!VISITED[i]) {  
  27.                 dfs(i);  
  28.             }  
  29.         }  
  30.     }  
  31.   
  32.     public static void dfs(int i) {  
  33.         VISITED[i] = true;  
  34.         for (int j = 0; j < MAX_COLORS; j++) {  
  35.             if (GRAPH[i][j] == EDGE[i][j]) { // 如果地图上的区域 i 与 j 是相邻的。  
  36.                 if (!VISITED[j]) { // 如果区域 j 尚未被访问过。  
  37.                     COLOR[j] = j; // 为区域 j 涂上颜色 j。  
  38.                     dfs(j); // 对区域 j 进行深度优先搜索。  
  39.                 } else if (COLOR[j] != i) { // 如果区域 j 已经被访问过,并且其颜色与区域 i 不相同。  
  40.                     if (COUNT[COLOR[j]] == 0) { // 如果区域 j 的颜色尚未被使用过。  
  41.                         COLOR[j] = i; // 为区域 j 涂上颜色 i。  
  42.                         COUNT[i]++; // 增加颜色 i 的使用次数。  
  43.                     } else { // 如果区域 j 的颜色已经被使用过。  
  44.                         int k = findColor(COLOR[j]); // 找到与区域 j 的颜色不同的另一种颜色。  
  45.                         if (k != -1) { // 如果找到这种颜色。  
  46.                             if (COUNT[k] == 0) { // 如果这种颜色尚未被使用过。  
  47.                                 COLOR[j] = k; // 为区域 j 涂上这种颜色。  
  48.                                 COUNT[k]++; // 增加这种颜色的使用次数。  
  49.                             } else { // 如果这种颜色已经被使用过。  
  50.                                 int l = findColor(k); // 找到与这种颜色不同的另一种颜色。  
  51.                                 if (l != -1) { // 如果找到这种颜色。  
  52.                                     COLOR[j] = l; // 为区域 j 涂上这种颜色。  
  53.                                     COUNT[l]++; // 增加这种颜色的使用次数。  
  54.                                 } else { // 如果找不到与这种颜色不同的另一种颜色,则无法为地图染色。  
  55.                                     System.out.println("Map cannot be colored with four colors.");  
  56.                                     return;  
  57.                                 }  
  58.                             }  
  59.                         } else { // 如果找不到与区域 j 的颜色不同的另一种颜色,则无法为地图染色。  
  60.                             System.out.println("Map cannot be colored with four colors.");  
  61.                             return;  
  62.                         }  
  63.                     }  
  64.                 } else { // 如果区域 j 的颜色与区域 i 相同,则无法为地图染色。  
  65.                     System.out.println("Map cannot be colored with four colors.");  
  66.                     return;
复制代码


一个共产党员,应该是襟怀坦白,忠实,积极,以革命利益为第一生命,以个人利益服从革命利益;无论何时何地,坚持正确的原则,同一切不正确的思想和行为作不疲倦的斗争,用以巩固党的集体生活,巩固党和群众的联系;
发表于 2023-12-22 20:13:33 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

回复 | 举报

该帖共收到 1 条回复!
寒冽
本帖最后由 寒冽 于 2023-12-23 22:30 编辑

我觉得是你的问题,除了FourColorTheorem都可以正常运行。你需要考虑包括Java环境,文件名在内的低级错误。
在抓鸽子的路上变成了和平鸽。
发表于 2023-12-23 22:26:39 | 只看该作者

回复 | 举报

百科目前不允许匿名发帖哦~ 请先 [ 登陆 ][ 注册 ] 吧~

本版积分规则

发新帖
  • 回复
  • 点评
  • 评分

[ MC百科(mcmod.cn) 除另有声明,所有开放公共编辑的内容均使用 BY-NC-SA 3.0 协议 ]

Minecraft百科CC协议
快速回复 返回顶部 返回列表