简体   繁体   English

C#:如何使用数组,字符和字符串? 需要清理

[英]C#: How to use arrays, chars and strings? Clear up needed

I am new to programming and I have a project in my Algorithm class. 我是编程新手,我的Algorithm类中有一个项目。 What we have to do is decide on a problem and solve it. 我们要做的是确定问题并加以解决。 We haven't learnt much more than string, char and WriteLine. 我们对字符串,char和WriteLine的了解不多。 We did add a couple of things as you will see soon! 我们确实添加了一些内容,您很快就会看到!

I decided that what I want to solve this: The user inserts a word, no matter how long and the program will automatically make the first letter a capital letter. 我决定要解决的问题:无论用户插入多长时间,该程序都会自动将第一个字母变成大写字母。 So far this is what I have: 到目前为止,这就是我所拥有的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {
    start:
        Console.WriteLine("Please enter a word below:");
        Console.WriteLine("");
        string str = Console.ReadLine();
        char char1;
        if (str[0] >= 97)
        {
            char1 = (char)(str[0] - 32);
        }
        else
        {
            char1 = (char)(str[0] + 32);
        }

        char char2 = (char)(str[1]);
        char char3 = (char)(str[2]);
        char char4 = (char)(str[3]);
        char char5 = (char)(str[4]);
        Console.WriteLine("");
        Console.Write(char1);
        Console.Write(char2);
        Console.Write(char3);
        Console.Write(char4);
        Console.WriteLine(char5);
        goto start;
}
}
}

The problem with that code is that any word with less than 5 letters will make the program crash. 该代码的问题在于,少于5个字母的任何单词都会使程序崩溃。 Anything with more than 5 letters will just be cut at the fifth letter... I was told that using arrays should solve this problem. 超过5个字母的任何内容都将在第5个字母处被剪掉...有人告诉我,使用数组可以解决此问题。 Seeing as I am a total newbie at this, I would need this to be broken down and be as simply told as possible! 鉴于我是一个新手,所以我需要将其分解并尽可能简单地告知!

Any help getting this to work would be very appreciated. 任何帮助使它起作用的方法,将不胜感激。

Thanks :) 谢谢 :)

Console.WriteLine("Enter a word:");
string str = Console.ReadLine();
Console.WriteLine(str[0].ToString().ToUpper() + str.Substring(1));

This will work. 这将起作用。

Or... if you need to go through the entire string and find the first actual alphabetical character, you can do the following: 或者...如果您需要遍历整个字符串并找到第一个实际的字母字符,则可以执行以下操作:

Console.WriteLine("Please enter a word:");
string s = Console.ReadLine();
bool found = false;
char[] chars = new char[s.Length];
for (int i = 0; i < s.Length; i++)
{
    if (Char.IsLetter(s[i]) && !found)
    {
         chars[i] = s[i].ToString().ToUpper()[0];
         found = true;
    }
    else
    {
        chars[i] = s[i];
    }
}
s = new String(chars);
Console.WriteLine(s);

Use a for loop like this after writing char1 to the Console: 将char1写入控制台后,使用如下所示的for循环:

if (str.Length > 1)
{
    for (int i = 1; i < str.Length; i++)// Start at 1 to skip char1
    {
        Console.Write(str[i]);
    }
}

There are some methods you can call on string that will be helpful: 您可以对string调用一些方法,这些方法会有所帮助:

  • Substring
  • ToUpper

In fact, you don't need to worry about characters; 实际上,您不必担心字符。 this problem can be solved using only strings. 仅使用字符串即可解决此问题。

Also take care to check that your code handles the case where the string is empty (using an if statement), which will happen if the user just presses Enter without typing anything. 还应注意检查代码是否处理了字符串为空的情况(使用if语句),如果用户仅按Enter键而不输入任何内容,则将发生这种情况。

You're taking an algorithms class and they make you choose a problem to solve? 您正在学习算法课程,它们使您选择要解决的问题? Sounds dangerous for someone learning. 听起来对学习的人来说很危险。

Console.WriteLine("Please enter a word below:");
Console.WriteLine("");
string inputString = Console.ReadLine();    // try to use meaningful variable names

// shorthand for the if ... else block:
// type variableName = (true/false condition) ? "is true" : "is false";
char firstChar = inputString[0] >= 97 ? (char)(inputString[0] - 32) : (char)(inputString[0] + 32);

Console.WriteLine("");
Console.Write(firstChar);

for (int i = 1; i < inputString.Length; i++)    // skip firstChar
{
    Console.Write(inputString[i]);
}

As others have mentioned, you need to use a loop for this if you want anything resembling a general solution. 正如其他人所提到的,如果您想要类似于一般解决方案的内容,则需要为此使用循环。

Also, you'll want to avoid using goto statements. 另外,您将要避免使用goto语句。 There are many reasons, one being that they (in my opinion) make code difficult to read and maintain. 原因很多,其中一个是(我认为)它们使代码难以阅读和维护。

Additionally, if your code had worked as written, it would never end. 另外,如果您的代码按编写的方式工作,它将永远不会结束。 Your program, as written would execute, then begin again, never stopping. 您编写的程序将执行,然后再次开始,永不停止。 If you want this sort of behavior, then you should wrap your code in an infinite loop which exits upon some condition. 如果您想要这种行为,则应将代码包装在无限循环中,该循环在某些情况下会退出。 This might look something like: 这可能看起来像:

bool keepRunning = true;
while(keepRunning){
    //code here
    Console.Write("go again? (y/n) ");
    keepRunning = (string)(Console.ReadLine()).equals("y") ? false : true;  
}

On that last statement, I forget if you need to cast the output of ReadLine to a string before calling the .equals method... I don't have my IDE up. 关于最后一条语句,我忘记了是否需要在调用.equals方法之前将ReadLine的输出强制转换为字符串...我没有启动我的IDE。 i think you get the idea. 我想你应该已经明白了。

edit: I saw another response that was just posed about using the .ToUpper method. 编辑:我看到了另一个关于使用.ToUpper方法的.ToUpper I thought of this but assumed maybe you needed to use the char type. 我想到了,但假设您可能需要使用char类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM