Correction de code C#
생성일: 2024년 12월 31일
생성일: 2024년 12월 31일
patch this code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjetInformatique
{
public class Mofus
{
public static string[][] add(string[][] tableau, string input = "")
{
textConsole.WriteLine("Quel mot voulez-vous ajouter ?"); input = Console.ReadLine(); int n = input.Length; for (int i = 0; i < tableau.Length; i++) { if (tableau[i] == null) { tableau[i] = new string[] { "v" }; } else { if (i == n) { string[] temp = new string[tableau[i].Length + 1]; for (int j = 0; j < tableau[i].Length; j++) { temp[j] = tableau[i][j]; } temp[temp.Length - 1] = input; tableau[i] = temp; break; } } else { if (i == n)&& (tableau[i] == null) { } } } return tableau; } }
}
Here’s a patched version that compiles and makes logical sense. The main issues were:
}) in the nested else blocks.if (i == n)&& (tableau[i] == null))—missing parentheses around combined conditions.tableau[i] when null versus when it already has data.Below is one way to rewrite the method so it compiles and follows a clearer logic:
csharpusing System; namespace ProjetInformatique { public class Mofus { public static string[][] add(string[][] tableau, string input = "") { Console.WriteLine("Quel mot voulez-vous ajouter ?"); input = Console.ReadLine(); int n = input.Length; // Parcours du tableau for (int i = 0; i < tableau.Length; i++) { // Si la ligne est vide (null), on l’initialise if (tableau[i] == null) { // Si on se trouve à l’indice correspondant à la longueur du mot // on met directement le nouveau mot if (i == n) { tableau[i] = new string[] { input }; break; } else { // Sinon, on fait quelque chose par défaut (ex. "v") // Ce bloc dépend de votre logique métier tableau[i] = new string[] { "v" }; } } else { // Si on se trouve à l’indice où ajouter le mot // on agrandit le tableau existant à cet emplacement if (i == n) { string[] temp = new string[tableau[i].Length + 1]; for (int j = 0; j < tableau[i].Length; j++) { temp[j] = tableau[i][j]; } temp[temp.Length - 1] = input; tableau[i] = temp; break; } } } return tableau; } } }
input = Console.ReadLine()).n = input.Length pour décider à quel index (i.e., i == n) on insère le mot.tableau[i] est null et qu’on est à l’indice i == n, on crée un nouveau tableau avec le seul élément input.tableau[i] n’est pas null et qu’on est à i == n, on agrandit la liste de chaînes existante pour y insérer input.Vous pouvez bien sûr ajuster la logique (par exemple ce que vous voulez faire dans le bloc else) en fonction de votre besoin métier exact.