如何让这个计算器应用程序为两种不同的方法制作两个不同的场景

问题描述 投票:0回答:0

哦,孩子!所以我已经在这个计算器应用程序上工作了几天来练习 java,但现在我遇到了困难。它基本上应该采用项目、编码挑战和测验成绩来计算课程成绩。我已经完成了项目和编码,但我无法通过测验!它应该将场景更改为测验栏(这取决于在主场景中输入了多少(因此,如果您输入 15 个测验完成,将出现一个包含 15 个框的场景等))当单击按钮时,但我有大量试图让它工作的问题。有时我会得到一个很大的错误,有时它会崩溃。目前,它甚至不会开始。

我一直在疯狂地试图找出问题,但即使在更新正则表达式模式、多次修改代码并摆弄场景构建器之后,它所做的只是在“输入测验成绩”之后什么都不做被点击。这是代码:

Main.Java

package application;
    
import java.io.FileInputStream;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;


public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            
            FXMLLoader loader = new FXMLLoader();
            VBox root = loader.load(new FileInputStream("src/application/GradeCalculatorView.fxml"));
            GradeCalculatorController controller = (GradeCalculatorController)loader.getController();
            controller.applicationStage = primaryStage;
            Scene scene = new Scene(root,600,300);
            
            primaryStage.setScene(scene);
            primaryStage.setTitle("Fikayo's Grade Calculator");
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}

GradeCalculatorView.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.*?>
<?import javafx.collections.*?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.VBox?>
<?import javafx.collections.FXCollections?>

<VBox prefHeight="511.0" prefWidth="586.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.GradeCalculatorController">
   <children>
      <HBox>
         <children>
            <Label text="Project Grade">
               <HBox.margin>
                  <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
               </HBox.margin></Label>
            <TextField fx:id="projectGradeTextField"> 
              <HBox.margin>
                  <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
               </HBox.margin>
            </TextField>
            <Label text="\%">
               <HBox.margin>
                  <Insets right="10.0" top="10.0" />
               </HBox.margin></Label>
                <Label fx:id="projectErrorLabel" text="" textFill="RED">
                <HBox.margin>
                    <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                </HBox.margin>
            </Label>
         </children>
      </HBox>
      <HBox prefHeight="100.0" prefWidth="200.0">
         <children>
            <Label text="Passed Required Coding Challenges " />
            <ChoiceBox fx:id="passedRequiredCodingChallengesChoiceBox" prefWidth="150.0">
            <items>
            <FXCollections fx:factory="observableArrayList">
                <Integer fx:value="0" />
                <Integer fx:value="1" />
                <Integer fx:value="2" />
                <Integer fx:value="3" />
                <Integer fx:value="4" />
                <Integer fx:value="5" />
                <Integer fx:value="6" />
                <Integer fx:value="7" />
                <Integer fx:value="8" />
                <Integer fx:value="9" />
                <Integer fx:value="10" />
                <Integer fx:value="11" />
                <Integer fx:value="12" />
                <Integer fx:value="13" />
                <Integer fx:value="14" />
                <Integer fx:value="15" />
            </FXCollections>
            </items>
            </ChoiceBox>
         </children>
      </HBox>
      <HBox prefHeight="100.0" prefWidth="200.0">
         <children>
            <Label text="Passed Optional Coding Challenges" />
            <ChoiceBox fx:id="passedOptionalCodingChallengesChoiceBox" prefWidth="150.0">
                <items>
                <FXCollections fx:factory="observableArrayList">
                    <Integer fx:value="0" />
                    <Integer fx:value="1" />
                    <Integer fx:value="2" />
                    <Integer fx:value="3" />
                    <Integer fx:value="4" />
                    <Integer fx:value="5" />
                </FXCollections>
                </items>
               <opaqueInsets>
                  <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
               </opaqueInsets>
            </ChoiceBox>
         </children>
      </HBox>
      <HBox prefHeight="53.0" prefWidth="280.0">
         <children>
            <Label text="Number of Required Quizzes Done" />
                <ChoiceBox fx:id="requiredQuizzesChoiceBox" prefWidth="150.0">
                <items>
                <FXCollections fx:factory="observableArrayList">
                    <Integer fx:value="1" />
                    <Integer fx:value="2" />
                    <Integer fx:value="3" />
                    <Integer fx:value="4" />
                    <Integer fx:value="5" />
                    <Integer fx:value="6" />
                    <Integer fx:value="7" />
                    <Integer fx:value="8" />
                    <Integer fx:value="9" />
                    <Integer fx:value="10" />
                    <Integer fx:value="11" />
                    <Integer fx:value="12" />
                    <Integer fx:value="13" />
                    <Integer fx:value="14" />
                    <Integer fx:value="15" />
                </FXCollections>
                </items>
               <opaqueInsets>
                  <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
               </opaqueInsets>
            </ChoiceBox>
                  <Button mnemonicParsing="false" onAction="#getQuizGrades" text="Enter Quiz Grades" />
            <Label fx:id="averageRequiredQuizLabel" />
         </children>
         <VBox.margin>
            <Insets top="10.0" />
         </VBox.margin>
      </HBox>
      <HBox prefHeight="53.0" prefWidth="280.0">
         <children>
            <Label text="Number of Optional Quizzes Done" />
            <ChoiceBox fx:id="optionalQuizzesChoiceBox" prefWidth="150.0">
               <items>
                  <FXCollections fx:factory="observableArrayList">
                     <Integer fx:value="1" />
                     <Integer fx:value="2" />
                     <Integer fx:value="3" />
                     <Integer fx:value="4" />
                     <Integer fx:value="5" />
                    <Integer fx:value="6" />
                    <Integer fx:value="7" />
                  </FXCollections>
               </items>
               <opaqueInsets>
                  <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
               </opaqueInsets>
            </ChoiceBox>
            <Button mnemonicParsing="false" onAction="#getQuizGrades" text="Enter Quiz Grades" />
            <Label fx:id="averageOptionalQuizLabel" />
         </children>
      </HBox>
      <Button mnemonicParsing="false" onAction="#calculateGrade" text="Calculate Grade">
         <VBox.margin>
            <Insets top="10.0" />
         </VBox.margin>
      </Button>
      <HBox prefHeight="0.0" prefWidth="437.0">
         <children>
            <Label fx:id="courseGradeLabel">
               <opaqueInsets>
                  <Insets bottom="10.0" right="10.0" top="10.0" />
               </opaqueInsets>
               <HBox.margin>
                  <Insets bottom="10.0" left="100.0" top="10.0" />
               </HBox.margin>
            </Label>
         </children>
         <opaqueInsets>
            <Insets left="10.0" />
         </opaqueInsets>
      </HBox>
   </children>
   <padding>
      <Insets top="1.0" />
   </padding>
</VBox>

GradeCalculator.java

package application;

import java.util.ArrayList;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class GradeCalculatorController {
    Stage applicationStage;

     @FXML
        private ChoiceBox<Integer> passedRequiredCodingChallengesChoiceBox;

        @FXML
        private TextField projectGradeTextField;

        @FXML
        private ChoiceBox<Integer> passedOptionalCodingChallengesChoiceBox;

        @FXML
        private ChoiceBox<Integer> requiredQuizzesChoiceBox;
        
        @FXML
        private Label courseGradeLabel;
        
        @FXML
        private Label averageRequiredQuizLabel;
        
        @FXML
        private Label averageOptionalQuizLabel;
        
        @FXML
        private Label projectErrorLabel;
        
        @FXML
        private ChoiceBox<Integer> optionalQuizzesChoiceBox;
        
        @FXML
        void getQuizGrades(){
            
        }
        
        /**
         * Convert the value entered to a double value. This method will verify that the value
         * entered is indeed a number and is a valid percentage grade (0-100). If the
         * value entered is not a valid percentage grade, this method will return 0.0 as the project
         * grade instead.
         * 
         * 
         * @param valueEntered a String that holds a value entered by the user intended to be a project grade
         * @return the project value entered by the user if it is a valid percentage grade and 0 otherwise
         */
        
        double getProjectGrade(String valueEntered) {
            boolean validProjectGrade = true;
            int dotcount = 0;
            boolean errorFound = false;
            for (char c : valueEntered.toCharArray()) {
                if (!errorFound) {  
                    if (c == '.') {
                        if (dotcount == 1) {
                            validProjectGrade = false;
                            projectErrorLabel.setText("You can only use one decimal point in a project grade. Make sure to enter a number.");
                            errorFound = true;
                        }
                        dotcount++;
                    } else if (!Character.isDigit(c) || (c == '.' && dotcount > 0)) {
                        validProjectGrade = false;
                        projectErrorLabel.setText("Do not use " + c + " in a project grade. Make sure to enter a number.");
                        errorFound = true;
                    }
                }
            }
        // Convert the string entered by the user to a double if the input is a valid number
        // Otherwise the project grade will default to 0
        double projectGrade = 0;
        if (validProjectGrade) {
            projectGrade = Double.parseDouble(valueEntered);
        }
        
        // Checks if the number entered is a valid percentage grade
        // If valid, include it in the grade computation
        if (projectGrade < 0 || projectGrade > 100) {
            projectErrorLabel.setText("Project Grade is not between 0 and 100%");
            projectGrade = 0;
        } 
        
            return projectGrade;
        }
        
        double averageRequiredQuizGrade = 0.0;
        double averageOptionalQuizGrade = 0.0;
        
        void calculateAverageRequiredQuizGrade(Scene requiredQuizGradeScene, ArrayList<TextField> requiredQuizGradeTextfields) {
            applicationStage.setScene(requiredQuizGradeScene);
            averageRequiredQuizGrade = 0.0;
            for (TextField requiredQuizGradeTextfield : requiredQuizGradeTextfields) {
                averageRequiredQuizGrade += Double.parseDouble(requiredQuizGradeTextfield.getText());
            }
            averageRequiredQuizGrade = (averageRequiredQuizGrade / 20);
        }

        void calculateAverageOptionalQuizGrade(Scene optionalQuizGradeScene, ArrayList<TextField> optionalQuizGradeTextfields) {
            applicationStage.setScene(optionalQuizGradeScene);
            averageOptionalQuizGrade = 0.0;
            if (optionalQuizGradeTextfields != null && !optionalQuizGradeTextfields.isEmpty()) {
                for (TextField optionalQuizGradeTextfield : optionalQuizGradeTextfields) {
                    averageOptionalQuizGrade += Double.parseDouble(optionalQuizGradeTextfield.getText());
                }
                averageOptionalQuizGrade = (averageOptionalQuizGrade / optionalQuizGradeTextfields.size());
            }
        }




        private ChoiceBox<Integer> requiredQuizGradeChoiceBox;
        private ChoiceBox<Integer> optionalQuizGradeChoiceBox;
    
        @FXML
        
        // Calculates the Grades for all the other factors (project, quizzes, and the Coding Challenges)
        void getRequiredQuizGrades(ActionEvent enterRequiredQuizGradeEvent) {
            Scene mainScene = applicationStage.getScene();
                
                int numberRequiredOfQuizzes = requiredQuizzesChoiceBox.getValue();
                int rowsCreated = 0;
                VBox requiredQuizGradeContainer = new VBox();
                ArrayList<TextField> requiredQuizGradeTextfields = new ArrayList<TextField>();
                while (rowsCreated < numberRequiredOfQuizzes) {
            
                    HBox rowContainer = new HBox();
                    Label quizGradeLabel = new Label("Quiz Grade");
                    TextField quizGradeTextfield = new TextField();
                    requiredQuizGradeTextfields.add(quizGradeTextfield);
                    
                    rowContainer.getChildren().addAll(quizGradeLabel,quizGradeTextfield);
                    rowsCreated++;
                    
                    requiredQuizGradeContainer.getChildren().add(rowContainer);
                }
                    Button doneButton = new Button("Done");
                    doneButton.setOnAction(doneEvent -> calculateAverageRequiredQuizGrade(mainScene, requiredQuizGradeTextfields));
                    requiredQuizGradeContainer.getChildren().add(doneButton);   
                    Scene requiredQuizGradesScene = new Scene(requiredQuizGradeContainer);
                    applicationStage.setScene(requiredQuizGradesScene);
            }
        
        void getOptionalQuizGrades(ActionEvent enterOptionalQuizGradeEvent) {
            Scene mainScene = applicationStage.getScene();
                
                int numberOfOptionalQuizzes = optionalQuizGradeChoiceBox.getValue();
                int rowsCreated = 0;
                VBox optionalQuizGradeContainer = new VBox();
                ArrayList<TextField> optionalQuizGradeTextfields = new ArrayList<TextField>();
                while (rowsCreated < numberOfOptionalQuizzes) {
            
                    HBox rowContainer = new HBox();
                    Label quizGradeLabel = new Label("Quiz Grade");
                    TextField optionalQuizGradeTextfield = new TextField();
                    optionalQuizGradeTextfields.add(optionalQuizGradeTextfield);
                    
                    rowContainer.getChildren().addAll(quizGradeLabel,optionalQuizGradeTextfield);
                    rowsCreated++;
                    
                    optionalQuizGradeContainer.getChildren().add(rowContainer);
                }
                    Button doneButton = new Button("Done");
                    doneButton.setOnAction(doneEvent -> calculateAverageOptionalQuizGrade(mainScene, optionalQuizGradeTextfields));
                    optionalQuizGradeContainer.getChildren().add(doneButton);   
                    Scene optionalQuizGradesScene = new Scene(optionalQuizGradeContainer);
                    applicationStage.setScene(optionalQuizGradesScene);
            }
        
        
//          Button doneButton = new Button("Done");
//          doneButton.setOnAction(doneEvent -> calculateAverageQuizGrade(mainScene, quizGradeTextfields));
    @FXML
    
    // Calculates the Grades for all the other factors (project, quizzes, and the Coding Challenges)
    void calculateGrade(ActionEvent event) {
        
        // Clear the error messages
        projectErrorLabel.setText("");
        
        double courseGrade = 0.0;
        
        // assuming the project is worth 50% of the grade
        String projectValueEntered = projectGradeTextField.getText();
        
        double projectGrade = getProjectGrade(projectValueEntered);
        
        // Required quizzes divide by 15
        // Optional divide by 5
            
        courseGrade = courseGrade + projectGrade * 50/100;
        
        System.out.println("Project grade " + projectGrade+ " Course grade so far: " + courseGrade);
    
        //courseGrade += (averageQuizGrade * 10) * 0.25;
        //System.out.println("Quiz grade " + averageQuizGrade + " Course grade so far: " + courseGrade);
        
        // double quizGrade = averageQuizGrade;
        
        int RequiredCodingChallengesPassed = passedRequiredCodingChallengesChoiceBox.getValue();
        //courseGrade = courseGrade + (RequiredCodingChallengesPassed * 1.25/100); 
        System.out.println("Required coding challenges passed: " + RequiredCodingChallengesPassed + " Course grade so far: " + courseGrade);
        
        int OptionalCodingChallengesPassed = passedOptionalCodingChallengesChoiceBox.getValue();
        //courseGrade = courseGrade + (OptionalCodingChallengesPassed * 1.25/100);
        courseGrade = courseGrade + ((RequiredCodingChallengesPassed+OptionalCodingChallengesPassed)*100/20) * 0.25;
        //courseGrade = courseGrade + (quizGrade * 100/10) * 0.25;
        System.out.println("Optional coding challenges passed: " + OptionalCodingChallengesPassed + " Course grade so far: " + courseGrade);
        
        courseGradeLabel.setText(String.format("Your course grade is %.2f",  + courseGrade ));
  

    }

}


谁能告诉我我的代码哪里出错了??我直接不知道在这里做什么!

java javafx scenebuilder
© www.soinside.com 2019 - 2024. All rights reserved.