使用递归和递归助手将用户输入的大写字符从文本字段打印到文本区域

Using recursive and recursive helper to print user entered capital char's from texField to textArea

我写了这个 java -fx 程序,它在没有递归实现的情况下工作得很好。我写了按钮 onAction lambda 以确保程序在转换为递归之前正常工作。我花了最后几个小时试图找出两个必需的递归以及如何使用 button.onAction lambda 表达式调用它们,但需要朝着正确的方向推动。这是我的。

static TextField textField = new TextField ();
static String text = textField.getText();
static TextArea textArea = new TextArea();
static Button btSubmit = new Button ("Submit");

@Override
public void start(Stage primaryStage){

Label label1 = new Label("Enter letters and I will "
        + "count the capitals.\t"); //Create textfield label.

textArea.setEditable(false);
textArea.setMaxWidth(450);
textArea.setMaxHeight(100);

HBox hbox = new HBox(); //Create hbox.
hbox.setAlignment(Pos.BASELINE_CENTER); //Set hbox to center.
hbox.getChildren().addAll(label1, textArea, textField, 
        btSubmit); //Add children to hbox.


BorderPane pane = new BorderPane(); //Create pane.
pane.setTop(hbox); //Set hbox to top of pane.
pane.setCenter(textArea); //Set text area to center.

Scene scene = new Scene (pane, 450, 200); //Create scene.
primaryStage.setTitle("Count Capital Letters"); 
primaryStage.setScene(scene);
primaryStage.show();    

btSubmit.setOnAction(e -> {
    String text = textField.getText();
    int upperCase = 0;
    for (int i = 0; i < text.length(); i++){
        if (Character.isUpperCase(text.charAt(i))) upperCase++;
    }
    String numCaps = String.valueOf(upperCase);
    textArea.appendText("Number of capitals: " + numCaps);
    });
}

public static int count(char[] chars) {
    String text = textField.getText();
    chars = text.toCharArray();
    if (chars.length == 0); 
    return 0;
}
public static int count(char[] chars, int high) {
    high = 0;
    String text = textField.getText();
    chars = text.toCharArray();
    for (int i = 0; i < chars.length; i++){
        if (Character.isUpperCase(chars[i])) {
            high++;
            }
        }
    return high;
}

public static void main(String[] args){
launch(args);
}}

我想做的是让按钮操作调用递归方法,但我对如何用递归调用替换当前操作感到困惑。

睡了一觉之后,这是我为这两种递归方法想出的。我知道昨晚我有两个不同的方法,并且递归应该调用自己。这是我到目前为止所做的更改。

而不是两个单独的方法:

public static int count(char[] chars, int high) {
     int count = 0;
     if (high < chars.length) {
          if (Character.isUpperCase(chars[high])) {
               return 1 + count;
          }
          else {
               return count(chars, high+1);
          }
     }
     return 0;
}

至少我是在正确的轨道上吗?被要求使用两种递归方法(原始方法及其辅助方法)让我失望。

从您的代码中不清楚 high 应该是什么。我假设它应该是您要检查的字符数组中最后一个字符的索引(可能是独占的)。

所以你的递归步骤确实需要减少 high,而不是增加它,你只需直接检查 chars[high-1]。您还需要一个终止步骤,即没有更多字符可供查看时。

所以我会把它实现为

private int count(char[] chars, int high) {
    // if high==0 we are looking at zero characters, so there are no upper-case characters:
    if (high == 0) {
        return 0 ;
    }

    if (Character.isUpperCase(chars[high-1])) {
        // if the last character we look at is upper-case, then the total
        // for this set of characters is one more than the total for the
        // set of characters omitting the last one:
        return 1 + count(chars, high-1);
    } else {
        // otherwise (the last character is not upper-case), the total
        // for this set of characters is the same as the total for the 
        // set of characters omitting the last one:
        return count(chars, high-1);
    }
}

为方便起见,您还可以实施

public int count(String text) {
    return count(text.getChars(), text.length());
}

然后您的事件处理程序只需要调用

label1.setText("Number of upper case: "+count(textField.getText()));

(你究竟为什么要创造一切 static 是另外一个话题。)