[英]Keeps crashing on 3rd if condition
So I am kind of new to programming (16 y/o) and I have been solving problems for a while now, but now I am solving this one prob (which is quite easy) there seems to be just one thing getting on my way. 所以我是编程的新手(16岁),现在我已经解决了一段时间,但是现在我正在解决这个问题(这很容易),似乎只有一件事正在发生。 The problem is pretty simple, it only needs me to read 3 integers and print out the greatest one, I have already solved it in C++/C and had no problem, at all.
问题很简单,只需要我读取3个整数并打印出最大的整数,我已经在C ++ / C中解决了这个问题,根本没有问题。 But this one, It prints out and works perfectly when the first two if conditions are true (if a is the greatest value, it prints a, if b is the greatest value it prints b) but when c is the greatest value it comes out empty.
但是,当条件为真时,它可以打印并在前两个条件下完美工作(如果a是最大值,则打印a,如果b是最大值,则打印b),但是当c是最大值时,它可以打印出来空。 Sorry for the long post, hope someone can help me out.
抱歉,很长的帖子,希望有人能帮帮我。
using System;namespace MaxP351 { class MainClass { public static void Main (string[] args) { string[] input = Console.ReadLine ().Split (' '); // getting input as string int[] nums = new int[input.Length]; // 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[] nums[i] = Convert.ToInt32(input[i]); } int a, b, c; a = nums [0]; b = nums [1]; c = nums [2]; if (a > b && a > c) { Console.WriteLine (a); break; } else if (b > a && b > c) { Console.WriteLine (b); break; } else if (c > a && c > b) { Console.WriteLine (c); break; }
} }
If c
is truly the largest number then your code should be printing out the value for c
. 如果
c
确实是最大的数字,则您的代码应打印出c
的值。 If this were my code, I would set a break point at your if
statement and see what the values are. 如果这是我的代码,我将在您的
if
语句处设置一个断点,然后查看值是什么。 Alternatively you can print out the values of a,b and c right before your if statements and make sure they are the values that you expect. 另外,您可以在if语句之前打印出a,b和c的值,并确保它们是您期望的值。 Example:
例:
Console.WriteLine("Values before if statement:")
Console.WriteLine (a);
Console.WriteLine (b);
Console.WriteLine (c);
if (a > b && a > c) {
...
Again - I would recommend going the breakpoint route, but the above might be a quick and dirty way to see the values if you aren't familiar with visual studio debugging. 再说一遍-我建议您使用断点路线,但是如果您不熟悉Visual Studio调试,则以上内容可能是一种快速而肮脏的查看值的方法。
In summary - your code should give you the desired output. 总而言之-您的代码应为您提供所需的输出。 The problem most likely lies somewhere else.
问题很可能在其他地方。
1. No need break; 1.无需休息; for if-else statements.
用于if-else语句。
2. No need if statements at all to display the largest integer. 2.根本不需要if语句显示最大整数。 You can do below.
您可以在下面做。
Console.WriteLine(nums.Max())
3. If you don't want to use Max function. 3.如果您不想使用Max功能。 Do a proper loop which is better than too many if conditions;
做一个适当的循环,要比太多情况要好;
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);
You dont need a 'break' as you are using if-else if. 使用if-else if时,您不需要“中断”。 Below should help
下面应该有帮助
if (a > b && a > c) {
Console.WriteLine (a);
} else if (b > a && b > c) {
Console.WriteLine (b);
} else {
Console.WriteLine (c);
}
Your if
statements do not handle the numbers with same value 您的
if
陈述式无法处理相同值的数字
Correct as below: 更正如下:
if (a >= b && a >= c) {
Console.WriteLine (a);
//break;
} else if (b >= a && b >= c) {
Console.WriteLine (b);
//break;
} else {
Console.WriteLine (c);
//break;
}
Try this 尝试这个
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();
}
Edit: If you make it dynamic then 编辑:如果您使它动态
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();
}
I executed your code it works properly if break is removed. 我执行了您的代码,如果删除了中断,它将正常工作。 there is no loop for the break statement to break and continue.it gives a compilation error.
break语句没有中断和继续的循环。它给出了编译错误。 Remove that and it gives the correct result.
删除它,它会给出正确的结果。
http://i.imgur.com/O02akZx.jpg here is the screen shot http://i.imgur.com/O02akZx.jpg这是屏幕截图
Adding break;
增加
break;
inside if/else blocks won't let your code compile :) After fixing this, your if/else code block in itself, looks correct. 在if / else块内部不会让您的代码编译:)修复此问题后,您的if / else代码块本身看起来是正确的。 I tested it and it prints out c in my machine.
我测试了它,并在我的机器上打印出c。
To answer your question related to If statements , if I were you, I would check: 要回答与If语句有关的问题,如果我是你,我将检查:
This is neater if you can use .Max(): 如果可以使用.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();
}
This is without .Max(): 这没有.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();
}
Note: 注意:
List<int>
was used to accommodate user adding more than 3 values. List<int>
用于容纳用户添加三个以上的值。
Avoided using a, b, c so that the program can accept more than 3 values. 避免使用a,b,c,以便程序可以接受3个以上的值。
Used Int32.TryParse(your_string, out outputInt)
to conveniently detect parse errors and move on. 使用
Int32.TryParse(your_string, out outputInt)
方便地检测解析错误并继续。 Use .Trim()
to remove spaces before and after each text block. 使用
.Trim()
删除每个文本块之前和之后的空格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.