2017-02-06 1 views
1

"12345" として分割する操作を実行してforループを使用して追加する必要があります。 が好き:1 + 2 + 3 + 4 + 5 、その後、私はasp.netとC#でここに私のコードを掲載しています15.文字列内の文字を分割して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を出力として表示する必要があります。 :合計の値は、私がLINQのを使用することをお勧めのexeファイル
+1

コードの聖なる壁、バットマンであるJavaスクリプトではありません。 – Abion47

答えて

4

を生成する15の最終集計まで、合計のために別の機能のための入力である1とし、5は一桁

  • あるべきと言うセパレート
  • 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 
    
  • +0

    あなたが気にしていないならば、私はこの質問に自己返信することで人々の機能参照のためのより明確で直接的な答えを与えるためのコードを追加したいと思います。 –

    +0

    @ROHIT MANDALAPU:あなたが何をすべきかを知っているので実装されました。ですから、コード、コメント、ソリューションを自由に提供してください:) –

    0

    あなたは試すことができます:

    string source = "12345"; 
    var sum = 0; 
    foreach (var element in source) 
        sum += Convert.ToInt32(element.ToString()); 
    
    1

    このスクリプトは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) 
     
         { 
     
          string str = TextBox1.Text; 
     
          Label1.Text = ("Result"); 
     
          Label2.Text = ("You have Entered"); 
     
          Label5.Text = (":"); 
     
          Label8.Text = str.ToString(); 
     
          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 a = word1.Select(c => c - '0').Sum().ToString(); 
     
          Label3.Text = ("Sum Of Charecters"); 
     
          Label6.Text = (":"); 
     
          Label9.Text = a.ToString(); 
     
          string w = Label9.ToString(); 
     
          string s = w.ToString(); 
     
          string b = a.Select(c => c - '0').Sum().ToString(); 
     
          for (int i = 0; i < b.Length; i++) 
     
          { 
     
           string p = b.Select(c => c - '0').Sum().ToString(); 
     
           Label10.Text = p.ToString(); 
     
          } 
     
          Label4.Text = ("Summed Value"); 
     
          Label7.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>

    関連する問題