有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何切换end函数?

如中所示,“X美元转换为Y俄罗斯卢布”(在两位数的新十进制格式之后)的值,问题是它可以是俄罗斯卢布、英镑或欧元。我如何区分?(插入相关行的文本)

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.text.DecimalFormat;

public class CurrencyConversion extends Applet implements ItemListener
{
 //declare variables and color
 double dollars, answer;
 int empCode;
 Image dollarSign;
 Color darkRed = new Color(160, 50, 0);

 //Create components for applet
 Label promptLabel = new Label("Enter the dollar amount (do not use commas or dollar signs):");
  TextField currencyField = new TextField(20);

 Label codeLabel = new Label ("Select the desired currency:");

 CheckboxGroup codeGroup = new CheckboxGroup () ;
  Checkbox britishpoundBox = new Checkbox("British Pound",false,codeGroup);
  Checkbox euroBox = new Checkbox("Euro",false,codeGroup);
  Checkbox russianrubleBox = new Checkbox("Russian Ruble",false,codeGroup);
  Checkbox hiddenBox = new Checkbox("",true,codeGroup);


 Label outputLabel = new Label("Click an option to convert the desired currency.");

 public void init()
 {
  setBackground(darkRed);
  setForeground(Color.white);
  add(promptLabel);
  add(currencyField);
  currencyField.requestFocus();
  currencyField.setForeground(Color.black);
  add(codeLabel);
  add(britishpoundBox);
  britishpoundBox.addItemListener(this);
  add(euroBox);
  euroBox.addItemListener(this);
  add(russianrubleBox);
  russianrubleBox.addItemListener(this);
  add(outputLabel);
 }

 //This method is triggered by click
 public void itemStateChanged(ItemEvent choice)
 {
  try
  {
   dollars = getCurrency();
   empCode = getCode();
   answer = getComm(dollars,empCode);
   output(answer, dollars);
  }

  catch (NumberFormatException e)
  {
   outputLabel.setText("You must enter a dollar amount greater than zero.");
   hiddenBox.setState(true);
   currencyField.setText("");
   currencyField.requestFocus();
  }
 }

 public double getCurrency()
 {
  double currency = Double.parseDouble(currencyField.getText());

  if (currency <= 0) throw new NumberFormatException();

  return currency;
 }

 public int getCode()
 {
  int code = 0;
  if (britishpoundBox.getState()) code = 1;
  else
   if (euroBox.getState()) code = 2;
   else
    if (russianrubleBox.getState()) code = 3;
  return code;
 }

 public double getComm(double currency, int code)
 {
  double amount = 0.0;
  switch(code)
  {
   case 1:
    amount = .79610 * currency;
    break;
   case 2:
    amount = .70880 * currency;
    break;
   case 3:
    amount = 35.88240 * currency;
    break;
  }
  return amount;
 }

 public void output(double amount, double currency)
 {
  DecimalFormat twoDigits = new DecimalFormat("##.00");
  outputLabel.setText("Your amount of " + twoDigits.format(currency) + " dollars converts to " + twoDigits.format(amount) RIGHT HERE - what do I do? );
 }

 public void paint(Graphics g)
 {
  dollarSign = getImage(getDocumentBase(), "dollarsign.gif");
  g.drawImage(dollarSign,12,28,this);
 }
}

共 (3) 个答案

  1. # 1 楼答案

    我建议您使用java类。util。通货每种货币的汇率不应硬编码。对于第一阶段,只需将其放入属性文件,即: 摩擦=35.88240 英镑=0.79610

    避免使用硬编码的货币列表。而是解析这个属性文件,提取所有密钥,然后使用货币。getInstance(currencyCode)。UI和逻辑都是通用的。您的应用程序将至少缩短两倍,并且更加灵活

  2. # 2 楼答案

    创建从IntegerString的映射,用各种货币描述填充它,并将Integer传递给输出函数,以便从映射中获取描述

  3. # 3 楼答案

    将货币概念包装到自己的对象中。重写ToString()方法并返回货币名称,或定义显式属性以返回货币类型。传递currency对象,而不是不太复杂的double,并将以前的double属性设置为对象上的属性