30RF
如题,让AI帮我写了4个代码,结果都无法编译
- import java.util.Scanner;
-
- public class QuadraticRoots {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- System.out.println("请输入一元二次方程的系数:");
- System.out.print("a = ");
- double a = scanner.nextDouble();
- System.out.print("b = ");
- double b = scanner.nextDouble();
- System.out.print("c = ");
- double c = scanner.nextDouble();
-
- double discriminant = b * b - 4 * a * c;
-
- if (discriminant > 0) {
- double root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
- double root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
- System.out.println("方程的根为:" + root1 + " 和 " + root2);
- if (root1 < 0 && root2 < 0) {
- System.out.println("不等式的方向为:<");
- } else if (root1 > 0 && root2 > 0) {
- System.out.println("不等式的方向为:>");
- } else {
- System.out.println("不等式的方向为:无解");
- }
- } else if (discriminant == 0) {
- double root = -b / (2 * a);
- System.out.println("方程的根为:" + root);
- if (root < 0) {
- System.out.println("不等式的方向为:<");
- } else if (root > 0) {
- System.out.println("不等式的方向为:>");
- } else {
- System.out.println("不等式的方向为:无解");
- }
- } else {
- System.out.println("此方程没有实数根。");
- }
- }
- }
复制代码- import java.util.Scanner;
-
- public class DistanceToLine {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- System.out.println("请输入直线的两个端点的坐标(x1, y1)和(x2, y2):");
- double x1 = scanner.nextDouble();
- double y1 = scanner.nextDouble();
- double x2 = scanner.nextDouble();
- double y2 = scanner.nextDouble();
- System.out.println("请输入定点的坐标(x0, y0):");
- double x0 = scanner.nextDouble();
- double y0 = scanner.nextDouble();
-
- double numerator = Math.abs((y2 - y1) * x0 - (x2 - x1) * y0 + x2 * y1 - y2 * x1);
- double denominator = Math.sqrt(Math.pow(y2 - y1, 2) + Math.pow(x2 - x1, 2));
- double distance = numerator / denominator;
- System.out.println("定点到直线的距离为:" + distance);
- }
- }
复制代码- import java.awt.geom.Point2D;
- import java.util.Arrays;
-
- public class TriangleAreaCalculator {
-
- public static void main(String[] args) {
- // 定义三角形的三个顶点
- Point2D.Double[] trianglePoints = {
- new Point2D.Double(0, 0),
- new Point2D.Double(5, 0),
- new Point2D.Double(2.5, 3)
- };
-
- // 使用海伦公式计算三角形面积
- double area = calculateTriangleArea(trianglePoints);
- System.out.println("三角形的面积为: " + area);
- }
-
- /**
- * 使用海伦公式计算三角形的面积。
- * @param trianglePoints 包含三角形三个顶点的数组
- * [url=home.php?mod=space&uid=238747]@Return[/url] 三角形的面积
- */
- public static double calculateTriangleArea(Point2D.Double[] trianglePoints) {
- double semiPerimeter = calculateSemiPerimeter(trianglePoints);
- double area = Math.abs(semiPerimeter * (trianglePoints[0].x + trianglePoints[1].x + trianglePoints[2].x) / 3);
- return area;
- }
-
- /**
- * 计算三角形的半周长。
- * @param trianglePoints 包含三角形三个顶点的数组
- * @return 三角形的半周长
- */
- public static double calculateSemiPerimeter(Point2D.Double[] trianglePoints) {
- double semiPerimeter = 0;
- for (int i = 0; i < trianglePoints.length; i++) {
- Point2D.Double currentPoint = trianglePoints[i];
- Point2D.Double nextPoint = trianglePoints[(i + 1) % trianglePoints.length]; // 使用模运算确保循环遍历数组
- semiPerimeter += currentPoint.distance(nextPoint);
- }
- return semiPerimeter / 2;
- }
- }
复制代码- import java.util.*;
-
- public class FourColorTheorem {
- private static final int MAX_COLORS = 4;
- private static final int UNCOLORED = 0;
- private static final int[] COUNT = new int[MAX_COLORS + 1];
- private static final int[][] GRAPH = new int[MAX_COLORS][MAX_COLORS];
- private static final int[][] EDGE = new int[MAX_COLORS][MAX_COLORS];
- private static final int[] COLOR = new int[MAX_COLORS];
- private static final boolean[] VISITED = new boolean[MAX_COLORS];
-
- public static void main(String[] args) {
- int[][] map = {
- {0, 1, 3, 3},
- {1, 0, 2, 3},
- {3, 2, 0, 1},
- {3, 3, 1, 0}
- }; // 一个简单的地图表示。此处的值代表了各区域相邻的区域颜色。例如,值为1表示与该区域相邻的区域颜色为1。
- colorize(map);
- System.out.println("Map colored with " + (countColors(map) - 1) + " colors"); // -1是因为我们没有为未涂色的区域染色。
- }
-
- public static void colorize(int[][] map) {
- Arrays.fill(COUNT, 0);
- for (int i = 0; i < map.length; i++) {
- if (!VISITED[i]) {
- dfs(i);
- }
- }
- }
-
- public static void dfs(int i) {
- VISITED[i] = true;
- for (int j = 0; j < MAX_COLORS; j++) {
- if (GRAPH[i][j] == EDGE[i][j]) { // 如果地图上的区域 i 与 j 是相邻的。
- if (!VISITED[j]) { // 如果区域 j 尚未被访问过。
- COLOR[j] = j; // 为区域 j 涂上颜色 j。
- dfs(j); // 对区域 j 进行深度优先搜索。
- } else if (COLOR[j] != i) { // 如果区域 j 已经被访问过,并且其颜色与区域 i 不相同。
- if (COUNT[COLOR[j]] == 0) { // 如果区域 j 的颜色尚未被使用过。
- COLOR[j] = i; // 为区域 j 涂上颜色 i。
- COUNT[i]++; // 增加颜色 i 的使用次数。
- } else { // 如果区域 j 的颜色已经被使用过。
- int k = findColor(COLOR[j]); // 找到与区域 j 的颜色不同的另一种颜色。
- if (k != -1) { // 如果找到这种颜色。
- if (COUNT[k] == 0) { // 如果这种颜色尚未被使用过。
- COLOR[j] = k; // 为区域 j 涂上这种颜色。
- COUNT[k]++; // 增加这种颜色的使用次数。
- } else { // 如果这种颜色已经被使用过。
- int l = findColor(k); // 找到与这种颜色不同的另一种颜色。
- if (l != -1) { // 如果找到这种颜色。
- COLOR[j] = l; // 为区域 j 涂上这种颜色。
- COUNT[l]++; // 增加这种颜色的使用次数。
- } else { // 如果找不到与这种颜色不同的另一种颜色,则无法为地图染色。
- System.out.println("Map cannot be colored with four colors.");
- return;
- }
- }
- } else { // 如果找不到与区域 j 的颜色不同的另一种颜色,则无法为地图染色。
- System.out.println("Map cannot be colored with four colors.");
- return;
- }
- }
- } else { // 如果区域 j 的颜色与区域 i 相同,则无法为地图染色。
- System.out.println("Map cannot be colored with four colors.");
- return;
复制代码
|
|