0

我似乎无法让我的主要方法正常工作,因为它应该 100%。这很好,但我需要解决循环问题。

我的主要目标是让程序在用户输入任何数字 >=1 时重复方法 pathCalc() 并在用户输入 0 时结束程序。但是,当用户输入 >=1 重复程序时,程序确实重复,但是当它询问用户是否重复或再次退出,并且用户输入 0 退出时,程序重复方法 pathCalc() 而不是退出。

如何在重复方法后使其工作,以便用户输入 >= 1 重复方法或 0 退出?

    import java.util.Scanner;

      public class AssignmentArrays{
         static int[] data = new int [6];

            public static void getIDs(){

               Scanner seg = new Scanner(System.in);

               int[] data = new int [6];
               data[0] = 0;
               data[1] = 0;
               data[2] = 0;
               data[3] = 0;
               data[4] = 0;
               data[5] = 0;

              /* Segment values */



               System.out.println("Enter cost for segment 0:");
               data[0] = seg.nextInt();

               System.out.println("Enter cost for segment 1:");
               data[1] = seg.nextInt();

               System.out.println("Enter cost for segment 2::");
               data[2] = seg.nextInt();

               System.out.println("Enter cost for segment 3:");
               data[3] = seg.nextInt();

               System.out.println("Enter cost for segment 4:");
               data[4] = seg.nextInt();

               System.out.println("Enter cost for segment 5:");
               data[5] = seg.nextInt();

   }

     public static void pathCalc(){     

     /* Path inputs */

     Scanner node1 = new Scanner(System.in);


     int pathCost;

     System.out.println("Enter ID of segment 0 of path:");
     int node1value = node1.nextInt();

     System.out.println("Enter ID of segment 1 of path:");
     int node2value = node1.nextInt();

     System.out.println("Enter ID of segment 2 of path:");
     int node3value = node1.nextInt();

     /* Path cost calculation */

     pathCost = data[node1value] + data[node2value] + data[node3value];
     System.out.println("The cost of the trip is: $" + pathCost);
     }

     public static void main(String[] args){
      getIDs();
      pathCalc();
      System.out.println("Enter 0 to exit or any other number"+
                                        " to evaluate another path:");
         int choice;
         choice = end.nextInt();

         while(choice != 0){
         getIDs();
         pathCalc();
         System.out.println("Enter 0 to exit or any other number"+
                                        " to evaluate another path:");
         choice = end.nextInt();
         }
     }
}
4

2 回答 2

1

您正在阅读choicewhile 循环中的两个时间。移出一个实例,如下所示:

    getIDs();
    pathCalc();
    System.out.println("Enter 0 to exit or any other number"+
                                            " to evaluate another path:");
    int choice = end.nextInt();
    while(choice != 0){
         //getIDs();
         pathCalc();
         System.out.println("Enter 0 to exit or any other number"+
                                            " to evaluate another path:");
         choice = end.nextInt();
    }

此外,不需要额外的标志。choice您可以在上述情况下使用自己。

0如果用户在开头输入,您的 while 循环将不会启动;0当用户随后进入时,循环将自动终止。

编辑:更新程序。

public class AssignmentArrays {
    static int[] data = new int[6];
    static Scanner seg;

    public static void getIDs() {
        int[] data = new int[6];
        data[0] = 0;
        data[1] = 0;
        data[2] = 0;
        data[3] = 0;
        data[4] = 0;
        data[5] = 0;

        /* Segment values */

        System.out.println("Enter cost for segment 0:");
        data[0] = seg.nextInt();

        System.out.println("Enter cost for segment 1:");
        data[1] = seg.nextInt();

        System.out.println("Enter cost for segment 2::");
        data[2] = seg.nextInt();

        System.out.println("Enter cost for segment 3:");
        data[3] = seg.nextInt();

        System.out.println("Enter cost for segment 4:");
        data[4] = seg.nextInt();

        System.out.println("Enter cost for segment 5:");
        data[5] = seg.nextInt();

    }

    public static void pathCalc() {
        int pathCost;

        System.out.println("Enter ID of segment 0 of path:");
        int node1value = seg.nextInt();

        System.out.println("Enter ID of segment 1 of path:");
        int node2value = seg.nextInt();

        System.out.println("Enter ID of segment 2 of path:");
        int node3value = seg.nextInt();

        /* Path cost calculation */

        pathCost = data[node1value] + data[node2value] + data[node3value];
        System.out.println("The cost of the trip is: $" + pathCost);
    }

    public static void main(String[] args) {
        seg = new Scanner(System.in);
        getIDs();
        pathCalc();
        System.out.println("Enter 0 to exit or any other number"
                + " to evaluate another path:");
        int choice;
        choice = seg.nextInt();

        while (choice != 0) {
            getIDs();
            pathCalc();
            System.out.println("Enter 0 to exit or any other number"
                    + " to evaluate another path:");
            choice = seg.nextInt();
        }
        seg.close();
    }
}
于 2012-11-17T06:46:25.400 回答
0
if (choice == 0){ 
System.exit(0);
 }

直接退出或确保在进入循环之前检查选择

于 2012-11-17T06:52:25.947 回答