这是我遇到麻烦的家庭作业.
我需要使用方法为Roman Numeral转换器创建一个整数.之后,我必须使用该程序用罗马数字写出1到3999,所以硬编码就出来了.我的代码非常简单; 它是一个基本的I/O循环,在使用getIntegerFromUser我们在课堂上制作的包时可以退出.
有没有办法为字符串赋值,然后在调用方法时将它们一起添加?
更新:我从我的教授那里得到了一些伪代码来帮助我,虽然我明白他想说的是什么,但是我遇到了麻烦if.我是否需要许多if语句,以便我的转换器能够正确处理罗马数字格式或者是否有一种方式可以更有效地执行此操作?我已更新我的代码以反映我的占位符方法.
更新(2012年10月28日):我得到了它的工作.这是我最终使用的内容:
public static String IntegerToRomanNumeral(int input) {
if (input < 1 || input > 3999)
return "Invalid Roman Number Value";
String s = "";
while (input >= 1000) {
s += "M";
input -= 1000; }
while (input >= 900) {
s += "CM";
input -= 900;
}
while (input >= 500) {
s += "D";
input -= 500;
}
while (input >= 400) {
s …Run Code Online (Sandbox Code Playgroud) 我在计算机科学课程的编程工作中遇到了麻烦.该计划是添加罗马数字.我不知道如何开始实际添加罗马数字的代码并输出两个数字的总和:这是我目前为主程序的代码:
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"); …Run Code Online (Sandbox Code Playgroud)