切换案例陈述

时间:2013-09-18 04:42:43

标签: c#

我正在尝试在 C#中创建一个应用程序,它将文本框中的数字转换为标签控件中的罗马数字,并需要使用case语句。但是我的变量之一罗马会收到错误消息:使用未分配的局部变量'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 = "";
    }
}

}

5 个答案:

答案 0 :(得分: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's solution to this problem.

答案 1 :(得分: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");
        }
}

答案 2 :(得分:0)

这是因为罗马变量确实是未分配的。您应该在输入声明之前指定它

答案 3 :(得分:0)

而不是switch,您可以使用更好的Linq做其他方式。

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;

答案 4 :(得分: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;
}