【问题标题】:Repeat executed code after user input用户输入后重复执行的代码
【发布时间】:2012-11-16 08:12:00
【问题描述】:

我正在创建一个程序来计算他的旅行费用。每个段都有一个成本,要求用户输入每个段的成本,然后输入 3 个段 ID (0-6)。将 3 个 ID 的成本相加得出最终价格。

如果用户输入 >=1,我需要从头开始重复程序(见最后的评论),我该怎么做?另外,我可以改进我的程序吗?如何改进?

import java.util.Scanner;

public class AssignmentArrays{
    public static void main(String[] args){


      Scanner seg0 = new Scanner(System.in);
      Scanner seg1 = new Scanner(System.in);
      Scanner seg2 = new Scanner(System.in);
      Scanner seg3 = new Scanner(System.in);
      Scanner seg4 = new Scanner(System.in);
      Scanner seg5 = 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 */

        while(data[0] == 0){

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

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

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

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

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

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

           /* Path inputs */

           Scanner node1 = new Scanner(System.in);
           Scanner node2 = new Scanner(System.in);
           Scanner node3 = new Scanner(System.in);

           int node1value;
           int node2value;
           int node3value;
           int pathCost;

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

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

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

          /* Path cost calculation */

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

          /* Repeate or end program */

           Scanner end = new Scanner(System.in);

           int userChoice;

           System.out.println("Enter 0 to exit or any other number to evaluate another path:");
              userChoice = end.nextInt();

           if (userChoice == 0){
              System.out.println("The program has ended");
              break;
              }
              else if(userChoice >= 1){
              /* REPEATE ALL OF THE ABOVE HERE */
              }
          }

     }

}

【问题讨论】:

  • 1) 为什么有 6 个扫描仪? 2) 请对代码块使用一致且符合逻辑的缩进。
  • 6 个扫描仪? 1 绰绰有余。此外,如果您想在 1 上重复整个程序作为输入。如果userChoice 为 1,则只需将要重复的代码的相关部分包含在循环中
  • 我想我明白了。扫描仪呢?我应该怎么做呢?
  • @Kronos,只需初始化一个并继续使用同一个。

标签: java loops methods


【解决方案1】:

我可能会让这个类更加面向对象。但是,您可以通过使用方法分解代码来简化您所拥有的:

import java.util.Scanner;

public class AssignmentArrays{

static int[] data = new int [6];

public static void getSegmentIDs() {
   ...
}

pubilc static int getUserMenuChoice() {
   ...
}

public static void main(String[] args){

 int exit = false;

 while(!exit) {
   getSegmentIDs();
   choice = getUserMenuChoice();
   if (choice == 0) exit = true;
 } 

}
}

【讨论】:

  • 我把程序分成了3个方法,getIDs(); , 路径计算 ();和主要方法。现在我有一个问题,因为 DATA 变量在 getIDs();我在 pathCalc 中需要它。 “找不到符号”
  • 您可以将其设为 AssignmentArrays 类的静态成员。我已经更新了我的答案以反映这一点。
  • 好的,现在可以了,但是我的程序从 pathCalc() 开始;而不是 getIDs();。我该如何解决这个问题?
【解决方案2】:

我会通过这样做将整个事情放入一个 do while 语句中:

  String answer ="";
  do {
  Scanner seg0 = new Scanner(System.in);
  Scanner seg1 = new Scanner(System.in);
  Scanner seg2 = new Scanner(System.in);
  YOUR CODE CONTINOUS

  /* Repeate or end program */
  System.out.println("would you like to redo? (Y/N)");
  answer = end.next();
  } while(answer.toLowerCase().contains("y"));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-18
    • 2014-11-18
    • 1970-01-01
    • 1970-01-01
    • 2021-02-01
    相关资源
    最近更新 更多