gpt4 book ai didi

java - 即使在程序执行后如何继续请求控制台输入运行

转载 作者:行者123 更新时间:2023-12-01 10:41:34 26 4
gpt4 key购买 nike

我试图在从控制台获取用户输入后执行程序。 [下面的代码块]。但是,我不想在程序执行完成后终止。我希望控制台在执行完成后始终询问我 INITIAL_MESSAGE。实际上,在执行程序后,我希望控制台再次询问我 INTIAL_MESSAGE,以便我可以再次根据需要输入输入并再次执行程序。我实际上是在这个方法中调用interactor(),从main方法作为起点。请告诉我如何实现这一目标

public class ConsoleInteraction {

/**
* @param args
*/

public static int numberOfJavaTrainees ;
public static int numberOfPHPTrainees ;
Barracks trainingBarrack = new Barracks();


public void interactor() throws IOException {

//reading capability from the consolemessages properties file

ResourceBundle bundle = ResourceBundle.getBundle("resources/consolemessages");

// Create a scanner so we can read the command-line input
Scanner scanner = new Scanner(System.in);

// Prompt for training or viewing camp
System.out.print(bundle.getString("INITIAL_MESSAGE"));
//Get the preference as an integer
int preference = scanner.nextInt();


//Show options based on preference
if(preference == 1)
{
//System.out.println("Whom do you want to train?\n 1.Java Guy \n 2.PHP Guy \n 3.Mix \n Enter You preference:");
System.out.print(bundle.getString("WHO_TO_TRAIN"));
int traineepreference = scanner.nextInt();
if (traineepreference == 1)
{
//System.out.println("How many Java guys you want to train ? : ");
System.out.print(bundle.getString("HOW_MANY_JAVA"));
numberOfJavaTrainees = scanner.nextInt();
trainingBarrack.trainTrainees(numberOfJavaTrainees, 0);

}
else if (traineepreference == 2)
{
//System.out.println("How many PHP guys you want to train ? : ");
System.out.print(bundle.getString("HOW_MANY_PHP"));
numberOfPHPTrainees = scanner.nextInt();
trainingBarrack.trainTrainees(0, numberOfPHPTrainees);

}
else if (traineepreference == 3)
{
System.out.print(bundle.getString("HOW_MANY_JAVA"));
numberOfJavaTrainees = scanner.nextInt();
System.out.print(bundle.getString("HOW_MANY_PHP"));
numberOfPHPTrainees = scanner.nextInt();
trainingBarrack.trainTrainees(numberOfJavaTrainees, numberOfPHPTrainees);

}
else
{
System.out.print(bundle.getString("ERROR_MESSAGE1"));
}
}
else if (preference == 2)
{
System.out.println("Showing Camp to You");
System.out.println("Java trained in Trainee Camp : "+ TraineeCamp.trainedJavaGuys);
System.out.println("PHP trained in Trainee Camp : "+ TraineeCamp.trainedPHPGuys);

}

else
{
System.out.print(bundle.getString("ERROR_MESSAGE2"));
}

scanner.close();
}
}

最佳答案

考虑将这些更改快速起草到您的类(class)中。可能无法编译。可能无法按您的计划进行。

我认为你应该改变的一些要点:

  • 使用常量作为选择值。使您的代码更易于阅读。
  • 在方法外部初始化 Bundle 和 Scanner。可能会被重复使用。
  • 不要在 if-else-if 级联中编写冗长的代码部分,而是调用其中的方法 - 从而大大提高可读性

    public class ConsoleInteraction  {

    public static int numberOfJavaTrainees ;
    public static int numberOfPHPTrainees ;

    //Don't read that every time...
    ResourceBundle bundle = ResourceBundle.getBundle("resources/consolemessages");


    public static void main(String[] args) {
    //Moving Scanner out of loop
    try {
    Scanner scanner = new Scanner(System.in);
    ConsoleInteraction ci = new ConsoleInteraction();
    //Loop until this returns false
    while(ci.interactor(scanner)) {
    System.out.println("=== Next iteration ===");
    }
    } catch (IOException e) {
    e.printStackTrace();
    }

    }

    //Constant values to make code readable
    public final static int PREF_TRAINING = 1;
    public final static int PREF_SHOW_CAMP = 2;
    public final static int PREF_QUIT = 99;

    public boolean interactor(Scanner scanner) throws IOException {

    // Prompt for training or viewing camp
    System.out.print(bundle.getString("INITIAL_MESSAGE"));
    //Get the preference as an integer
    int preference = scanner.nextInt();

    //Show options based on preference.

    if(preference == PREF_TRAINING) {
    //LIKE YOU DID BEFORE OR calling method:
    readTraining(scanner);
    } else if (preference == PREF_SHOW_CAMP) {
    //LIKE YOU DID BEFORE OR calling mathod:
    showCamp();
    } else if (preference == PREF_QUIT) {
    //Last loop
    return false;
    } else {
    System.out.print(bundle.getString("ERROR_MESSAGE2"));
    }
    //Next loop
    return true;
    }
    }

关于java - 即使在程序执行后如何继续请求控制台输入运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34384443/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com