将十进制转换为罗马

将十进制转换为罗马

本文介绍了将十进制转换为罗马的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写一个小数值介于1和10之间的程序,并显示其等效的罗马数字值。如果输入的值超出可接受的范围,则显示错误消息。写一个两类解决方案。第二类应该允许用户输入测试值。



我无法得到输出和循环设置这个问题。



我尝试过:



Write a program that takes a decimal value between 1 and 10 and displays its equivalent Roman numeral value. Display an error message if the value entered is outside of the acceptable range. Write a two class solution. The second class should allow the user to input a test value.

I could not get the output and the loop set up in this problem.

What I have tried:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chapater5PA1
{
    class Program
    {
        static void Main(string[] args)
        {
            int value;
            string roman;

            DisplayInstrution();
            value = GetDecimal();

            roman = CalculateRoman(value);

            DisplayResults(value, roman);
            Console.ReadKey();
        }


        public static void DisplayInstrution()
        {
            Console.WriteLine("This application allows decimal value between 1 and 10");
            Console.WriteLine("to be entered. It displays the equivalent roman numeral value.");
            Console.ReadKey();
        }

        public static int GetDecimal()
        {
            int value;
            Console.WriteLine("Decimal value: ");
            value = Int32.Parse(Console.ReadLine());

            return value;
        }

        public static string CalculateRoman(int value)
        {
            string result;

            if (value < 0 || value > 10)
                result = "Invalid input";
            else
                if (value == 1)
                result = "I";
            else
                if (value == 2)
                result = "II";
            else
                if (value == 3)
                result = "III";
            else
                if (value == 4)
                result = "IV";
            else
                if (value == 5)
                result = "V";
            else
                if (value == 6)
                result = "VI";
            else
                if (value == 7)
                result = "VII";
            else
                if (value == 8)
                result = "VIII";
            else
                if (value == 9)
                result = "IX";
            else
                result = "IX";

            return result;

        }

        public static void DisplayResults(int value, string roman)
        {
            if (value != 0)
            {
                Console.WriteLine("Roman value: ", roman);
            }
        }
    }
  }

推荐答案

Console.WriteLine("Roman value: "+ roman);





当你不明白你的代码在做什么或为什么它做它的作用时,答案是调试器

使用调试器查看代码正在执行的操作。它允许你逐行执行第1行并在执行时检查变量,它是一个令人难以置信的学习工具。



[]

[]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做的比较。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。



When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于将十进制转换为罗马的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 13:33