在While循环中重复方法

时间:2012-11-17 06:43:10

标签: java loops methods

我似乎无法让我的主要方法正常工作,因为它应该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();
         }
     }
}

2 个答案:

答案 0 :(得分:1)

您正在while循环中阅读choice两次。如下所示移出一个实例:

    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();
    }
}

答案 1 :(得分:0)

if (choice == 0){ 
System.exit(0);
 }

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