你干嘛哈哈哎哟 发表于 2022-10-18 21:18:03

求解决方法

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
int main()
{
int a,g,h;
int getch();
srand((unsigned int) time(NULL));
        while(getch())
        printf("%d,%d\n",rand()%6+1,rand()%6+1);

return 0;
大佬帮我看看,如何比较输出的两个数的大小,就是这两个rand()%6+1,rand()%6+1,用rand()%6+1给两个变量赋值输出的值一样

jackz007 发表于 2022-10-18 21:36:55

本帖最后由 jackz007 于 2022-10-18 21:39 编辑

      不会呀,试试这个代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>

int main(void)
{
      int i                                                                            ;
      time_t t                                                                         ;
      srand((unsigned) time(& t))                                                      ;
      for(i = 0 ; i < 20 ; i ++) printf("%d , %d\n" , rand() % 6 + 1 , rand() % 6 + 1) ;
}
      编译、运行实况:
D:\\C>g++ -o x x.c

D:\\C>x
5 , 2
6 , 4
5 , 2
5 , 6
2 , 4
3 , 2
4 , 6
6 , 5
2 , 1
4 , 6
3 , 4
1 , 6
6 , 6
6 , 5
4 , 3
4 , 2
4 , 3
2 , 3
1 , 4
5 , 5

D:\\C>
      连续两次调用产生的两个随机数相等的概率并不高。

你干嘛哈哈哎哟 发表于 2022-10-18 21:43:02

jackz007 发表于 2022-10-18 21:36
不会呀,试试这个代码:

      编译、运行实况:


我想知道的是我那个编码运行后出现的两个数如何去比较大小{:5_100:}

jackz007 发表于 2022-10-18 21:50:23

你干嘛哈哈哎哟 发表于 2022-10-18 21:43
我想知道的是我那个编码运行后出现的两个数如何去比较大小

         简单啊,难道不能存进变量后直接比较吗?关键是你比它后准备要干什么?这个才重要!

你干嘛哈哈哎哟 发表于 2022-10-18 21:59:10

jackz007 发表于 2022-10-18 21:50
简单啊,难道不能存进变量后直接比较吗?关键是你比它后准备要干什么?这个才重要!

最后一步就是比较大小而已{:5_109:}

你干嘛哈哈哎哟 发表于 2022-10-18 22:00:09

本帖最后由 你干嘛哈哈哎哟 于 2022-10-18 22:01 编辑

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
int main()
{
int a,g,h;
int getch();
srand((unsigned int) time(NULL));
        while(getch())
        printf("%d,%d\n",rand()%6+1,rand()%6+1);
    g=rand()%6+1;
    h=rand()%6+1;
    if(g>h)
    printf("你赢了\n");
    else
    printf("你输了\n");
return 0;                              
}
你说的存进变量是这个意思吗, 这样也比较不了大小

jackz007 发表于 2022-10-18 22:16:18

本帖最后由 jackz007 于 2022-10-18 22:22 编辑

      比较大小的代码根本就没有进循环。
      试试这个代码
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>

int main(void)
{
      int a , g , h                        ;
      int getch()                        ;
      srand((unsigned int) time(NULL))   ;
      while(getch() != '\r') {
                g = rand() % 6 + 1         ;
                h = rand() % 6 + 1         ;
                if(g > h) printf("你赢了\n") ;
                else printf("你输了\n")      ;
      }
      return 0                           ;
}
      编译、运行实况:
D:\\C>g++ -o x x.c

D:\\C>x
你赢了
你输了
你赢了
你赢了
你赢了
你输了
你输了
你赢了
你输了
你输了
你赢了
你输了
你输了
你赢了
你输了

D:\\C>
      这个代码在按下回车键的时候会退出循环。

你干嘛哈哈哎哟 发表于 2022-10-18 22:46:48

本帖最后由 你干嘛哈哈哎哟 于 2022-10-18 23:10 编辑

jackz007 发表于 2022-10-18 22:16
比较大小的代码根本就没有进循环。
      试试这个代码



谢谢,但为什么代码没有进循环运行后他不会打印出来呢

jackz007 发表于 2022-10-18 23:26:22

本帖最后由 jackz007 于 2022-10-19 00:59 编辑

你干嘛哈哈哎哟 发表于 2022-10-18 22:46
谢谢,但为什么代码没有进循环运行后他不会打印出来呢

      因为你写的那个代码必须把赋值、比较和打印语句一起纳入循环体内才能看到你所期望的效果,否则,你安排循环干什么!
页: [1]
查看完整版本: 求解决方法