Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 基于旅行商的问题_Java - Fatal编程技术网

Java 基于旅行商的问题

Java 基于旅行商的问题,java,Java,我正在写一个基于旅行推销员问题的程序。用户可在四个城市中确定其x和y坐标。销售员总是从city1开始,到city1结束,因此有6条可能的路线。但是,每条路线都有一条等效路线,即route1与route6具有相同的距离。我已经解释了这一点。我还试图解释(route1或route6)和(route2或route4)是否具有相同的距离。程序告诉你了 然而,每当四条甚至所有六条路线的距离都相同时,程序只会告诉我,四条或六条路线中有两条的距离最短。这就是我需要帮助的地方 import java.util.

我正在写一个基于旅行推销员问题的程序。用户可在四个城市中确定其x和y坐标。销售员总是从
city1
开始,到
city1
结束,因此有6条可能的路线。但是,每条路线都有一条等效路线,即
route1
route6
具有相同的距离。我已经解释了这一点。我还试图解释(
route1
route6
)和(
route2
route4
)是否具有相同的距离。程序告诉你了

然而,每当四条甚至所有六条路线的距离都相同时,程序只会告诉我,四条或六条路线中有两条的距离最短。这就是我需要帮助的地方

import java.util.Scanner;
import java.lang.Math;

public class CityDistancesProgram 
{
   public static void main(String[] args)
   {
     Scanner keyboard = new Scanner(System.in);

     //x and y coordinates of each city
     int x1, y1, x2, y2, x3, y3, x4, y4;

     //Variable for the distances of each route
     double route1, route2, route3, route4, route5, route6; 

     //Since the distance from cityA to cityB is the same as the distance from cityB to cityA,
     //these are all the possible combinations of distances between each city
     double city1city2, city2city3, city3city4, city4city1, city2city4, city3city1;
     double city2city1, city3city2, city4city3, city1city4, city4city2, city1city3;

     double shortestRoute;

     System.out.println("Enter the value of each city's x-coordinate and y-coordinate");
     System.out.println(" ");

     //First city
     System.out.println("City 1's x-coordinate:");
     x1 = keyboard.nextInt();
     System.out.println("City 1's y-coordinate:"); 
     y1 = keyboard.nextInt();

     //Second city
     System.out.println("City 2's x-coordinate:");
     x2 = keyboard.nextInt();
     System.out.println("City 2's y-coordinate:"); 
     y2 = keyboard.nextInt();

     //Third city
     System.out.println("City 3's x-coordinate:");
     x3 = keyboard.nextInt();
     System.out.println("City 3's y-coordinate:"); 
     y3 = keyboard.nextInt();

     //Fourth city
     System.out.println("City 4's x-coordinate:");
     x4 = keyboard.nextInt();     
     System.out.println("City 4's y-coordinate:"); 
     y4 = keyboard.nextInt();

     System.out.println("City 1's coordinates are: (" +  x1 + ", "  +  y1 +")");
     System.out.println("City 2's coordinates are: (" +  x2 + ", "  +  y2 +")");
     System.out.println("City 3's coordinates are: (" +  x3 + ", "  +  y3 +")");
     System.out.println("City 4's coordinates are: (" +  x4 + ", "  +  y4 +")");

       //Computing all possible combinations of distance between each city
       city1city2 = Math.sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2)); //distance from city1 to city2 
       city3city1 = Math.sqrt((x1 - x3)*(x1 - x3) + (y1 - y3)*(y1 - y3)); //distance from city1 to city3 
       city4city1 = Math.sqrt((x1 - x4)*(x1 - x4) + (y1 - y4)*(y1 - y4)); //distance from city4 to city1
       city2city3 = Math.sqrt((x2 - x3)*(x2 - x3) + (y2 - y3)*(y2 - y3)); //distance from city2 to city3 
       city3city4 = Math.sqrt((x3 - x4)*(x3 - x4) + (y3 - y4)*(y3 - y4)); //distance from city3 to city4 
       city2city4 = Math.sqrt((x2 - x4)*(x2 - x4) + (y2 - y4)*(y2 - y4)); //distance from city2 to city4 

       city2city1 = city1city2; //distance from city2 to city1
       city3city2 = city2city3; //distance from city3 to city2
       city4city3 = city3city4; //distance from city4 to city3
       city1city4 = city4city1; //distance from city1 to city4
       city4city2 = city2city4; //distance from city4 to city2
       city1city3 = city3city1; //distance from city1 to city3

       //Computing the distance of each possible route
       route1 = city1city2 + city2city3 + city3city4 + city4city1;
       route2 = city1city2 + city2city4 + city4city3 + city3city1;
       route3 = city1city3 + city3city2 + city2city4 + city4city1;
       route4 = city1city3 + city3city4 + city4city2 + city2city1;
       route5 = city1city4 + city4city2 + city2city3 + city3city1;
       route6 = city1city4 + city4city3 + city3city2 + city2city1;

       System.out.println(" ");
       System.out.println("The first route has a total distance of " + route1 + " km");
       System.out.println("The second route has a total distance of " + route2 + " km");
       System.out.println("The third route has a total distance of " + route3 + " km");
       System.out.println("The fourth route has a total distance of " + route4 + " km");
       System.out.println("The fifth route has a total distance of " + route5 + " km");
       System.out.println("The sixth route has a total distance of " + route6 + " km");

       shortestRoute = Math.min(Math.min(route1, Math.min(route2,route3)), Math.min(route4,Math.min(route5,route6)));
       System.out.println(" ");

       if(shortestRoute == route1 || shortestRoute == route6)
       {
         System.out.println("route1 and route6 have the shortest distance");
       }
       else if(shortestRoute == route2 || shortestRoute == route4)
       {
         System.out.println("route2 and route4 have the shortest distance");
       }
       else if(shortestRoute == route3 || shortestRoute == route5)
       {
         System.out.println("route3 and route5 have the shortest distance");
       }
       else if((shortestRoute == route1 || shortestRoute == route6) && (shortestRoute == route2 || shortestRoute == route4))
       {
          System.out.println("route1, route6, route2 and route4 have the shortest distance");
       }
       else if((shortestRoute == route1 || shortestRoute == route6) && (shortestRoute == route3 || shortestRoute == route5))
       {
          System.out.println("route1, route6, route3 and route5 have the shortest distance");
       }
       else if((shortestRoute == route3 || shortestRoute == route5) && (shortestRoute == route2 || shortestRoute == route4))
       {
          System.out.println("route3, route5, route2 and route4 have the shortest distance");
       }
       else
       {
            System.out.println("There is no shortest distance, they are all the same");
       }
   }
}

问题在于最后的if语句集合。这是一组其他表达式,所以只要匹配一个表达式,就完成了。您需要确保表达式的顺序是从最具体到最不具体

您可以简化为以下内容(仅限伪代码)


在我看来,代码不是很有效。这是正确的吗?我想可以对它进行审查,但OP不应该期望对遇到的具体问题给出直接或间接的答案。这也不能阻止回答者给出答案,尽管重新编写代码可能会使问题的根源更加明显。代码确实有效,但没有按照我希望的方式工作,例如,当所有城市的距离相同时,程序不会这样说,正如你在我的else声明中所看到的,我本打算让这一切发生。它只是随机吐出两个城市@JamalIf如果您想在这个问题上获得帮助,堆栈溢出将是正确的位置。如果你愿意,我可以把这篇文章移到那边。收到帮助后,您可以在此处发布工作代码以供审阅。很抱歉,我忘了在该评论上ping@Jamal。这是因为堆栈溢出是一个专门的地方,当你被损坏的代码难倒时,你可以问它;“代码检查”是指当您的代码开始工作,然后您怀疑自己编写的代码是否良好时进行检查。@AlexChavez现在您的代码正常工作了,我们可以在“代码检查”中进行检查。
bool r1 = shortestRoute == route1 || shortestRoute == route6;
bool r2 = shortestRoute == route2 || shortestRoute == route4;
bool r3 = shortestRoute == route3 || shortestRoute == route5;

if (r1 && r2 && r2) {
    print "all the same"
}
else if (r1 && r2) {
}
else if (r1 && r3) {
}
else if (r2 && r3) {
}
// Now individual checks for r1 r2 and r3