Guess.md 4.5 KB
Newer Older
F
feilong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
# 猜数字

使用C#编写一个猜数字游戏,以下实现没有错误的代码是?

## 答案

```csharp
public class Program{
    public static void Main(string[] args){
        Random rand = new Random();
        int success = 0;
        while(true){
            int number = rand.Next(0,5);
            bool replay = false;
            Console.Write("我想好了一个0-5内的数字,你猜是多少?输入你的猜测或输入q退出: ");
            while(true){
                string val = Console.ReadLine();
                if(val=="q"){
                    break;
                }
                int a = Convert.ToInt32(val);
                if(a==number){
                    success +=1;
                    Console.WriteLine("恭喜你才对了,是否再来一个?[y/n]:");
                    string ret = Console.ReadLine();
                    replay = ret=="y";
                    break;
                }else{
                    Console.WriteLine("猜错了,再来一次或输入q退出:");
                }
            }
            if(!replay){
                break;
            }
        }
        Console.WriteLine("太棒了,你一共猜对了{0}次!", success);
    }
}
```

## 选项

### A

```csharp
public class Program{
    public static void Main(string[] args){
        Random rand = new Random(05);
        int success = 0;
        while(true){
            int number = rand.Next();
            bool replay = false;
            Console.Write("我想好了一个0-5内的数字,你猜是多少?输入你的猜测或输入q退出: ");
            while(true){
                string val = Console.ReadLine();
                if(val=="q"){
                    break;
                }
                int a = Convert.ToInt32(val);
                if(a==number){
                    success +=1;
                    Console.WriteLine("恭喜你才对了,是否再来一个?[y/n]:");
                    string ret = Console.ReadLine();
                    replay = ret=="y";
                    break;
                }else{
                    Console.WriteLine("猜错了,再来一次或输入q退出:");
                }
            }
            if(!replay){
                break;
            }
        }
        Console.WriteLine("太棒了,你一共猜对了{0}次!", success);
    }
}
```

### B

```csharp
public class Program{
    public static void Main(string[] args){
        Random rand = new Random();
        int success = 0;
        while(true){
            int number = rand.Next(0,5);
            bool replay = false;
            Console.Write("我想好了一个0-5内的数字,你猜是多少?输入你的猜测或输入q退出: ");
            while(true){
                string val = Console.Read();
                if(val=="q"){
                    break;
                }
                int a = Convert.ToInt32(val);
                if(a==number){
                    success +=1;
                    Console.WriteLine("恭喜你才对了,是否再来一个?[y/n]:");
                    string ret = Console.Read();
                    replay = ret=="y";
                    break;
                }else{
                    Console.WriteLine("猜错了,再来一次或输入q退出:");
                }
            }
            if(!replay){
                break;
            }
        }
        Console.WriteLine("太棒了,你一共猜对了{0}次!", success);
    }
}
```

### C

```csharp
public class Program{
    public static void Main(string[] args){
        Random rand = new Random();
        int success = 0;
        while(true){
            int number = rand.Next(0,5);
            bool replay = false;
            Console.Write("我想好了一个0-5内的数字,你猜是多少?输入你的猜测或输入q退出: ");
            while(true){
                var val = Console.ReadLine();
                if(val=="q"){
                    break;
                }
                if(val==number){
                    success +=1;
                    Console.WriteLine("恭喜你才对了,是否再来一个?[y/n]:");
                    string ret = Console.ReadLine();
                    replay = ret=="y";
                    break;
                }else{
                    Console.WriteLine("猜错了,再来一次或输入q退出:");
                }
            }
            if(!replay){
                break;
            }
        }
        Console.WriteLine("太棒了,你一共猜对了{0}次!", success);
    }
}
```