Python - 猜数字游戏
python
import random
def game(): tzxl8023.com
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
while guess != number_to_guess:
guess = int(input('猜一个1到100之间的数字: '))
attempts += 1
if guess < number_to_guess:
print('太小了!')
elif guess > number_to_guess:
print('太大了!')
print(f'恭喜你,你猜对了!用了{attempts}次尝试。')
if name == “main”:
game()
JavaScript (HTML内嵌) - 简单的点击计数器
html
0
<script>
let count = 0;
function incrementCount() {
count++;
document.getElementById('count').textContent = count;
}
</script>
Java - 简单的控制台程序(不是游戏,但类似) java import java.util.Scanner;
public class SimpleConsoleApp {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println(“请输入你的名字:”);
String name = scanner.nextLine();
System.out.println("你好, " + name + “!”);
}
}
C# - 控制台猜数字游戏(类似于Python版本)
csharp
using System;
class Program
{
static void Main()
{
Random rnd = new Random();
int numberToGuess = rnd.Next(1, 101);
int guess = 0;
int attempts = 0;
while (guess != numberToGuess)
{
Console.Write("猜一个1到100之间的数字: ");
guess = int.Parse(Console.ReadLine());
attempts++;
if (guess < numberToGuess)
Console.WriteLine("太小了!");
else if (guess > numberToGuess)
Console.WriteLine("太大了!");
}
Console.WriteLine($"恭喜你,你猜对了!用了{attempts}次尝试。");
}
}
以上代码只是简单示例,你可以根据自己的需要进行扩展和改进。由于篇幅限制,我无法为每种编程语言都提供一个完整的小游戏代码,但我可以为你提供几个简单示例,涵盖几种流行的编程语言。
Python - 猜数字游戏
python
import random
def game():
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
while guess != number_to_guess:
guess = int(input('猜一个1到100之间的数字: '))
attempts += 1
if guess < number_to_guess:
print('太小了!')
elif guess > number_to_guess:
print('太大了!')
print(f'恭喜你,你猜对了!用了{attempts}次尝试。')
if name == “main”:
game()
JavaScript (HTML内嵌) - 简单的点击计数器
html
0
<script>
let count = 0;
function incrementCount() {
count++;
document.getElementById('count').textContent = count;
}
</script>
Java - 简单的控制台程序(不是游戏,但类似) java import java.util.Scanner;
public class SimpleConsoleApp {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println(“请输入你的名字:”);
String name = scanner.nextLine();
System.out.println("你好, " + name + “!”);
}
}
C# - 控制台猜数字游戏(类似于Python版本)
csharp
using System;
class Program
{
static void Main()
{
Random rnd = new Random();
int numberToGuess = rnd.Next(1, 101);
int guess = 0;
int attempts = 0;
while (guess != numberToGuess)
{
Console.Write("猜一个1到100之间的数字: ");
guess = int.Parse(Console.ReadLine());
attempts++;
if (guess < numberToGuess)
Console.WriteLine("太小了!");
else if (guess > numberToGuess)
Console.WriteLine("太大了!");
}
Console.WriteLine($"恭喜你,你猜对了!用了{attempts}次尝试。");
}
}
以上代码只是简单示例,你可以根据自己的需要进行扩展和改进。