c#中需要唯一输入的问题

本文关键字:输入 问题 唯一 | 更新日期: 2023-09-27 18:13:48

我需要从用户请求10个数字作为单独的输入。每个数字必须在10到100之间(含100)。

将每个数字与最后输入的数字进行比较,以确定它是否重复。如果是,请用户输入一个不同的号码。

一旦确定所有条件都满足,将每个数字输出到屏幕。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication9
{ 
    class Program
    {
        static void Main(string[] args)
        {
          String userNumber;
          int userNumberInt;
          // declare a variable to save the previous
          for (int i = 0; i < 10; i++)
          {
              Console.WriteLine("Enter your Number:");
              userNumber = Console.ReadLine();
              userNumberInt = Convert.ToInt32(userNumber);
              if ((userNumberInt <= 100) && (userNumberInt >= 100))
              {
                  Console.WriteLine("Your Valid number is" + userNumber);
                  // set previous to the usernumber
              }
              // else if(previous == userNumber)
              // another invalid situatuion just like below
              else
              {
                  Console.WriteLine("Invalid Number"};
                  i--; // giving another chance
              }
          }
          Console.ReadLine();
        }
    }
}

c#中需要唯一输入的问题

就这样做,注释状态…

// declare a variable to save the previous
应:

int previous = 0;

:

// set previous to the usernumber
应:

previous = userNumberInt;

调整和修复你的条件:

if ((userNumberInt <= 100) && (userNumberInt >= 100))
应:

if ((userNumberInt <= 100) && (userNumberInt >= 10) && (userNumberInt != previous))

输出数字作为练习…div;)