C# buffer3将单词存储在索引i处。所以想象一下你的话是“这”是“四”长。你最长的单词是“This”(4个字符长),“four”和“long”的长度相同。第二次循环时,在索引0处存储“This”,在索引2处存储“four”,在索引3处存储“long”。索引
C# buffer3将单词存储在索引i处。所以想象一下你的话是“这”是“四”长。你最长的单词是“This”(4个字符长),“four”和“long”的长度相同。第二次循环时,在索引0处存储“This”,在索引2处存储“four”,在索引3处存储“long”。索引,c#,string,C#,String,buffer3将单词存储在索引i处。所以想象一下你的话是“这”是“四”长。你最长的单词是“This”(4个字符长),“four”和“long”的长度相同。第二次循环时,在索引0处存储“This”,在索引2处存储“four”,在索引3处存储“long”。索引1中没有存储任何内容,因为“is”与最长单词的长度不同。您需要两个索引变量。在循环外部添加int j=0。并在if语句中添加j++,即“对于列表中的单词,其中words.length==largestword.length选择单词还有,我试过你
buffer3将单词存储在索引i处。所以想象一下你的话是“这”是“四”长。你最长的单词是“This”(4个字符长),“four”和“long”的长度相同。第二次循环时,在索引0处存储“This”,在索引2处存储“four”,在索引3处存储“long”。索引1中没有存储任何内容,因为“is”与最长单词的长度不同。您需要两个索引变量。在循环外部添加int j=0。并在if语句中添加j++,即“对于列表中的单词,其中words.length==largestword.length选择单词还有,我试过你说的,但我得到了这个输出最大的单词很长。最大的单词很长。最大的单词很长。最大的单词很长。最大的单词很长。最大的单词很长。最大的单词很长。最大的单词很长。最大的单词很长。最大的单词很长。最大的单词很长。最大的单词很长单词很长。最大的单词很长。最大的单词很长。最大的单词很长。最大的单词很长。最大的单词很长。最大的单词很长。最大的单词很长。下面我提供的代码有什么问题?我测试过了,效果很好欢迎来到stackoverflow!最好为示例代码提供一个简短的描述,以提高post的准确性:)
using System;
using System.Linq;
class largest1{
public void largest(){
Console.WriteLine("Enter the String:");
string buffer1 = Console.ReadLine();
string[] buffer = buffer1.Split(' ');
int length;
string largestword = buffer[0];
for(int i = 0; i < buffer.Length; i++){
string temp = buffer[i];
length = temp.Length;
if( largestword.Length < buffer[i].Length ) {
largestword = buffer[i];
}
}
var largestwords = from words in buffer
let x = largestword.Length
where words.Length == x
select words;
Console.Write("Largest words are:");
foreach(string s in largestwords){
Console.Write(s);
}
}
static void Main(){
largest1 obj = new largest1();
obj.largest();
}
}
//Put this in your class so that it is persisted
string largestword = "";
//Put this right before your for loop
largestword = buffer[0];
//Put this inside your for loop
if( largestword.Length < buffer[i].Length ) {
largestword = buffer[i];
}
class LargestWordsClass
{
public LargestWordsClass()
{
_LargestWords = new List<String>();
}
//This says that the variable can be set from the class but read by anyone
public int LargestWordSize
{
get;
private set;
}
//This lets users get the list without being able to modify it
private List<String> _LargestWords;
public String[] LargestWords
{
get
{
return _LargestWords.ToArray();
}
}
public void FindLargestWord()
{
_LargestWords.Clear();
Console.WriteLine("Enter the String: ");
String buffer = Console.ReadLine();
String[] splitBuffer = buffer.Split(' ');
LargestWordSize = 0;
for (int i = 0; i < splitBuffer.Length; i++)
{
if (LargestWordSize < splitBuffer[i].Length)
{
LargestWordSize = splitBuffer[i].Length;
_LargestWords.Clear();
_LargestWords.Add(splitBuffer[i]);
}
else if (LargestWordSize == splitBuffer[i].Length)
{
_LargestWords.Add(splitBuffer[i]);
}
Console.WriteLine("The word is " + splitBuffer[i] + " and the length is " + splitBuffer[i].Length.ToString());
}
Console.WriteLine("The largest word" + ((_LargestWords.Count > 1) ? "s are" : " is:"));
for (int i = 0; i < _LargestWords.Count; i++)
{
Console.WriteLine(_LargestWords[i]);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class MainClass {
public static void Main() {
string[] words = { "cherry", "apple", "blueberry" };
int longestLength = words.Max(w => w.Length);
Console.WriteLine("The longest word is {0} characters long.", longestLength);
}
}
public void largest(){
Console.WriteLine("Enter the String:");
string buffer1 = Console.ReadLine();
string[] buffer = buffer1.Split(' ');
int length;
/*foreach (string word in buffer)
{
//Console.WriteLine(word);
}*/
string temp = string.Empty;
for(int i = 0; i < buffer.Length; i++){
if(temp.length = buffer[i].length)
temp = buffer[i];
}
Console.WriteLine("The word is " + temp + " and the length is " + temp.length.tostring());
}
var ordered = test
.GroupBy(str => str.Length)
.OrderByDescending(grp => grp.Key)
.First()
.ToList();
string[] words = { "berryblue", "cherry", "apple", "blueberry" };
int maxLength = words.Max(s => s.Length);
var longestWords = from word in words where word.Length == maxLength select word;
foreach (var word in longestWords) System.Console.WriteLine(word);
static void Main(string[] args)
{
Console.WriteLine("Enter the string:");
string[] words = Console.ReadLine().Split(' ');
var longestWords = words
.Where(w => w.Length == words.Max(m => m.Length));
Console.WriteLine("Longest words are: {0}",
string.Join(", ", longestWords.ToArray()));
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Testingstring
{
class Program
{
static void Main(string[] args)
{
string s,s2;
s2 = " ";
char[] a = new char[]{' '};
Console.WriteLine("Enter any string");
s = Console.ReadLine();
foreach (string s1 in s.Split(a))
{
if (s2.Length < s1.Length)
{
s2 = s1;
}
}
Console.WriteLine("The largest string is " + s2 +" and its length is " +s2.Length);
Console.ReadKey();
}
}
}
class Program
{
static void Main(string[] args)
{
var longestWord = LongestWord("Hello Stack Overflow Welcome to Challenge World");
PrintTheLongestWord(longestWord.Key);
}
public static KeyValuePair<string, int> LongestWord(String statement)
{
Dictionary<string, int> wordsLengths = InitializeDictionary(statement.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries));
return GetMax(wordsLengths);
}
private static Dictionary<string, int> InitializeDictionary(string[] wordsList)
{
Dictionary<string, int> wordsLengths = new Dictionary<string, int>();
foreach (var word in wordsList)
{
wordsLengths[word] = word.Length;
}
return wordsLengths;
}
private static KeyValuePair<string, int> GetMax(Dictionary<string, int> dictionary)
{
KeyValuePair<string, int> max = new KeyValuePair<string, int>(" ", Int32.MinValue);
foreach (var item in dictionary)
{
if (item.Value.CompareTo(max.Value) > 0)
max = item;
}
return max;
}
public static void PrintTheLongestWord(string word)
{
Console.WriteLine($"the Longest word is {word} with length {word.Length}");
}
}
public static string LongestWord2(String statement)
{
string max = "";
foreach (string word in statement.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries))
{
if (word.Length>max.Length)
{
max = word;
}
}
return max;
}