在3号条件下一直崩溃
本文关键字:崩溃 一直 条件下 3号 | 更新日期: 2023-09-27 18:02:27
所以我是一种新的编程(16 y/o),我一直在解决问题一段时间了,但现在我正在解决这个问题(这很容易)似乎只有一件事在我的路上。这个问题很简单,它只需要我读取3个整数并打印出最大的一个,我已经在c++/C中解决了它,没有任何问题。但是这一个,当前两个if条件为真时,它打印出来并正常工作(如果a是最大值,它打印a,如果b是最大值,它打印b)但是当c是最大值时,它输出为空。很抱歉发了这么长的帖子,希望有人能帮助我。
using System; MaxP351名称空间{类MainClass{public static void Main (string[] args){string[] input =控制台。ReadLine()。Split (' ');//获取字符串形式的输入int[] nums = new int[input.Length];//声明与输入长度相同的int数组For (int I = 0;我& lt;input.Length;i++){//开始for循环将输入字符串转换为nums中的int []nums[i] = Convert.ToInt32(input[i]);}a, b, c;A = nums [0];B = nums [1];C = nums [2];If (a> b &&A> c) {控制台。WriteLine(一个);打破;} else if (b> a &&B> c) {控制台。WriteLine (b);打破;} else if (c> a &&C> b) {控制台。WriteLine (c);打破;}
} }
之前
如果c
确实是最大的数字,那么您的代码应该打印出c
的值。如果这是我的代码,我会在你的if
语句中设置一个断点,看看值是什么。或者,您可以在if语句之前打印出a,b和c的值,并确保它们是您期望的值。例子:
Console.WriteLine("Values before if statement:")
Console.WriteLine (a);
Console.WriteLine (b);
Console.WriteLine (c);
if (a > b && a > c) {
...
再次-我建议采用断点路线,但如果你不熟悉visual studio调试,上面可能是一种快速而肮脏的方式来查看值。
总之-你的代码应该给你想要的输出。问题很可能出在其他地方。
1。不需要break;对于if-else语句
2。根本不需要if语句来显示最大的整数。你可以这样做:
Console.WriteLine(nums.Max())
3。如果你不想使用Max函数。做一个适当的循环,这比太多的if条件要好;
int? maxVal = null; // ? means nullable int. Set null just to initialize
for (int i = 0; i < nums.Length; i++)
{
int currentNum = nums[i];
if (!maxVal.HasValue || currentNum > maxVal.Value)
{
maxVal = currentNum;
}
}
Console.WriteLine(maxVal);
当你使用if-else if时,你不需要'break'。下面应该有帮助
if (a > b && a > c) {
Console.WriteLine (a);
} else if (b > a && b > c) {
Console.WriteLine (b);
} else {
Console.WriteLine (c);
}
您的if
语句不处理具有相同值的数字
更正如下:
if (a >= b && a >= c) {
Console.WriteLine (a);
//break;
} else if (b >= a && b >= c) {
Console.WriteLine (b);
//break;
} else {
Console.WriteLine (c);
//break;
}
我执行了你的代码,如果break被删除,它可以正常工作。没有循环让break语句中断并继续。它给出了一个编译错误。删除它,它会给出正确的结果。
https://i.stack.imgur.com/PgVqB.jpg这是屏幕截图
试试这个
static void Main(string[] args)
{
try
{
Console.WriteLine("Input first number: ");
var a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Input second number: ");
var b = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Input third number: ");
var c = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
if (a > b && a > c)
{
Console.WriteLine(a);
}
else if (b > a && b > c)
{
Console.WriteLine(b);
}
else if (c > a && c > b)
{
Console.WriteLine(c);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadKey();
}
编辑:如果是动态的,那么
static void Main(string[] args)
{
try
{
Console.WriteLine("Enter numbers (Example 25,46,19)");
var input = Console.ReadLine();
if (input == null)
{
Console.WriteLine("Your input is wrong");
return;
}
var numbers = input.Split(',');
//Sort by descending order
for (int i = 0; i < numbers.Length; i++)
{
for (int j = 0; j < numbers.Length - 1; j++)
{
if (Convert.ToInt32(numbers[i]) > Convert.ToInt32(numbers[j]))
{
var temp = Convert.ToInt32(numbers[i]);
numbers[i] = numbers[j];
numbers[j] = temp.ToString();
}
}
}
Console.WriteLine("Greater Number is {0}", numbers[0]);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadKey();
}
在if/else块内添加break;
不会让您的代码编译:)修复此问题后,您的if/else代码块本身看起来是正确的。我测试了一下,它在我的机器上打印出了c。
要回答你关于If语句的问题,如果我是你,我会检查:
- 如果c确实是最大的(它可能大于a,但小于b,那么它失败了)
- c之前或之后不需要的空间(可能转换失败?)你可以在转换前尝试调整字符串)
如果您可以使用。max ():
,这会更简洁。 public static void Main(string[] args)
{
string[] input = Console.ReadLine().Split(' '); // getting input as string
List<int> nums = new List<int>(); // declaring int array with same length as input
for (int i = 0; i < input.Length; i++) // starting a for loop to convert input string to int in nums[]
{
int intValue;
if (Int32.TryParse(input[i].Trim(), out intValue))
{
nums.Add(intValue);
}
}
int max = nums.Max();
int min = nums.Min();
Console.WriteLine("Largest: " + max);
Console.WriteLine("Smallest: " + min);
Console.WriteLine("Press Enter to exit.");
Console.ReadLine();
}
没有。max ():
public static void Main(string[] args)
{
string[] input = Console.ReadLine().Split(' '); // getting input as string
List<int> nums = new List<int>(); // declaring int array with same length as input
for (int i = 0; i < input.Length; i++) // starting a for loop to convert input string to int in nums[]
{
int intValue;
if (Int32.TryParse(input[i].Trim(), out intValue))
{
nums.Add(intValue);
}
}
//int max = nums.Max();
//int min = nums.Min();
int min = nums[0];
int max = nums[0];
for (int i = 0; i < nums.Count(); i++)
{
if (nums[i] < min) min = nums[i];
if (nums[i] > max) max = nums[i];
}
Console.WriteLine("Largest: " + max);
Console.WriteLine("Smallest: " + min);
Console.WriteLine("Press Enter to exit.");
Console.ReadLine();
}
注意:
List<int>
用于容纳用户添加3个以上的值。避免使用a, b, c,以便程序可以接受3个以上的值
使用
Int32.TryParse(your_string, out outputInt)
方便地检测解析错误并继续。使用.Trim()
删除每个文本块前后的空格