【问题标题】:how to add roman numbers in java如何在java中添加罗马数字
【发布时间】:2013-11-05 15:09:47
【问题描述】:

我在完成计算机科学课程的编程作业时遇到了问题。该程序是添加到罗马数字。我不知道如何开始使用实际将罗马数字相加并输出两个数字之和的代码:这是我迄今为止的主程序代码:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

/**
 * Class RomanNumeralCalculator - write a description of the class here
 * 
 * @author (your name) 
 * @version (a version number)
*/
public class RomanNumeralCalculator extends JFrame
{
   public static JPanel panel1, panel2, panel3, panel4;
   public static JLabel main1, main2, label1, label2,label3,label4,image;
   public static JTextField number1, number2;
   public static JTextArea output;
   public static JButton calculate,exit;
   public static String input="Enter"; 
   public static int position;
String num="";
public RomanNumeralCalculator()
{
    super ("Roman Numeral Calculator");
    setSize(1045,740);
    setLocation(0,0);
    Container container = getContentPane();
    container.setBackground(Color.GREEN);

    JLabel image=new JLabel(new ImageIcon("romanNumerals.jpg"));

    panel1=new JPanel();
    panel2=new JPanel();
    panel3=new JPanel();
    panel4=new JPanel();

    panel1.setLayout(new GridLayout(2,7));
    panel1.setBackground(Color.GREEN);
    panel2.setLayout(new FlowLayout());
    panel2.setBackground(Color.MAGENTA);
    panel3.setLayout(new FlowLayout());
    panel3.setBackground(Color.YELLOW);
    panel4.setLayout(new FlowLayout());
    panel4.setBackground(Color.GRAY);

    JLabel main1=new JLabel("Welcome To The Roman Numerals Calculator");
    JLabel main2=new JLabel("BY: Harpreet Singh, Date: 31/10/2013");

    label3=new JLabel("Here are the following numbers that can be used in the calculator:");
    label4=new JLabel("I=1 , V=5 , X=10 , L=50 , C=100 , D=500 , M=1000");

    calculate=new JButton("Calculate");
    exit=new JButton("Exit");

    ButtonHandler handler = new ButtonHandler();
    calculate.addActionListener(handler);
    exit.addActionListener(handler);

    label1=new JLabel("Enter first number in roman numerals:");
    number1=new JTextField("",10);
    label2=new JLabel("Enter second number in roman numerals:");
    number2=new JTextField("",10);

    output=new JTextArea(10,100);
    output.setEditable(false);

    container.setLayout(new BorderLayout());
    container.add(panel1, BorderLayout.NORTH);
    container.add(panel2, BorderLayout.CENTER);
    container.add(panel3, BorderLayout.SOUTH);

    panel1.add(main1);
    panel1.add(main2);
    panel1.add(image);
    panel2.add(label3);
    panel2.add(label4);
    panel3.add(label1);
    panel3.add(number1);
    panel3.add(label2);
    panel3.add(number2);
    panel3.add(calculate);        
    panel3.add(exit);

    setVisible(true);
}

public class ButtonHandler implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource()==calculate)
        {
            String num1=number1.getText();
            String num2=number2.getText();
            CalculateNumbers x=new CalculateNumbers();
            x.calculations(num1,num2);
        }
        else if (e.getSource()==exit)
        {
            System.exit(0);
        }
    }
}

public static void main (String args[])
{
    /*String choice=JOptionPane.showInputDialog(null, "Please Type In 'Enter' to Continue: ", "Enter", JOptionPane.PLAIN_MESSAGE);
    if ((choice == null) || ((choice != null) && !(choice.equalsIgnoreCase(input))))
    {
    JOptionPane.showMessageDialog(null, "Invalid Password. Please Try Again.");
    System.exit(0);
    }*/
    //else
    //{
    //JOptionPane.showMessageDialog(null, "Correct Password");
    RomanNumeralCalculator application = new RomanNumeralCalculator();
    //}
}

}

【问题讨论】:

  • 提示:首先将它们转换为常规数字。
  • 你需要两个函数:一个接受一个字符串(罗马数字)并返回一个数字,另一个接受一个数字并返回一个字符串(罗马数字)。因此,您将获取两个输入,将它们传递给您的第一个函数以转换为数字,将它们相加,然后将结果传递给您的第二个函数以获取罗马数字的结果。
  • 使用开关、映射或实现解析器来转换数字。
  • 我该怎么做,因为我输入数字的主要方式是文本字段

标签: java roman-numerals


【解决方案1】:

先写两个方法

  1. 将罗马数字转换为普通数字
  2. 将常规数字转换为罗马数字

当用户提供罗马数字输入时,将其转换为常规数字,执行算术运算,最后将结果转换为罗马数字

同时检查Converting Roman Numerals To DecimalConvert Int to Roman Numeral

【讨论】:

  • 我不能用两种方法代替
  • 你能帮我开始使用一种方法吗,因为我是 Java 新手,并且对我将如何做到这一点感到困惑
  • 我明白这将是一项艰巨的任务。我分享的链接有答案和解释,请浏览一下
  • 你能帮我把整数转换成等效的罗马数字吗
【解决方案2】:

创建一个罗马数字类,它将在给定十进制数字的情况下转换您的数字,反之亦然。如果需要,您甚至可以将其设为静态。

这将使您的代码看起来更好,更面向对象。

这里有一个算法解释。

roman number algorithm

【讨论】:

    【解决方案3】:
           import java.util.Scanner;
    
            public class Roman {
    
            public static void main(String[] args) {
    
            Scanner stdIn = new Scanner(System.in);
    
             System.out.print("Enter a Roman Number:> ");
    
              char[] roman = stdIn.nextLine().toCharArray();
    
        int total = 0;
        for(int i = roman.length-1; i > -1; i--){
            switch(roman[i]){
                case 'I':
                    total += value(roman[i]); break;
                case 'V':
                case 'X':
                case 'L':
                case 'C':
                case 'D':
                case 'M':
                    if(i != 0 && (value(roman[i-1]) < value(roman[i]))){
                        total += value(roman[i]) - value(roman[i-1]);
                        i--;
                    }else{
                        total += value(roman[i]);
                    }
                    break;
            }
        }
        System.out.println(total);
    }
    
    public static int value(char c){
        switch(c){
            case 'I':
                return 1;
            case 'V':
                return 5;
            case 'X':
                return 10;
            case 'L':
                return 50;
            case 'C':
                return 100;
            case 'D':
                return 500;
            case 'M':
                return 1000;
            default:
                return 0;
        }
    } }
    

    【讨论】:

    • 编译后当出现“输入罗马数字”的信息时,输入例如:M+V会添加
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-09
    • 2012-02-24
    • 2020-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多