作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试为我的 CS 1 类(class)编写代码。
代码的要点是编写一个代码,您可以在其中从一百美元的钞票中进行任何金额的找零,我需要归还适当的钞票和硬币。
我必须手动编写 JFrame
如果有人能告诉我计算中哪里出错了,那将会很有帮助。
/**
*
* @author esamayoa
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GetChange extends JFrame {
//Declare variables
JButton bCompute, bReset;
JTextField tAmount, tQuarters, tDimes, tNickels, tPennies, tTwenty, tTen, tFive, tOne;
JLabel lAmount, lQuarters, lDimes, lNickels, lPennies, lTwenty, lTen, lFive, lOne;
double amount, diff, totalPaid, quarter, dime, nickel, penny, twenty, ten, five, one;
public GetChange (){
//Set the attributes of the Jframe
setTitle("Eric");
setLocation(500,10);
setSize(450,1000);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
//Create your Gui components
lAmount = new JLabel("Amount");
lQuarters = new JLabel("Quarters:");
lDimes = new JLabel("Dimes:");
lNickels = new JLabel("Nickels:");
lPennies = new JLabel("Pennies:");
lTwenty = new JLabel("Twenties");
lTen = new JLabel("Tens");
lFive = new JLabel("Fives");
lOne = new JLabel("Ones");
tAmount = new JTextField();
tQuarters = new JTextField();
tDimes = new JTextField();
tNickels = new JTextField();
tPennies = new JTextField();
tTwenty = new JTextField();
tTen = new JTextField();
tFive = new JTextField();
tOne = new JTextField();
bReset = new JButton("Reset");
bCompute = new JButton("Compute");
//Add you Gui components to the Jframe
setLayout(new GridLayout(10,2));
add(lAmount);
add(tAmount);
add(lQuarters);
add(tQuarters);
add(lDimes);
add(tDimes);
add(lNickels);
add(tNickels);
add(lPennies);
add(tPennies);
add(lTwenty);
add(tTwenty);
add(lTen);
add(tTen);
add(lFive);
add(tFive);
add(lOne);
add(tOne);
add(bCompute);
add(bReset);
//Updates frame
this.validate();
//Add Action Listeners to buttons
bReset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
tAmount.setText("");
tQuarters.setText("");
tDimes.setText("");
tNickels.setText("");
tPennies.setText("");
tTwenty.setText("");
tTen.setText("");
tFive.setText("");
tOne.setText("");
tAmount.setText("");
}
});
//Create computation for compute button
bCompute.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
totalPaid = 100;
amount = Double.parseDouble(tAmount.getText());
diff = totalPaid-amount;
twenty = diff/20;
diff = diff%20;
tTwenty.setText(""+ twenty);
ten = diff/10;
diff = diff%10;
tTen.setText(""+ ten);
five = diff/5;
diff = diff%5;
tFive.setText(""+ five);
one = diff/1;
diff = diff%1;
tOne.setText(""+ one);
quarter = diff/.25;
diff = diff%.25;
tQuarters.setText(""+ quarter);
dime = diff/.1;
diff = diff%.1;
tDimes.setText(""+ dime);
nickel = diff/.05;
diff = diff%.05;
tNickels.setText(""+ nickel);
penny = diff/.01;
diff = diff%.01;
tPennies.setText(""+ penny);
}
});
}
//Main Method
public static void main(String[] args) {
// TODO code application logic here
GetChange myApp = new GetChange();
}
}
最佳答案
要解决第一个问题(框架未显示),请在添加所有组件后插入以下内容,
this.validate(); // updates frame
要添加重置按钮的实现,只需使用 .setText("");
bReset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
tAmount.setText("");
tQuarters.setText("");
tDimes.setText("");
tNickels.setText("");
tPennies.setText("");
tTwenty.setText("");
tTen.setText("");
tFive.setText("");
tOne.setText("");
tResult.setText("");
}
});
向计算按钮添加实现...
bCompute.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
double Amount = Double.parseDouble(tAmount.getText());
double q = (double) Integer.parseInt(tQuarters.getText()) *.25;
double d = (double) Integer.parseInt(tDimes.getText()) *.10;
double n = (double) Integer.parseInt(tNickels.getText()) *.05;
double p = (double) Integer.parseInt(tPennies.getText()) *.01;
double T = (double) Integer.parseInt(tTwenty.getText()) *20;
double Ten = (double) Integer.parseInt(tTen.getText()) *10;
double Five = (double) Integer.parseInt(tFive.getText()) *5;
double one = (double) Integer.parseInt(tOne.getText()) *1;
double TotalPaid = q+d+n+p+T+Ten+Five+1;
double diff = TotalPaid-Amount;
//Heres an example to create how many twenties you need
int totalTwenties = (int)diff /20;
diff = diff%20;
tTwenty.setText("" + totalTwenties);
}
});
基本上,我得到了支付金额和成本金额之间的差异。
A) 然后,通过/20 找到 20 可以容纳差异的次数。接下来,我取模或求出与二十之差的余数。
B) 要查找您需要找零的其他硬币/纸币的数量,请重复 A 中描述的过程。
关于java - GetChangeGui 手册 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36959720/
我正在尝试为我的 CS 1 类(class)编写代码。 代码的要点是编写一个代码,您可以在其中从一百美元的钞票中进行任何金额的找零,我需要归还适当的钞票和硬币。 我必须手动编写 JFrame 如果有人
我是一名优秀的程序员,十分优秀!