由于直接在一个回答中展示多种电脑语言的游戏代码可能过于冗长,我将为你提供几种流行编程语言(Python、JavaScript、Java 和 C++)中的简单“猜数字”游戏代码示例。

在这里插入图片描述

Python
python
import random

def game():
number = random.randint(1, 100) joy-your-future.com
guess = None
attempts = 0

while guess != number:  
    guess = int(input("猜一个1到100之间的数字: "))  
    attempts += 1  
    if guess < number:  
        print("太低了!")  
    elif guess > number:  
        print("太高了!")  
  
print(f"恭喜你,你在{attempts}次尝试后猜对了!")  

game()
JavaScript (Node.js 或浏览器控制台)
javascript
let number = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;

while (guess !== number) {
guess = parseInt(prompt(“猜一个1到100之间的数字:”));
attempts++;
if (guess < number) {
console.log(“太低了!”);
} else if (guess > number) {
console.log(“太高了!”);
}
}

console.log(恭喜你,你在${attempts}次尝试后猜对了!);
Java
java
import java.util.Random;
import java.util.Scanner;

public class GuessNumber {
public static void main(String[] args) {
Random rand = new Random();
int number = rand.nextInt(100) + 1;
Scanner scanner = new Scanner(System.in);
int guess = 0;
int attempts = 0;

    while (guess != number) {  
        System.out.print("猜一个1到100之间的数字: ");  
        guess = scanner.nextInt();  
        attempts++;  
        if (guess < number) {  
            System.out.println("太低了!");  
        } else if (guess > number) {  
            System.out.println("太高了!");  
        }  
    }  

    System.out.println("恭喜你,你在" + attempts + "次尝试后猜对了!");  
}  

}
C++
cpp
#include
#include
#include

using namespace std;

int main() {
srand(time(0));
int number = rand() % 100 + 1;
int guess = 0;
int attempts = 0;

while (guess != number) {  
    cout << "猜一个1到100之间的数字: ";  
    cin >> guess;  
    attempts++;  
    if (guess < number) {  
        cout << "太低了!" << endl;  
    } else if (guess > number) {  
        cout << "太高了!" << endl;  
    }  
}  

cout << "恭喜你,你在" << attempts << "次尝试后猜对了!" << endl;  
return 0;  

}
以上代码都是简单的“猜数字”游戏,每种语言都有其独特的输入/输出方法和特性。由于直接在一个回答中展示多种电脑语言的游戏代码可能过于冗长,我将为你提供几种流行编程语言(Python、JavaScript、Java 和 C++)中的简单“猜数字”游戏代码示例。

Python
python
import random

def game():
number = random.randint(1, 100)
guess = None
attempts = 0

while guess != number:  
    guess = int(input("猜一个1到100之间的数字: "))  
    attempts += 1  
    if guess < number:  
        print("太低了!")  
    elif guess > number:  
        print("太高了!")  
  
print(f"恭喜你,你在{attempts}次尝试后猜对了!")  

game()
JavaScript (Node.js 或浏览器控制台)
javascript
let number = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;

while (guess !== number) {
guess = parseInt(prompt(“猜一个1到100之间的数字:”));
attempts++;
if (guess < number) {
console.log(“太低了!”);
} else if (guess > number) {
console.log(“太高了!”);
}
}

console.log(恭喜你,你在${attempts}次尝试后猜对了!);
Java
java
import java.util.Random;
import java.util.Scanner;

public class GuessNumber {
public static void main(String[] args) {
Random rand = new Random();
int number = rand.nextInt(100) + 1;
Scanner scanner = new Scanner(System.in);
int guess = 0;
int attempts = 0;

    while (guess != number) {  
        System.out.print("猜一个1到100之间的数字: ");  
        guess = scanner.nextInt();  
        attempts++;  
        if (guess < number) {  
            System.out.println("太低了!");  
        } else if (guess > number) {  
            System.out.println("太高了!");  
        }  
    }  

    System.out.println("恭喜你,你在" + attempts + "次尝试后猜对了!");  
}  

}
C++
cpp
#include
#include
#include

using namespace std;

int main() {
srand(time(0));
int number = rand() % 100 + 1;
int guess = 0;
int attempts = 0;

while (guess != number) {  
    cout << "猜一个1到100之间的数字: ";  
    cin >> guess;  
    attempts++;  
    if (guess < number) {  
        cout << "太低了!" << endl;  
    } else if (guess > number) {  
        cout << "太高了!" << endl;  
    }  
}  

cout << "恭喜你,你在" << attempts << "次尝试后猜对了!" << endl;  
return 0;  

}
以上代码都是简单的“猜数字”游戏,每种语言都有其独特的输入/输出方法和特性。

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值