c#输入一个三位数,计算并输出各位数之和
时间: 2024-09-20 12:16:07 浏览: 80
在C#中,你可以编写一个小程序来接收用户的输入,并将其解析为一个三位数,然后计算每位数的和。下面是一个简单的示例:
```csharp
using System;
class Program
{
static void Main(string[] args)
{
Console.Write("请输入一个三位数: ");
string input = Console.ReadLine(); // 获取用户输入的字符串
int number;
if (int.TryParse(input, out number) && number >= 100 && number <= 999)
{
int hundreds = number / 100; // 取百位
int tens = (number % 100) / 10; // 取十位
int units = number % 10; // 取个位
int sumOfDigits = hundreds + tens + units; // 各位数相加
Console.WriteLine($"输入的三位数各位数之和是: {sumOfDigits}");
}
else
{
Console.WriteLine("无效的输入,请输入一个三位数。");
}
阅读全文
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20250102104920.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)