小编omn*_*mni的帖子

如何在try catch语句中重新请求输入

string l = Console.ReadLine();

try
{
    int.Parse(l);
}
catch (FormatException)
{
    Console.WriteLine("Invalid input. Please enter 1, 2, or 3.");
}
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我已经要求输入,但如果用户输入一个非整数的答案,如字母"f",catch语句会捕获它,但之后再次抛出异常,因为变量"l"仍然等于"f".救命?

c#

2
推荐指数
1
解决办法
4654
查看次数

解析GMT DateTIme

我有03-10-14 18:44:58 时间部分是GMT,其中03 =第10天=月= 14 =年,18 =小时44 =分58 =秒

我怎么解析这个?这是我正在使用但它无法正常工作:

var date = "03-10-14 18:44:58";
_Packet.Time = DateTime.ParseExact(datetime, "dd-MM-yy HH:mm:ss 'GMT'", CultureInfo.CurrentCulture, DateTimeStyles.AllowWhiteSpaces);
Run Code Online (Sandbox Code Playgroud)

c# datetime

1
推荐指数
1
解决办法
396
查看次数

字符串数组c ++中的最小元素

我试图找到字符串数组中的最小元素,但我无法弄清楚如何做到这一点.我提出了这个代码的想法,它适用于整数,但不适用于字符串.这将编译,虽然它只检查字符串中的第一个字符的ASCII值.换句话说,在字符串数组中:lists[5] = { "aaa", "z", "cccc", "tt", "jjj"}; lists [1]"z"是字符串的最小元素.但是,因为'a'是较低的ASCII值,代码将打印出来Smallest aaa而不是Smallest z.现在我知道我可以使用.length对字符串中的每个字符做一些深刻的同情,但是我想使用一些简单的东西可以解决这个问题,因为我想将它添加到一个将被重载为整数的函数中我可以在字符串和整数比较之间来回切换.但如果这是不可能的,我将只有两个独立的功能来处理每个.

如果您对如何在字符串数组中找到最小元素有任何建议,该怎么办?

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main() {


string  lists[5] = { "aaa", "z", "cccc", "tt", "jjj"};
    string smallests;
    smallests = lists[0];
    for (int i = 0; i < 5; i++){
        cout << smallests << endl;
        if (lists[i] < smallests){   // Flip < to > to find largest
            smallests = lists[i];
        }
    } …
Run Code Online (Sandbox Code Playgroud)

c++ arrays string algorithm

-1
推荐指数
1
解决办法
2045
查看次数

标签 统计

c# ×2

algorithm ×1

arrays ×1

c++ ×1

datetime ×1

string ×1