2016-04-15 81 views
0

我使用此代碼打開網站。當我說「打開谷歌」它會打開谷歌:如何隨機打開多個網站?

if (r == "open google") 
{ 
    Process.Start("https://google.com"); 
} 

現在,我怎麼能隨機打開多個網站?我的意思是,當我說這是怎麼回事,這些網站就開一個隨機:http://www.pcmag.comhttps://www.ehow.com ...

我品嚐過的代碼,但我的軟件中打開所有的網站:

if (r == "what's new" || r == "what's up") 
{ 

    Process.Start("http://www.pcmag.com"); 
    Process.Start("https://www.ehow.com"); 
    Process.Start("http://www.zdnet.com"); 
    Process.Start("http://www.empireonline.com"); 
} 
+1

將它們放在一個數組中,然後生成一個0到1之間的隨機數,然後訪問由該隨機數表示的位置。 – npinti

回答

2
private static void OpenRandomSite() 
{ 
    var sites = new [] 
    { 
     "http://www.pcmag.com", 
     "https://www.ehow.com", 
     "http://www.zdnet.com" 
    }; 

    var rnd = new Random(); 

    Process.Start(sites[rnd.Next(sites.Length)]); 
} 

您的代碼會是這樣:

switch(r) 
{ 
    case "open google": 
     Process.Start("https://google.com"); 
     break; 

    case "what's up": 
     OpenRandomSite(); 
     break; 
} 
+0

感謝 - 只是一件事,我想用我的聲音打開那些隨機站點(我在我的軟件中使用語音識別)。 if(r ==「What's new」|| r ==「what's up」) { Process.Start(OpenRandomSite()); } r means = record - 當我使用process.start時,它顯示一個錯誤,無法將void轉換爲字符串。如何解決它? –

+0

查看編輯到我的答案:) –

+0

非常感謝你:)) –

0

這個函數會返回一個隨機網址:

public List<string> Sites = new List<string>() 
{ 
    "http://www.google.com", 
    "http://www.yahoo.com", 
    "http://www.somethingelse" 
}; 

Random randomizer = new Random(); 

public string RandomSite() 
{ 
    int r = randomizer.Next(Sites.Count); 
    return Sites[r]; 
} 

用法是自我解釋:RandomSite()將從您的收藏中返回一個隨機URL。