頁:
[1]
我正在設計一個簡單的猜數字,但是寫錯了,拜託請幫我看一下
using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int ans = 38;
int guess = 0;
guess = int.Parse(Console.ReadLine());
Random rd = new Random();
ans = rd.Next(1,50);
if (guess >= ans)
{
Console.WriteLine("太大囉");
}
if (guess <= ans)
{
Console.WriteLine("太小囉");
}
if (guess == ans)
{
Console.WriteLine("答對了");
}
else
{
Console.WriteLine("答對了");
}
}
}
}...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div><div></div> 能否直接說明你的錯誤在哪?
目前是有看到一個錯誤:最後一個else應該不要,或者是改為Console.WriteLine("答錯了"); 本帖最後由 ChiChung 於 2009-5-19 05:01 PM 編輯
看起來是判斷完太大或太小時沒有重新輸入數字的機制
用個while迴圈包起來吧
還有System.Linq及using System.Collections.Generic是可以不必引入的
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int ans = 38;
int guess = 0;
guess = int.Parse(Console.ReadLine());
Random rd = new Random();
ans = rd.Next(1,50);
if (guess > ans)
{
Console.WriteLine("太大囉");
}
if (guess < ans)
{
Console.WriteLine("太小囉");
}
if (guess == ans)
{
Console.WriteLine("答對了");
}
}
}
發現三個錯誤~~
1.太大要用">"而不是">="
2.太小要用"<"而不是"<="
3.最後一個else要拿掉...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div>
頁:
[1]