找到 2628 篇文章 關於 C#
234 次瀏覽
我們示例中的數字是 11,即二進位制 - 1101,1101 中設定的位總數為 3;要找到它,請使用迴圈,直到它不等於 0。這裡,我們的 num 是 11,即十進位制 -while (num>0) { cal += num & 1; num >>= 1; }示例要計算數字中設定的位總數,請使用以下程式碼。線上演示using System; public class Demo { public static void Main() { int cal = 0; // 二進位制為 1011 int num = 11; while (num>0) { cal += num & 1; num >>= 1; } // 1101 中的 1 位為 3 Console.WriteLine("位總數: "+cal); } }輸出位總數: 3
467 次瀏覽
假設我們擁有的數字是 12。我們已經宣告並初始化了一個 uint 變數,併為其分配了一個十進位制字面量,uint val = 12;12 的二進位制表示 - 1100上面的位是 4,因此要查詢總位數,請使用 Math.log() 方法 -uint res = (uint)Math.Log(val , 2.0) + 1;示例您可以嘗試執行以下程式碼來計算數字中的總位數。線上演示using System; public class Demo { public static void Main() { uint val = 12; // 二進位制為 1100 uint res = (uint) Math.Log(val, 2.0) + 1; // 1100 有 4 位 Console.WriteLine("位總數: " + res); } }輸出位總數: 4
27K+ 次瀏覽
首先,設定字串 -string str = "Website"; Console.WriteLine("字串: "+str);檢查字串中的每個字元,並遞增一個變數,該變數將計算該字元出現的次數 -for (int j = 0; j < str.Length; j++) { if (str[0] == str[j]) { cal++; } }示例您可以嘗試執行以下程式碼來計算每個字元出現的次數。線上演示using System; public class Demo { public static void Main() { string str = "Website"; Console.WriteLine("字串: "+str); while (str.Length > 0) { ... 閱讀更多
457 次瀏覽
我使用陣列添加了數字 -int[] num = new int[] {1, 25, 1, 55, 1};現在迴圈遍歷並查詢 1,如果存在 1,則遞增計算出現次數的變數 -foreach(int j in num) { if (j == 1) { cal++; } }示例以下是計算輸入數字中 1 的個數的程式碼。線上演示using System; public class Demo { public static void Main() { int cal = 0; int[] num = new int[] {1, 25, 1, 55, ... 閱讀更多
649 次瀏覽
您需要檢查母音和子音,但不要忘記檢查大寫和小寫。對於計算母音,分別檢查“aeiou”字元,即if (myStr[i] == 'a' || myStr[i] == 'e' || myStr[i] == 'i' || myStr[i] == 'o' || myStr[i] == 'u' || myStr[i] == 'A' || myStr[i] == 'E' || myStr[i] == 'I' || myStr[i] == 'O' || myStr[i] == 'U') { vowel_count++; }對於計算子音,在 elseif 條件下檢查其他字元 -else if ((myStr[i] >= 'a' && myStr[i] = 'A' && myStr[i]
619 次瀏覽
設定十進位制數 -int decVal = 40;現在取一個變數並在其中設定 decVal。找到 8 的餘數,因為八進位制具有基數為 8 的數字系統,並在迴圈中對其進行評估,如下面的程式碼片段所示。while (quot != 0) { octalVal[i++] = quot % 8; quot = quot / 8; }示例您可以嘗試執行以下程式碼將十進位制轉換為八進位制數。線上演示using System; class Demo { public static void Main() { int decVal, quot, i = 1, j; int[] octalVal = new int[80]; ... 閱讀更多
2K+ 次瀏覽
首先,設定華氏溫度 -double fahrenheit = 97; Console.WriteLine("華氏溫度: " + fahrenheit);現在將其轉換為攝氏溫度 -celsius = (fahrenheit - 32) * 5 / 9;示例您可以嘗試執行以下程式碼將華氏溫度轉換為攝氏溫度。線上演示using System.Collections.Generic; using System.Linq; using System.Text; namespace Demo { class MyApplication { static void Main(string[] args) { double celsius; double fahrenheit = 97; Console.WriteLine("華氏溫度: " + fahrenheit); celsius = (fahrenheit - 32) * 5 / 9; Console.WriteLine("攝氏溫度: " + celsius); Console.ReadLine(); } } }輸出華氏溫度: 97 攝氏溫度: 36.1111111111111
1K+ 次瀏覽
假設您已將十進位制設定為 -decVal = 34; Console.WriteLine("十進位制: {0}", decVal);對於您獲得的十進位制值的二進位制數字,使用 ToString() 方法 -while (decVal >= 1) { val = decVal / 2; a += (decVal % 2).ToString(); decVal = val; }現在設定一個新的空變數,使用迴圈顯示二進位制數字 -string binValue = "";示例您可以嘗試執行以下程式碼在 C# 中將十進位制轉換為二進位制。線上演示using System.Collections.Generic; using System.Text; namespace Demo { class MyApplication { ... 閱讀更多
3K+ 次瀏覽
首先,宣告從 0 到 9 的單詞 -// 從 0 到 9 的每個數字的單詞 string[] digits_words = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };以下是將要轉換為單詞的數字 -// 要轉換為單詞的數字 val = 4677; Console.WriteLine("數字: " + val);使用迴圈檢查給定數字中的每個數字,並將其轉換為單詞 -do { next = val % 10; a[num_digits] = next; num_digits++; val = val / 10; } while(val > 0);示例您可以嘗試 ... 閱讀更多
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP