java While 循环中的重复方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13428395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:40:38  来源:igfitidea点击:

Repeat Method In While Loop

javaloopsmethods

提问by Kronos

I cant seem to get my main method working as it should 100%. It is good but I need fix a looping problem.

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

My main goal was to get the program to repeat method pathCalc() once user enters any number >=1 and to end the program when user enters 0. However, when user enters >= 1 to repeat the program, program does repeat, but when it gets to the point to ask user if to repeat or exit again, and user enters 0 to exit, the program repeats method pathCalc() instead of exiting.

我的主要目标是让程序在用户输入任意数字 >=1 时重复方法 pathCalc() 并在用户输入 0 时结束程序。但是,当用户输入 >= 1 重复程序时,程序会重复,但是当到了要询问用户是要重复还是再次退出,并且用户输入 0 退出时,程序将重复 pathCalc() 方法而不是退出。

How do I get this to work after repeating method, so that the user enters >= 1 to repeat method or 0 to exit?

如何在重复方法后使其工作,以便用户输入 >= 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();
         }
     }
}

回答by Yogendra Singh

You are reading the choicetow times within the while loop. Move one instance out as below:

您正在阅读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();
    }

Also, there is not need of extra flag. you an use choiceitself in the condition as mentioned above.

此外,不需要额外的标志。您可以choice在上述条件下使用自身。

Your while loop will not start, if user enter 0in the beginning; and the loop will terminate automatically when user enters 0afterwards.

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

EDIT:Updated program.

编辑:更新程序。

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

回答by Meesh

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

to exit directly or make sure to check choice before entering the loop

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