我想做一个随机的数学问题,答案不能超过18个数字

时间:2019-06-30 12:08:48

标签: c#

public static void Main(string[] args)
    {
        Random numberGen = new Random();

        int num01 = numberGen.Next(1,1000001);
        int num02 = numberGen.Next(1,1000001);

        Console.WriteLine("What is " + num01 + " times " + num02 + " ?");

        int Answer = Convert.ToInt32(Console.ReadLine());
        if (Answer == num01 * num02)
        {
            Console.WriteLine("Well done your correct!");
        } 
        else
        {
            int responseIndex2 = numberGen.Next(1, 3);
            switch (responseIndex2) {

                case 1:
                    Console.WriteLine("You noob");
                    break;
                case 2:
                    Console.WriteLine("Are you trying uh?!");
                    break;
            }

        }

        Console.ReadKey();
    }

好吧,所以我找不到Convert.ToInt32的任何替代品,当我尝试运行该程序时,当我回答时它崩溃了,因为它的数字太多了,甚至只有20个数字?

1 个答案:

答案 0 :(得分:1)

32位带符号整数(@Getter @EqualsAndHashCode public class Order { public enum OrderType { BUY, SELL } private Id id; private Quantity quantity; private Money price; private OrderType orderType; public Order(Id id, Quantity quantity, Money price, OrderType orderType) { Preconditions.checkNotNull(id, "id can't be null"); Preconditions.checkNotNull(quantity, "quantity can't be null"); Preconditions.checkNotNull(price, "price can't be null"); Preconditions.checkNotNull(orderType, "orderType can't be null"); this.id = id; this.quantity = quantity; this.price = price; this.orderType = orderType; } )可以存储2 ^ 31-1的最大值,该值小于随机数学问题可以拥有的最大答案(最大答案为1000001 * 1000001 ,大约10 ^ 12)。

因此,您不应使用int来存储答案。您可以改用intlong是一个64位的整数,最大值为2 ^ 63-1,大约为9 * 10 ^ 18,远远大于数学问题可以提供的最大答案。

您可以使用相应的long将字符串转换为Convert.ToInt64

long