C
C#ā€¢14mo ago
martito

āœ… Help with the code, please. I want my program to work properly.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Carriages10ModelCars
{
class Program
{
static void Main(string[] args)
{
string[] modelcar = new string [10];
int[] yearcar = new int [10];

for (int i=0; i<10; i++)

{
Console.Write ("Enter a model of your car {0}", i+1);
modelcar[i] = int.Parse(Console.ReadLine());

Console.Write("Enter a year of production {0}, i+1");
if (int.TryParse(Console.ReadLine(), out yearcar [i]))
{
if (yearcar[i] >= 2020);
Console.WriteLine("The car is new.");
}

else;

{

Console.WriteLine("The car isn't new.");

}

Console.WriteLine("Invalid year. Please, enter again.");
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Carriages10ModelCars
{
class Program
{
static void Main(string[] args)
{
string[] modelcar = new string [10];
int[] yearcar = new int [10];

for (int i=0; i<10; i++)

{
Console.Write ("Enter a model of your car {0}", i+1);
modelcar[i] = int.Parse(Console.ReadLine());

Console.Write("Enter a year of production {0}, i+1");
if (int.TryParse(Console.ReadLine(), out yearcar [i]))
{
if (yearcar[i] >= 2020);
Console.WriteLine("The car is new.");
}

else;

{

Console.WriteLine("The car isn't new.");

}

Console.WriteLine("Invalid year. Please, enter again.");
}
}
}
}
9 Replies
martito
martitoOPā€¢14mo ago
For now, it just adds on every line (aka "car isn't new, car is new etc etc) onto every text you enter. How can I make it work properly?
SG97
SG97ā€¢14mo ago
modelcar is an array of strings, you cannot put an int to it assuming this is a school assignment, you haven't learned about classes yet? else also has unneeded ; and the if aswell the int array yearcar seems redundant for parsing Console.ReadLine() I suggest you look into int.TryParse the last Console.WriteLine makes no sense
martito
martitoOPā€¢14mo ago
yeah, we haven't classes yet
Steve John
Steve Johnā€¢14mo ago
for (int i = 0; i < 10; i++) { Console.Write("Enter a model of your car {0}:", i + 1); modelcar[i] = Console.ReadLine(); retry: Console.Write("Enter a year of production {0}:", i + 1); if (int.TryParse(Console.ReadLine(), out yearcar[i])) { if (yearcar[i] >= 2020) { Console.WriteLine(" The car is new."); } else { Console.WriteLine(" The car isn't new."); } } else { Console.WriteLine(" Invalid year. Please, enter again."); goto retry; } } your program is modified so that it works properlly
SG97
SG97ā€¢14mo ago
spoonfeed something we do here
Steve John
Steve Johnā€¢14mo ago
what?
SG97
SG97ā€¢14mo ago
the idea is to let people learn, and not give them the answer aka spoonfeed
Steve John
Steve Johnā€¢14mo ago
got it
martito
martitoOPā€¢14mo ago
Thank you very much šŸ‘šŸ‘šŸ‘
Want results from more Discord servers?
Add your server