zoukankan      html  css  js  c++  java
  • c#,回文数判断

     回文数:将数值反过来。如:123 反过来是321 ,如果两个数相等,则是回文,否则不是

    using System;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                //Console.WriteLine("Hello World!");
                int index;
                int swich_value;
                int[] Nums = new int[20];//设定最大可判断数值长度为20
                bool pos;
                int num;
                while (true)
                {
                    Console.WriteLine("输入一个正整数或者0");
                    try//当为空值时,会引发异常,加个捕获异常或者使用out关键字
                    {
                        num = int.Parse(Console.ReadLine());
                        if (num == 0)
                        {
                            //continue;
                            return;
                            //break;
                        }
                        swich_value = num;
                        pos = true;
                        index = 0;
                        while (swich_value > 0)
                        {
                            Nums[index] = swich_value % 10;
                            swich_value = swich_value / 10;
                            index++;
                        }
                        for (int j = 0; j < (index - 1 + 1) / 2; j++)
                        {
                            if (Nums[j] != Nums[index - 1 - j])
                            {
                                pos = false;
                                break;
                            }
                        }
                        if (pos)
                        {
                            Console.WriteLine($"{num}是回文数");
                        }
                        else
                        {
                            Console.WriteLine($"{num}不是回文数");
                        }
                        Console.ReadKey();
                    }
                    catch
                    {
                    }                
                    
                }
            }
        }
    }

    使用out关键字

    if(int.TryParse(Console.ReadLine(),out int num))
    {
    //to do; } //返回一个bool值,当用户输入的字符串可以转换为Int32时,返回true,反之为false.
  • 相关阅读:
    # ES6基础
    # yarn简单使用
    # laravel框架中的配置
    需求概述开发进度09
    需求概述开发进度08
    需求概述开发进度07
    需求概述开发进度06
    需求概述开发进度05
    需求概述开发进度04
    需求概述开发进度03
  • 原文地址:https://www.cnblogs.com/singhwong/p/11918447.html
Copyright © 2011-2022 走看看