我正在制作一个简单的纸张,摇滚,剪刀游戏(c#),除了用户可以键入的部分(例如)是或否,我做了所有内容,然后再次播放或退出。我该怎么做? 这是我目前的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Do you choose rock,paper or scissors");
string userChoice = Console.ReadLine();
Random r = new Random();
int computerChoice = r.Next(4);
if (computerChoice == 1)
{
if (userChoice == "rock")
{
Console.WriteLine("The computer chose rock");
Console.WriteLine("It is a tie ");
}
else if (userChoice == "paper")
{
Console.WriteLine("The computer chose paper");
Console.WriteLine("It is a tie ");
}
else if (userChoice == "scissors")
{
Console.WriteLine("The computer chose scissors");
Console.WriteLine("It is a tie ");
}
else
{
Console.WriteLine("You must choose rock,paper or scissors!");
}
}
else if (computerChoice == 2)
{
if (userChoice == "rock")
{
Console.WriteLine("The computer chose paper");
Console.WriteLine("Sorry you lose,paper beat rock");
}
else if (userChoice == "paper")
{
Console.WriteLine("The computer chose scissors");
Console.WriteLine("Sorry you lose,scissors beat paper ");
}
else if (userChoice == "scissors")
{
Console.WriteLine("The computer chose rock");
Console.WriteLine("Sorry you lose,rock beats scissors");
}
else
{
Console.WriteLine("You must choose rock,paper or scissors!");
}
}
else if (computerChoice == 3)
{
if (userChoice == "rock")
{
Console.WriteLine("The computer chose scissors");
Console.WriteLine("You win,rock beats scissors");
}
else if (userChoice == "paper")
{
Console.WriteLine("The computer chose rock");
Console.WriteLine("You win,paper beats rock");
}
else if (userChoice == "scissors")
{
Console.WriteLine("The computer chose paper");
Console.WriteLine("You win,scissors beat paper");
}
else
{
Console.WriteLine("You must choose rock,paper or scissors!");
}
}
Console.ReadLine();
}
}
}
帮助!...
答案 0 :(得分:1)
删除最后一个ReadLine
,将整个逻辑放在while
块中,如下所示:
bool keepPlaying = true;
while (keepPlaying) {
//game logic here
Console.WriteLine("New game? y/n");
ConsoleKeyInfo cki = Console.ReadKey(); //wait for player to press a key
keepPlaying = cki.KeyChar == 'y'; //continue only if y was pressed
}
答案 1 :(得分:0)
您可以将所有内容包装在do while
结构中:
class Program
{
static void Main(string[] args)
{
do
{
Console.WriteLine("Do you choose rock,paper or scissors");
string userChoice = Console.ReadLine();
// Rest of your code
}while(Console.ReadLine() == "yes");
}
}
这将执行您的代码一次,然后每次用户输入yes
时再次执行。
有了这个,你不需要任何私人变量,你就可以确定游戏至少会被执行一次。如果你想提示用户(单独的某些内容"再次播放?"),你可以在现有代码的末尾,}while
部分之前完成。
答案 2 :(得分:0)
首先,当你打电话给r.Next(4);
时,程序有机会生成一个从0到3的数字。所以computerChoice
的可能性是0,1,2,3 ......你的程序做了当它等于0时没有任何处理computerChoice
的代码,因此它会突然关闭。要解决这个第一次更改r.Next(4);
到r.Next(3);
,它将提供0,1,2的可能性。只需将else if (computerChoice == 3)
更改为`else if(computerChoice == 0)'。这将修复您的程序。其次,正如其他答案所说,围绕整个程序循环是明智的,这样你就可以多次玩RPS。
-mnursey
答案 3 :(得分:0)
希望这对你有用。
<!DOCTYPE HTML>
<html lang="sk">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image searcher</title>
</head>
<body>
<?php
$search_query = "Alchemilla vulgaris";
$search_query = urlencode( $search_query );
$url = "https://www.google.com/search?q=$search_query&tbm=isch&ved=2ahUKEwi_0dbpjJrxAhUU_BoKHVkmDOwQ2-cCegQIABAA&oq=$search_query";
echo $url;
echo "\n";
$html = file_get_contents( $url );
preg_match_all('#<div\s.*?(?:data-id=[\'"](.*?)[\'"]).*?>#is',$html, $matches );
var_dump($matches);
?>
</body>
</html>