JAVA FX - 从输入中获取大写字母数

时间:2016-04-10 15:13:48

标签: java javafx

我完全重做了代码,它几乎完成了。我想弄清楚的最后一件事是如何从用户在程序中输入的字符串中获取大写字母的数量。我没有JavaFx使用Scanner和System.out正确运行程序。但是,我还没有想出如何将代码从控制台输入转换为文本字段中的输入。这是我的新代码。

import java.util.Scanner;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

public class UppercaseLetters extends Application {

    public static void main(String[] args) {
        launch(args);
    }
@Override
public void start(Stage primaryStage) throws Exception {

    // Create Pane
    UpperCase pane = new UpperCase();

    primaryStage.setTitle("UpperCase");
    primaryStage.setScene(new Scene(pane));
    primaryStage.show();
    }

private class UpperCase extends GridPane {
    Label lblString = new Label("Enter String:");
    Label lblAmount = new Label("Number of capital Letters:");

    TextField tfString = new TextField();
    TextField tfAmount = new TextField();

    private UpperCase() {
        setHgap(10);
        setVgap(10);
        setPadding(new Insets(10,10,10,10));

        add(lblString, 0, 0);
        add(tfString, 1, 0);
        add(lblAmount, 0, 1);
        add(tfAmount, 1, 1);

        tfString.setText("0");
        tfString.setAlignment(Pos.CENTER_RIGHT);
        tfAmount.setText("0");
        tfAmount.setAlignment(Pos.CENTER_RIGHT);

        Button submit = new Button("Submit");
        HBox hButtons = new HBox(submit);
        submit.setOnAction(e->{

        });



    }
}


}

以下是没有JavaFx的程序代码。

import java.util.Scanner;

public class CapitalNumbers {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String s = input.nextLine();

        System.out.println("The number of uppercase letters = " + count(s.toCharArray()));
    }

    public static int count(char[] chars) {

        return count(chars, chars.length - 1);
    }

    public static int count(char[] chars, int high) {

        int count = Character.isUpperCase(chars[high]) ? 1 : 0;
        if (high == 0)
            return count;
        else
            return count + count(chars, high - 1);

    }
}

0 个答案:

没有答案