编写猜价格游戏

9、编写猜价格游戏

参考脚本如下

 1 #!/bin/bash
 2 #编写猜价格游戏
 3
 4 #定义正确价格
 5 Prnum=`expr $RANDOM % 20`              #获取一个1-20之间的随机数
 6 b=0
 7
 8 while true
 9 do
10   read -p "请输入价格(1-20):" Price    #输入价格
11   b=b+1
12   if [ $Price -eq $Prnum ];then        #判断输入的价格是否=随机生成的数字
13      echo "恭喜你,猜对啦!"
14      echo "价格是:$Price"
15      exit 0
16   elif [ $Price -gt $Prnum ];then
17      echo "太高了"
18   else
19      echo "太低了"
20   fi
21 done
时间: 2024-08-03 21:47:56

编写猜价格游戏的相关文章

c语言小程序:编写猜字游戏

#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int input=1; printf("欢迎使用猜数字游戏\n"); while (input) { printf("**********************\n"); printf("******* 1.start ******\n"); printf("

1.1 算法的作用:猜价格游戏

最近在c语言网学习c语言和算法,这些随笔当作笔记,以方便自己研究查阅 猜价格游戏介绍: 首先出示一件价格在999元以内的商品,参与者要猜出这件商品的价格.在猜价格的过程中,主持人会根据参与者给出的价格,相应地给出“高了”或“低了”的提示 1 #include <stdio.h> 2 int main() 3 { 4 int oldprice,price=0,i=0; 5 printf("请首先设置商品的真实价格:"); 6 scanf("%d",&

C语言:编写猜数字游戏,猜一个数字,一直到猜中为止

#include<stdio.h> #include<stdlib.h> #include<time.h> int game() {   int num=0;  int ret=0;  srand((unsigned int)time(NULL));  ret=rand;  ret=ret%100;  while(1)  {   scanf("%d",&num);    if(num>ret)    {     printf("

shell脚本写出一个简单的猜价格游戏

[[email protected] ~]# vim game.sh #!/bin/bash a=$(expr ${RANDOM} % 1000) #$RANDOM是一个环境变量,每次都会输出一个不一样的数,并且小于2的16次方 count=0 echo "这个商品的价格是(0-999)元之间,猜猜具体价格?" while true do let count++ read -p " 请输入您猜到的具体价格,并按Enter键确认:" b if [ $b -eq $a

编写一个猜数字游戏

编写一个程序,实现猜数字游戏,计算机随机产生一个数,输入猜的数,与计算机随机产生的数进行比较,当猜的数大于随机产生的数,给出提示猜的数过大,反之,给出提示猜的数太小.下面是具体的程序: #include <stdio.h> #include <stdlib.h> #include <time.h> void fun()        //定义fun函数,说明游戏的具体操作 {     int a=0;     int ret=rand()%100;           

c语言:编写猜数字小游戏。

编写猜数字小游戏. 程序: #include<stdio.h> #include<time.h> void menu() { printf("***欢迎来挑战猜数字游戏***\n"); printf("*****请选择开始或退出*****\n"); printf("******1.start 0.exit******\n"); } void game() { int num = 0; srand((unsigned)tim

C++编写的一个简单的猜数字游戏源码

将开发过程比较重要的一些内容段做个记录,下面内容段是关于C++编写的一个简单的猜数字游戏的内容. #include <iostream> #include <string> #include <cstdlib> #include <cctype> #include <ctime> #include <conio.h> using namespace std; int main () { int wins = 0; int losses

用C语言编写的简单的猜数字游戏

#include <stdio.h>#include <time.h>#include <stdlib.h>int main(){          int random = 0;          int input = 0;          int start = 1;          while (start)             {                   printf("*********************\n")

04-4. 猜数字游戏(15)

猜数字游戏是令系统随机产生一个100以内的正整数,用户输入一个数对其进行猜测,需要你编写程序自动对其与随机产生的被猜数进行比较,并提示大了(“Too big”),还是小了(“Too small”),相等表示猜到了.如果猜到,则结束程序.程序还要求统计猜的次数,如果1次猜出该数,提示“Bingo!”:如果3次以内猜到该数,则提示“Lucky You!”:如果超过3次但是在N(>3)次以内(包括第N次)猜到该数,则提示“Good Guess!”:如果超过N次都没有猜到,则提示“Game Over”,