【问题标题】:Repeat Method In While LoopWhile循环中的重复方法
【发布时间】:2012-11-17 06:43:10
【问题描述】:

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

【问题讨论】:

    标签: java loops methods


    【解决方案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();
        }
    }
    

    【讨论】:

    • 这并没有达到我需要做的事情。我有两个在 main 方法之前执行的方法。首先程序需要读取用户输入到数组 getIDs();,然后程序要求用户输入 3 个 id 来计算价格 pathCalc();然后询问用户他/她想做什么,计算另一条路径或退出。如果输入 >=1,pathCalc();。再次询问此用户是否要进行 pathCalc();或退出。
    • @Kronos:我更新了答案。希望它能如您所愿,否则,请分享您的pathCalc();getIDs(); 方法摘要,这将有助于我更好地理解您的问题。
    • 我更新了我的问题。我有两个例外。希望您现在能更好地理解。
    • @Kronos:我用修改后的程序更新了答案。这似乎奏效了。如果您发现任何问题,请检查并告诉我。
    【解决方案2】:
    if (choice == 0){ 
    System.exit(0);
     }
    

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

    【讨论】:

      猜你喜欢
      • 2015-10-26
      • 2012-04-27
      • 1970-01-01
      • 2014-03-23
      • 2013-05-21
      • 1970-01-01
      • 1970-01-01
      • 2021-09-09
      • 1970-01-01
      相关资源
      最近更新 更多