0

我正在尝试在C#中创建一个应用程序,将文本框中的数字转换为标签控件中的罗马数字,并且需要使用 case 语句。但是,我的变量Roman之一收到错误消息:使用未分配的局部变量“Roman”。

这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Roman_Numeral_Converter
 {
  public partial class Form1 : Form
  {   
     public Form1()
    {
        InitializeComponent();
    }

    private void btnCalc_Click(object sender, EventArgs e)
    {

        int Number=int.Parse(txtNum.Text); // To hold Number
        string Roman; // To hold Roman Numeral 

        if (Number>=1 && Number <=10)
        {
            switch (Roman)
            {
                case "Number==1":
                lblRoman.Text = "I";
                break;
                case "Number==2":
                lblRoman.Text = "II";
                break;
                case "Number==3":
                lblRoman.Text = "III";
                break;
                case "Number==4":
                lblRoman.Text = "IV";
                break;
                case "Number==5":
                lblRoman.Text = "V";
                break;
                case "Number==6":
                lblRoman.Text = "VI";
                break;
                case "Number==7":
                lblRoman.Text = "VII";
                break;
                case "Number==8":
                lblRoman.Text = "VIII";
                break;
                case "Number==9":
                lblRoman.Text = "IX";
                break;
                case "Number==10":
                lblRoman.Text = "X";
                break;
            }
        }
        else
        {
            MessageBox.Show("Error: Invalid Input");
        }

    }

    private void btnExit_Click(object sender, EventArgs e)
    {
        // Close the form.
        this.Close();
    }

    private void btnClear_Click(object sender, EventArgs e)
    {
        txtNum.Text = "";
        lblRoman.Text = "";
    }
}

}

4

5 回答 5

3

你的结构有点不对劲。

private void btnCalc_Click(object sender, EventArgs e)
{

    var Number = int.Parse(txtNum.Text); // To hold Number

    switch (Number)
    {
            case 1:
              lblRoman.Text = "I";
              break;
            case 2:
              lblRoman.Text = "II";
              break;
            case 3:
              lblRoman.Text = "III";
              break;
            case 4:
              lblRoman.Text = "IV";
              break;
            case 5:
              lblRoman.Text = "V";
              break;
            case 6:
              lblRoman.Text = "VI";
              break;
            case 7:
              lblRoman.Text = "VII";
              break;
            case 8:
              lblRoman.Text = "VIII";
              break;
            case 9:
              lblRoman.Text = "IX";
              break;
            case 10:
              lblRoman.Text = "X";
              break;
            default:
              MessageBox.Show("Error: Invalid Input");
              break;
    }

}

您正在使用lblRoman来保存结果,因此您的Roman变量是不必要的。此外,由于您正在查询 中的每个可能的有效数字switch,因此您可以使用default替换if/else结构。

我假设您将其作为学术练习。话虽如此,如果我不向您指出 Mosè Bottacini 对这个问题的解决方案,那就太失职了。

于 2013-09-18T04:44:51.843 回答
0

尝试这个,

当你的数字值像1这样的罗马数字时I

private void btnCalc_Click(object sender, EventArgs e)
{
        int Number = int.Parse(txtNum.Text); // To hold Number
        string Roman; // To hold Roman Numeral

        if (Number >= 1 && Number <= 10)
        {
            switch (Number)
            {
                case 1:
                    lblRoman.Text = "I";
                    break;

                case 2:
                    lblRoman.Text = "II";
                    break;

                case 3:
                    lblRoman.Text = "III";
                    break;

                case 4:
                    lblRoman.Text = "IV";
                    break;

                case 5:
                    lblRoman.Text = "V";
                    break;

                case 6:
                    lblRoman.Text = "VI";
                    break;

                case 7:
                    lblRoman.Text = "VII";
                    break;

                case 8:
                    lblRoman.Text = "VIII";
                    break;

                case 9:
                    lblRoman.Text = "IX";
                    break;

                case 10:
                    lblRoman.Text = "X";
                    break;
            }
        }
        else
        {
            MessageBox.Show("Error: Invalid Input");
        }
}
于 2013-09-18T04:46:50.873 回答
0

这是因为 Roman 变量实际上是未赋值的。您应该在输入语句之前分配它

于 2013-09-18T04:50:02.580 回答
0

您可以通过这种方式替换您的 switch 语句。当然你需要在使用它之前分配一个变量。

public string GetNum(string val)
{
    string res = ""; // Assign it an empty string. 
    var numToRom = new Dictionary<string, string>
                       {
                           {"1","I"},
                           {"2","II"}
                           //so on
                       };

        numToRom.TryGetValue(val, out res);
        return res;
}
于 2013-09-18T06:17:57.810 回答
0

而不是switch, 你可以做其他方式使用Linqwhich 更好。

int Number=int.Parse(txtNum.Text);
var romanList = new List<string> {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X"};
if (Number >= 1 && Number <= 10)
lblRoman.Text = romanList.Select((r, i) => new { Roman = r, Index = i+1}).FirstOrDefault(x=> x.Index == Number).Roman;
于 2013-09-18T05:47:16.633 回答