【问题标题】:One of my classes is not returning to the main class after it completes. How do I get it to return?我的一门课完成后没有返回主课。我如何让它返回?
【发布时间】:2020-12-07 17:35:47
【问题描述】:

我的主要方法是使用 switch case 菜单,效果很好。如果用户选择 2,它会转到该类并正常返回,但如果用户选择 1,它会执行该方法,然后停止并且不返回。我错过了什么?如果我执行系统退出,它会退出,但我需要它返回菜单。

停止的方法结束:

for (int index = 0; index < items.length; index++) {
            System.setOut(console);
            System.out.printf("%-16s %16d %16d %16d %16s \n", items[index], supplyOnHand[index], last24HourUsage[index], daysOnHand[index], status[index]);
            System.setOut(o);
            System.out.printf("%-16s %16d %16d %16d %16s \n", items[index], supplyOnHand[index], last24HourUsage[index], daysOnHand[index], status[index]);
        }
    }
}

主要:

import java.io.IOException;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws IOException {
        boolean exit = false;
        do {
            System.out.println("Please enter your choice:");
            System.out.println("1. Enter new data.");
            System.out.println("2. Review latest data.");
            System.out.println("3. Exit.");
            Scanner sc1 = new Scanner(System.in);
            int choice = sc1.nextInt();
            switch (choice){
                case 1:
                    Dashboard.main();
                    break;
                case 2:
                    ReadFile.main();
                    break;
                case 3:
                    exit = true;
                    break;
            }
        } while (!exit);
    }
}

编辑希望使它更清晰。我不擅长措辞。 如果我选择 2 它会执行并返回菜单:

Please enter your choice:
1. Enter new data.
2. Review latest data.
3. Exit.
2
Please enter your choice:
1. Enter new data.
2. Review latest data.
3. Exit.

如果我选择 1,它不会返回,只是挂起:

Please enter your choice:
1. Enter new data.
2. Review latest data.
3. Exit.
1

Welcome to the Rationed Medical Supply Dashboard.
After your entries of stock on hand and daily usage,
your days on hand and supply status will be displayed for each item.
The results will be displayed in the log below and
also in a running log in the file DashboardLog.txt
_______________________________________________________________________________________________________________________
How many items are we tracking today?
1
Enter name of item # 1: 
1
How many of item # 1 do we have?: 
1
What was the usage of item # 1 in the last 24 hours: 
1
12/07/20 12:47:37
Current status of all items being tracked:
Item|             Supply on hand| Last 24 Hr Usage|    Days on hand|          Status|
-------------------------------------------------------------------------------------

1                               1                1                1         Critical 

【问题讨论】:

  • “正常返回”和“不返回”是什么意思?执行过程中有没有异常?
  • 已编辑以使其更清晰。

标签: java methods return


【解决方案1】:

我的一门课在完成后没有返回主课。如何让它返回?

Java 不是这样工作的。

方法运行。方法返回。类只是属性包;一些领域,一些方法。他们不跑。他们没有回来。

您在弄乱 System.out,然后询问“停止”。这是一个糟糕的计划,不要乱用 setOut。

它根本没有停滞,至少,您的粘贴中没有任何内容表明这一点。但是,您正在使 System.out 成为其他东西,因此,您的代码很可能不会停止,而是例如System.out.println("Please enter your choice:"); 正在发送到您看不到的地方。

System.setOut 更改 System.out 无处不在

【讨论】:

    【解决方案2】:

    想通了。我只需要结束:

                System.setOut(console);
                System.out.printf("%-16s %16d %16d %16d %16s \n", items[index], supplyOnHand[index], last24HourUsage[index], daysOnHand[index], status[index]);
    
    

    代替:

                System.setOut(o);
                System.out.printf("%-16s %16d %16d %16d %16s \n", items[index], supplyOnHand[index], last24HourUsage[index], daysOnHand[index], status[index]);
    
    

    现在很好用。感谢大家的帮助!

    【讨论】:

      猜你喜欢
      • 2018-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-24
      • 2012-02-20
      相关资源
      最近更新 更多