有没有人在加油站或杂货店使用过这些机器,在那里您可以通过捐赠可回收元素来赚钱?好吧,我想制作一个虚拟的,到目前为止一切都很好,直到我不得不做一些数学运算。我只有 13 岁,所以这部分非常棘手,尽管我认为它会很简单。我需要将可回收类型的值(value)乘以数量,然后加到总金额中。但不是将它添加到总金额中,它似乎只是将总金额更改为我最近添加的值。假设我加了 2 jar ,即 10 美分,然后我又加了一 jar ,而不是总共 15 美分,我只有 5 美分。希望你明白。我还想对我的代码提出一些建设性的批评。我知道这不是最好的,但我刚刚开始学习 java,所以任何帮助都会很可爱。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Machine {
static JLabel label;
static JComboBox typeList;
static JComboBox amountList;
public static void GUI(){
JFrame frame = new JFrame("Recyclables Machine");
frame.setVisible(true);
frame.setSize(300,125);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
Integer[] amounts = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50};
amountList = new JComboBox(amounts);
panel.add(amountList);
String[] types = {"Choose Recycable Type","Plastic Bottle","Can","2 Liter","Glass Bottle"};
typeList = new JComboBox(types);
panel.add(typeList);
JButton button = new JButton("Add");
panel.add(button);
label = new JLabel("Total Money: 0 cents");
panel.add(label);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
gettinItDone();
}
});
}
public static void gettinItDone(){
String type = (String)typeList.getSelectedItem();
int amount = (int)amountList.getSelectedItem();
int money = 0;
int temp = 0;
if(type.equals("Plastic Bottle")){
temp = 5 * amount;
money = temp + money;
label.setText("Total Money: "+ money +" cents");
}else{
if(type.equals("Can")){
temp = 5 * amount;
money = temp + money;
label.setText("Total Money: "+ money +" cents");
}else{
if(type.equals("2 Liter")){
temp = 10 * amount;
money = temp + money;
label.setText("Total Money: "+ money +" cents");
}else{
if(type.equals("Glass Bottle")){
temp = 10 * amount;
money = temp + money;
label.setText("Total Money: "+ money +" cents");
}else{
JOptionPane.showMessageDialog(null,"Invalid Recyclable Type", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
}
}
}
最佳答案
“money”变量的范围仅在事件监听器触发时有效。
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
gettinItDone();
}
});
您需要将资金存储在#getIniitDone 方法的范围之外。
关于java - 数学 "equations"无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19124346/
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request