英文:
It doesn't throw an error on invalid input
问题
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace KKRIT
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Здравствуйте! Выберите страницу (от 1 до 354):");
            string page = Console.ReadLine();
            int result = Int32.Parse(page);
            while (page < 1 || page > 354)
            {
                Console.WriteLine("Страница введена успешно");
                else
                {
                    Console.WriteLine("Такой страницы не существует");
                }
            }
            Console.WriteLine("Переходим на страницу {0}...", page);
            Console.ReadLine();
        }
    }
}
英文:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace KKRIT
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Здравствуйте! Выберите страницу (от 1 до 354):");
            string page = Console.ReadLine();
            int result = Int32.Parse(page);
            while (page <= 1 || page >= 354)
            {
                Console.WriteLine("Страница введена успешно");
                else
                {
                    Console.WriteLine("Такой страницы не существует");
                }
            }
            Console.WriteLine("Переходим на страницу {0}...", page);
            Console.ReadLine();
        }
    }
}
If the input is incorrect, the else text is not displayed
I have already tried all the options from the Internet, repeated the format, but nothing comes out
答案1
得分: 0
你的if语句不正确。应该检查页面是否大于或等于1 并且小于或等于354(然后页面存在)。
static void Main(string[] args)
{
    Console.WriteLine("Здравствуйте! Выберите страницу (от 1 до 354):");
    string page = Console.ReadLine();
    int result = Int32.Parse(page);
    if (result >= 1 && result <= 354)
    {
        Console.WriteLine("Страница введена успешно");
    }
    else
    {
        // 无效的页面
        Console.WriteLine("Такой страницы не существует");
    }
    Console.WriteLine("Переходим на страницу {0}...", page);
    Console.ReadLine();
}
你可以交换if语句,检查页面是否小于1 或大于354,然后打印无效的页面。
if (result < 1 || result > 354)
{
    // 无效的页面
    Console.WriteLine("Такой страницы не существует");
}
else
{
    Console.WriteLine("Страница введена успешно");
}
英文:
Your if statements is not right. It should check if page is above or equal to 1 AND below or equal to 354 (Then page is existing).
static void Main(string[] args)
{
    Console.WriteLine("Здравствуйте! Выберите страницу (от 1 до 354):");
    string page = Console.ReadLine();
    int result = Int32.Parse(page);
    if (result >= 1 && result <= 354)
    {
        Console.WriteLine("Страница введена успешно");
    }
    else
    {
        // Invalid page
        Console.WriteLine("Такой страницы не существует");
    }
    Console.WriteLine("Переходим на страницу {0}...", page);
    Console.ReadLine();
}
You could switch the if statement and check if page is below 1 OR above 354 and then print Invalid page
if (result < 1 || result > 354)
{
    // Invalid page
    Console.WriteLine("Такой страницы не существует");
}
else
{
    Console.WriteLine("Страница введена успешно");
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论