Práctica Básica de Codificación
Un número se almacena en una variable llamada num en forma de string. Sumar 2 al número almacenado. Puede utilizar los siguientes pasos:
- Convertir el valor de la cadena
numa un entero y almacenarlo en una nueva variable entera llamadatemp. - Sumar
2al valor detemp. - Convertir el valor de
tempa unastringy asignarlo a la variablenum
main.cs
123456789101112131415161718using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { string num = "15"; // Write code below this line // Write code above this line Console.WriteLine(num); // Expected Output: 17 } } }
Utilizar Convert.ToInt32() y Convert.ToString().
main.cs
1234567891011121314151617181920using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { string num = "15"; // Write code below this line int temp = Convert.ToInt32(num); temp = temp + 2; num = Convert.ToString(temp); // Write code above this line Console.WriteLine(num); // Expected Output: 17 } } }
¡Gracias por tus comentarios!
Pregunte a AI
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla
Can you show me an example of how to do this in C#?
What happens if the string in `num` is not a valid number?
Can you explain why we need to use `Convert.ToInt32()` and `Convert.ToString()`?
Awesome!
Completion rate improved to 1.59
Práctica Básica de Codificación
Desliza para mostrar el menú
Un número se almacena en una variable llamada num en forma de string. Sumar 2 al número almacenado. Puede utilizar los siguientes pasos:
- Convertir el valor de la cadena
numa un entero y almacenarlo en una nueva variable entera llamadatemp. - Sumar
2al valor detemp. - Convertir el valor de
tempa unastringy asignarlo a la variablenum
main.cs
123456789101112131415161718using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { string num = "15"; // Write code below this line // Write code above this line Console.WriteLine(num); // Expected Output: 17 } } }
Utilizar Convert.ToInt32() y Convert.ToString().
main.cs
1234567891011121314151617181920using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { string num = "15"; // Write code below this line int temp = Convert.ToInt32(num); temp = temp + 2; num = Convert.ToString(temp); // Write code above this line Console.WriteLine(num); // Expected Output: 17 } } }
¡Gracias por tus comentarios!