【发布时间】:2019-11-18 19:35:11
【问题描述】:
我做了一个小骰子游戏,如果我掷 5 次相同的数字,我应该得到 Grand,如果我掷 4 次相同的数字,我应该得到扑克。
我的问题是我的代码只适用于“1”和“2”,如果我尝试用 4 获得扑克,则不算数
#include <stdio.h>
void abfrage(int wurf[], int size){
int i;
for (i=0; i<size; i++){
printf("Würfel %i: ", i+1);
scanf("%i", &wurf[i]);
}
}
// switch int*
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
// Sort the array
void bubbleSort(int wurf[], int size)
{
int i, j;
for (i = 0; i < size-1; i++){
for (j = 0; j < size-i-1; j++){
if (wurf[j] > wurf[j+1])
{
swap(&wurf[j], &wurf[j+1]);
}
}
}
}
void arrayausgabe(int wurf[], int size){
int u = 0;
for (u=0;u<size;u++) {
printf("array nummer %i: ", u); // err with "&"
printf("%i \n", wurf[u]);
}
}
void bewertung(int wurf[]){
int *x;
x = wurf;
if (*x==*(x+1)==*(x+2)==*(x+3)==*(x+4)) {
printf("Grand");
}else if (*x==*(x+1)==*(x+2)==*(x+3) || *(x+1)==*(x+2)==*(x+3)==*(x+4)) {
printf("Poker");
}else if ((*x==*(x+1)==*(x+2) && *(x+3)==*(x+4) )||(*(x+2)==*(x+3)==*(x+4) && *x==*(x+1))) {
printf("Full House");
}else {
printf("HAAA Verloren");
}
}
int main() {
int wurf[5];
printf("Programm Würfelspiel\nGrand\tgleiche Augenzahl auf allen 5 Würfeln\nPoker\tgleiche Augenzahl auf 4 Würfeln\nFull House\tgleiche und 2 gleiche Augenzahlen\n\nBitte gibt deine gewürfelten zahlen ein\n");
abfrage(wurf, 5);
bubbleSort(wurf, 5);
arrayausgabe(wurf, 5);
bewertung(wurf);
}
我在大一,如果代码看起来有点垃圾,很抱歉
【问题讨论】:
-
您能否添加 cmets 来解释这段代码的不同部分试图做什么?并更清楚地解释问题是什么,以及如何重新创建它?
-
做
*(x+1)是完全没有必要和混乱的;使用数组表示法访问数组:wurf[0] == wurf[1]... -
没有时间检查所有代码,但您的冒泡排序函数存在明显错误。 (1) 重新访问
for循环的上限和下限,以及 (2) 条件行if (wurf[j] > wurf[j+1])的索引。 -
您还应该检查来自
scanf()的返回值,以确保它读取了您期望的字段数。
标签: c arrays for-loop pointers bubble-sort