gpt4 book ai didi

c# - 如何拆分字符串中的字符并在 C# 中执行求和

转载 作者:太空宇宙 更新时间:2023-11-03 19:48:53 24 4
gpt4 key购买 nike

我有一个值(value)为“12345”的刺痛我要做的是我必须执行拆分操作并使用 for 循环添加它喜欢:1+2+3+4+5然后在标签中显示操作的输出为 15。

我在这里用 asp.net 和 c# 发布我的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace proj1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
int v1 = 0, x = 0; //sum = 0, l, r;
// char Q, W, E, R, T, Y, U, I, O, P, A, S, D, F, G, H, J, K, L, Z, X, C, V, B, N, M;
string str = TextBox1.Text;
Label1.Text = ("Result");
Label2.Text = ("You have Entered");
Label5.Text = (":");
Label8.Text = str.ToString();
// Array word[] = new Array();
string str1 = str.Replace(" ", "").Replace("`", "").Replace("~", "").Replace("!", "")
.Replace("@", "").Replace("#", "").Replace("$", "").Replace("%", "").Replace("^", "")
.Replace("&", "").Replace("*", "").Replace("(", "").Replace(")", "").Replace("-", "")
.Replace("_", "").Replace("=", "").Replace("+", "").Replace("{", "").Replace("}", "")
.Replace("|", "").Replace("[", "").Replace("]", "").Replace(":", "").Replace(";", "")
.Replace("'", "").Replace(",", "").Replace(".", "").Replace("/", "").Replace("?", "")
.Replace(">", "").Replace("<", "").Replace("1", "").Replace("2", "").Replace("3", "")
.Replace("4", "").Replace("5", "").Replace("6", "").Replace("7", "").Replace("8", "")
.Replace("9", "").Replace("0", "");
string str2 = str1.ToUpper();
string str3 = str2.Replace("A", "1").Replace("B", "2").Replace("C", "3").Replace("D", "4").Replace("E", "5").Replace("F", "8")
.Replace("G", "3").Replace("H", "5").Replace("I", "1").Replace("J", "1").Replace("K", "2").Replace("L", "3")
.Replace("M", "4").Replace("N", "5").Replace("O", "7").Replace("P", "8").Replace("Q", "1").Replace("R", "2")
.Replace("S", "3").Replace("T", "4").Replace("U", "6").Replace("V", "6").Replace("W", "6").Replace("X", "5")
.Replace("Y", "1").Replace("Z", "7");
string word1 = str3.ToString();
String[] word = word1.ToString().Split();
for (int i = 0; i < word.Length; i++)
{
x = (( x + (Convert.ToInt32(word[i]))));
}
int value1 = x;

Label3.Text = ("Value Of Charecters");
Label6.Text = (":");
Label9.Text = value1.ToString();


Label4.Text = ("Summed Value");
Label7.Text = (":");
Label10.Text = ("");
}

}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="proj1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<h1>Project - 1</h1>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td class="auto-style1">Enter Your Text</td>
<td>:</td>
<td>
<asp:TextBox ID="TextBox1" runat="server" Width="362px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style1"></td>
<td></td>
<td>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />
</td>
</tr>
<tr>
<td class="auto-style1"></td>
<td></td>
<td></td>
</tr>
<tr>
<td class="auto-style1">
<asp:Label ID="Label1" runat="server"></asp:Label>
</td>
<td></td>
<td></td>
</tr>
<tr>
<td class="auto-style1"></td>
<td></td>
<td></td>
</tr>
<tr>
<td class="auto-style1">
<asp:Label ID="Label2" runat="server"></asp:Label>
</td>
<td>
<asp:Label ID="Label5" runat="server"></asp:Label>
</td>
<td>
<asp:Label ID="Label8" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td class="auto-style1">
<asp:Label ID="Label3" runat="server"></asp:Label>
</td>
<td>
<asp:Label ID="Label6" runat="server"></asp:Label>
</td>
<td>
<asp:Label ID="Label9" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td class="auto-style1">
<asp:Label ID="Label4" runat="server"></asp:Label>
</td>
<td>
<asp:Label ID="Label7" runat="server"></asp:Label>
</td>
<td>
<asp:Label ID="Label10" runat="server"></asp:Label>
</td>
</tr>

</table>

</div>
</form>
</body>

</html>

实际上我必须执行此任务

  1. 在文本框中输入字符。然后点击按钮
  2. 保存字符并显示输入
  3. 用excel表中给定的数字替换字符。
  4. 执行总和,如果 a=1,b=2,c=3,d=4,e=5 那么您应该显示 15 作为该总和之后的输出。
  5. 将总和中的值分开,比如 15 为 1 和 5,它们是另一个函数的输入,用于总计,直到最终总和应该是个位数
  6. 生成exe文件

最佳答案

我建议使用 Linq:

  using System.Linq;

...

string source = "12345";

myLabel.Text = source
.Select(c => c - '0') // character to integer '0' -> 0 .. '9' -> 9
.Sum() // sum up
.ToString(); // represent as string

关于c# - 如何拆分字符串中的字符并在 C# 中执行求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42062709/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com