帮助确定用户是否在控制台应用程序中输入了重复的数字 [英] Help with determining if user inputs a duplicate number into the console application

查看:64
本文介绍了帮助确定用户是否在控制台应用程序中输入了重复的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还没有学过数组,也没有学过堆栈等等。我应该编写一个循环来检查用户是否输入两次相同的数字,如果他们这样做,控制台会要求他们输入一个新的数字。



更具体地说,如果用户输入11,12,11,12,这可以被控制台接受,因为12不是直接前一个数字的副本,但是11,11不起作用,因为它是直接复制品。



我似乎无法找到有关如何执行此操作的任何信息,也没有解释在班上。有人能指出我正确的方向吗?我至少在正确的道路上吗?



if(userNumber == previousNumber)一节给我的错误是使用未分配的局部变量'previousNumber' ,不知道怎么过去。



谢谢











 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;

命名空间 First_Five_Ages
{
class Program
{
静态 void Main( string [] args)
{

string previousNumber;
string userNumber;
int userNumberInt;
for int number = 0 ; number < = 10 ; ++ number)
{
Console.WriteLine( 输入您的号码);
userNumber = Console.ReadLine();
userNumberInt = Convert.ToInt32(userNumber);

if (userNumberInt < 10 || userNumberInt > 100
{
Console.WriteLine( 无效数字,输入另一个);
number--;
}
if
(userNumber == previousNumber)
{
Console.WriteLine( 重复数字,输入一个新的);
number--;
}




其他
{
控制台.WriteLine(userNumber);
previousNumber = userNumber;
}
}
}
}

}

解决方案

如果你还没有学过阵列等等,你应该在提出你的问题之前做这件事,否则没有意义。



它是很明显,如果你没有以数组或某个集合的形式存储输入对象的集合,那么你没有任何信息可以让你推断是否有其他对象是重复的。重复的是什么?



所以,你仍然需要一个数组或一些集合。数组中的搜索应该是显而易见的,但是输入数据而不重复的最有效方法是类 System.Collections.Generic.HashSet< int>

http://msdn.microsoft.com/en-us/library/bb359438.aspx [ ^ ]。



此类支持 set 的概念,因此。此外,如果您需要保留输入数据的顺序,您还可以使用类 System.Collections.Generic.List< int>

http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx [ ^ ]。



没有收藏品,没有地方可以去。至于数组类型,它们不适合这个问题,只是因为它们的长度是在初始化时定义的并且无法修改( System.Array.Resize 实际上并不是调整数组大小,它会创建一个全新的数组对象并复制所有数据,这是非常低效的。)



-SA

I have not learned about arrays yet, nor stacks, etc. I am supposed to write a loop that checks whether or not the user enters the same number twice, and if they do, the console asks them to enter a new number.

More specifically, if the user enters 11, 12, 11, 12, this is acceptable by the console because 12 is not a duplicate of the directly previous number, but 11, 11, would not work because it is a direct duplicate.

I can't seem to find any information about how to do this, nor has it been explained in class. Could someone point me in the right direction? Am I at least on the right path?

The section if(userNumber == previousNumber) keeps giving me the error of "Use of unassigned local variable 'previousNumber'", not sure how to get past this.

Thank you





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

namespace First_Five_Ages
{
    class Program
    {
        static void Main(string[] args)
        {

            string previousNumber;
            string userNumber;
            int userNumberInt;
            for (int number = 0; number <= 10; ++number)
            {
                Console.WriteLine("Enter your number");
                userNumber = Console.ReadLine();
                userNumberInt = Convert.ToInt32(userNumber);

                if (userNumberInt < 10 || userNumberInt > 100)
                {
                    Console.WriteLine("Invalid Number, enter another");
                    number--;
                }
                if
                    (userNumber == previousNumber)
                {
                    Console.WriteLine("Duplicate Number, enter a new one");
                    number--;
                }




                else
                {
                    Console.WriteLine(userNumber);
                    previousNumber = userNumber;
                }
            }
        }
    }

} 

解决方案

If you "have not learned about arrays yet, etc.", you should do it before posing your question, which otherwise makes no sense.

It is apparent that if you don't store the set of entered objects in the form of an array or some collection, you have no information which would let you to infer is some other object is duplicate or not. Duplicate of what?

So, you still need an array or some collection. The search in an array should be apparent, but the most efficient way of entering data without duplicates would be the class System.Collections.Generic.HashSet<int>:
http://msdn.microsoft.com/en-us/library/bb359438.aspx[^].

This class support the concept of the set and hence. Should you need, additionally, to preserve the order in which the data was entered, you can also use the class System.Collections.Generic.List<int>:
http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx[^].

There is nowhere to go without the collections. As to the array types, they are unsuitable for this problem, just because their length is defined at the moment of initialization and cannot be modified (System.Array.Resize does not actually resize the array, it creates a brand new array objects and copies all the data, which is very inefficient).

—SA


这篇关于帮助确定用户是否在控制台应用程序中输入了重复的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆