c# 题目:输入三个整数x,y,z,请把这三个数由小到大输出。
时间: 2024-06-13 12:07:50 浏览: 12
以下是C#中将三个整数由小到大输出的代码:
```csharp
int x = 3, y = 2, z = 1;
int temp;
if (x > y)
{
temp = x;
x = y;
y = temp;
}
if (x > z)
{
temp = x;
x = z;
z = temp;
}
if (y > z)
{
temp = y;
y = z;
z = temp;
}
Console.WriteLine("{0} {1} {2}", x, y, z); // 输出:1 2 3
```
相关问题
使用c#键盘输入三个数,在从大到小输出
可以回答这个问题。
您可以使用C#编写以下代码,以从键盘输入三个数字,并按照从大到小的顺序输出它们:
```csharp
using System;
public class Program {
public static void Main() {
Console.Write("请输入第一个数:");
int num1 = int.Parse(Console.ReadLine());
Console.Write("请输入第二个数:");
int num2 = int.Parse(Console.ReadLine());
Console.Write("请输入第三个数:");
int num3 = int.Parse(Console.ReadLine());
int max = num1;
int mid = num2;
int min = num3;
if (num2 > max) {
max = num2;
mid = num1;
}
if (num3 > max) {
max = num3;
mid = num1;
min = num2;
}
if (num1 < mid) {
mid = num1;
min = num2;
}
if (num2 < min) {
min = num2;
}
Console.WriteLine($"从大到小的顺序为:{max} {mid} {min}");
}
}
```
代码解释:
1. 首先,我们通过 `Console.ReadLine()` 方法从控制台输入三个数,并将它们存储在 `num1`、`num2` 和 `num3` 变量中。
2. 然后,我们定义三个变量 `max`、`mid` 和 `min`,分别表示这三个数的最大值、中间值和最小值。
3. 接着,我们使用条件语句判断每个数与其他两个数的大小关系,以确定它们在从大到小的顺序中的位置,并将它们分别存储在 `max`、`mid` 和 `min` 变量中。
4. 最后,我们使用 `Console.WriteLine()` 方法输出这三个数的从大到小的顺序。
这样,您就可以使用 C# 输入三个数并按从大到小的顺序输出它们了。
输入三个数将最大的数和最小的数求和后输出用c#语言转化为代码
以下是用 C# 语言编写的求最大值和最小值的和的代码:
```
using System;
class Program {
static void Main(string[] args) {
int[] nums = new int[3];
Console.WriteLine("请输入三个整数:");
for (int i = 0; i < nums.Length; i++) {
nums[i] = Convert.ToInt32(Console.ReadLine());
}
int max = nums[0], min = nums[0];
for (int i = 1; i < nums.Length; i++) {
if (nums[i] > max) {
max = nums[i];
}
if (nums[i] < min) {
min = nums[i];
}
}
int sum = max + min;
Console.WriteLine("最大值和最小值的和为:" + sum);
Console.ReadLine();
}
}
```
运行程序后,会提示输入三个整数,然后输出最大值和最小值的和。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)