请用C#帮我写代码:输入一个成绩(0到100之间的整数) 如果大于等于90输出:优秀; 小于90而大于等于80输出:良好; 小于80而大于等于70输出:一般; 小于70而大于等于60输出:及格; 否则输出:不及格.
时间: 2023-08-24 18:18:29 浏览: 56
下面是C#代码实现:
```
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入成绩(0到100之间的整数):");
int score = Convert.ToInt32(Console.ReadLine());
if (score >= 90)
{
Console.WriteLine("优秀");
}
else if (score >= 80)
{
Console.WriteLine("良好");
}
else if (score >= 70)
{
Console.WriteLine("一般");
}
else if (score >= 60)
{
Console.WriteLine("及格");
}
else
{
Console.WriteLine("不及格");
}
Console.ReadLine();
}
}
```
注意:此代码只是简单的实现了题目所要求的功能,实际应用中还需要进行输入值的合法性检查等处理。
相关问题
用C# 输入一个成绩(0到100之间的整数) 如果大于等于90输出:优秀; 小于90而大于等于80输出:良好; 小于80而大于等于70输出:一般; 小于70而大于等于60输出:及格; 否则输出:不及格.
可以使用以下代码实现:
```csharp
using System;
class Program {
static void Main(string[] args) {
Console.Write("请输入一个成绩(0到100之间的整数):");
int grade = int.Parse(Console.ReadLine()); // 从控制台读取一个整数
if (grade >= 90) {
Console.WriteLine("优秀");
} else if (grade >= 80) {
Console.WriteLine("良好");
} else if (grade >= 70) {
Console.WriteLine("一般");
} else if (grade >= 60) {
Console.WriteLine("及格");
} else {
Console.WriteLine("不及格");
}
}
}
```
这段代码中,我们首先使用 `Console.Write()` 函数提示用户输入一个成绩,并使用 `Console.ReadLine()` 方法从控制台读取一个字符串,再使用 `int.Parse()` 方法将字符串转换为整数,并将其存储在变量 `grade` 中。接着,我们使用多重 `if...else` 语句判断 `grade` 的取值范围,并分别打印出不同的信息。如果 `grade` 小于 60,那么就打印出 "不及格"。
2. 编一个程序,输入一个整数,使用if else语句,如果该数大于0,则加上100,否则加上500,输出结果。
好的,以下是一个使用 if-else 语句实现的 C# 程序,可以根据用户输入的整数进行条件判断,然后输出相应的结果:
```csharp
using System;
class Program
{
static void Main(string[] args)
{
// 获取用户输入的整数
Console.WriteLine("请输入一个整数:");
int num = int.Parse(Console.ReadLine());
// 根据条件进行判断,并输出结果
if (num > 0)
{
num += 100;
Console.WriteLine("输入的整数大于0,加上100的结果为:" + num);
}
else
{
num += 500;
Console.WriteLine("输入的整数小于等于0,加上500的结果为:" + num);
}
// 等待用户按下任意键退出程序
Console.ReadKey();
}
}
```
以上程序首先使用 `Console.ReadLine()` 获取用户输入的整数,然后使用 if-else 语句进行条件判断。如果输入的整数大于0,则将其加上100,并输出结果;否则将其加上500,并输出结果。
注意,上述程序中使用了 `int.Parse()` 方法将用户输入的字符串转换为整数类型。如果用户输入的是无效的整数字符串,程序会抛出异常。为了使程序更健壮,可以加上一些异常处理机制。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)