JavaFXML协助 - 硬币计数器

时间:2017-03-02 06:48:27

标签: java

过去一个月我一直在学习Java,而且我的课程比我的课程学到的更多,并且决定开始制作具有各种转换计算器的GUI程序。但是,有一个给我带来麻烦。对我来说这似乎很简单,但由于某种原因它无法正常工作。

我想要做的就是当我点击转换按钮时,程序将美元金额转换为四分之一,硬币,镍币,便士,其中便士可能是最低金额。我通过链接到我的主类的控制器类来配置它。我在下面附上了我的代码。谢谢!

package calculator.coins;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;

public class CoinsController {

@FXML
private Button convertButton;

@FXML
private Button resetButton;

@FXML
private TextField dollarsText;

@FXML 
private TextField quartersText;

@FXML
private TextField dimesText;

@FXML
private TextField nickelsText;

@FXML
private TextField penniesText;

@FXML
private void ConvertDollars(ActionEvent event)
{
    double monetary_amount, quarters, dimes, nickels, pennies;
    monetary_amount = Double.parseDouble(dollarsText.getText().toString());

    quarters = (monetary_amount / .25);
    monetary_amount = monetary_amount%.25;

    dimes = (monetary_amount / .10);
    monetary_amount = monetary_amount%.10;

    nickels = (monetary_amount / .05);
    monetary_amount = monetary_amount%0.05;

    pennies = (monetary_amount / .01);
    monetary_amount = monetary_amount%.01;

    quartersText.setText("" +quarters);
    dimesText.setText("" +dimes);
    nickelsText.setText("" +nickels);
    penniesText.setText("" +pennies);
}

@FXML
private void resetProg(ActionEvent event)
{
    quartersText.setText("");
    dimesText.setText("");
    nickelsText.setText("");
    penniesText.setText("");
    dollarsText.requestFocus();
}

}

1 个答案:

答案 0 :(得分:0)

参考controll的这段代码



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package exercise14;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import
        javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

/**
 *
 * @author j
 */
public class FXMLDocumentController {
    public Label output;
 //   public int b;
      public int c;
    public Label  o2;
    public Label  o3;
    public Label  o4;
       public int e;
       
   public TextField input;
    public void maths(){
   String j= input.getText();
   int a =Integer.parseInt(j);
   
   if(a>=25){
       int b=a/25;
       
    output.setText(String.valueOf(b));}
   }
    if(a%25>=10)
    {    int c=a%25; //17
    int d=c/10; 
   
    o2.setText(String.valueOf(d));
    }
  //  o3.setText(String.valueOf());
    if((a%25)%10>=1){
    int e =(a%25)%10; //7
    int f=e/5;
     o3.setText(String.valueOf(f));
    }
    
    if((((a%25)%10)%5)>=1){
        int m=((a%25)%10)%5;
        o4.setText(String.valueOf(m));
    }
    }
       
    
}