在C语言中,不能为扑克牌程序的单个字符变量指定字符串
我正在学习C语言,并试图编写一个简单的程序,该程序接受扑克牌的缩写输入并输出一个句子。例如,输入9H为“红桃九”或14S为“黑桃王牌”,其中11、12、13和14分别为杰克、皇后、国王和王牌 我遇到的问题是获取单个字符的输入并为其分配一个字符串值,如“Diamonds” 我知道可能有一种更优雅的方式来做这件事,但我是个新手。我完全走错路了吗 我的编译器给了我“赋值从指针生成整数而无需强制转换” 正确的方法是什么?我完全卡住了 这是我到目前为止所拥有的在C语言中,不能为扑克牌程序的单个字符变量指定字符串,c,string,if-statement,char,C,String,If Statement,Char,我正在学习C语言,并试图编写一个简单的程序,该程序接受扑克牌的缩写输入并输出一个句子。例如,输入9H为“红桃九”或14S为“黑桃王牌”,其中11、12、13和14分别为杰克、皇后、国王和王牌 我遇到的问题是获取单个字符的输入并为其分配一个字符串值,如“Diamonds” 我知道可能有一种更优雅的方式来做这件事,但我是个新手。我完全走错路了吗 我的编译器给了我“赋值从指针生成整数而无需强制转换” 正确的方法是什么?我完全卡住了 这是我到目前为止所拥有的 #include <stdio.
#include <stdio.h>
int main()
{
//Declarations
char suit = "";
int rank = 0;
//Input
printf("Enter card: ");
scanf("%d%c", rank, suit);
//Process
if(rank = 14)
printf("Ace of %c", suit);
else if (rank = 13)
printf("King of %c", suit);
else if (rank = 12)
printf("Queen of %c", suit);
else if (rank = 11)
printf("Jack of %c", suit);
else if (rank = 10)
printf("Ten of %c", suit);
else if (rank = 9)
printf("Nine of %c", suit);
else if (rank = 8)
printf("Eight of %c", suit);
else if (rank = 7)
printf("Seven of %c", suit);
else if (rank = 6)
printf("Six of %c", suit);
else if (rank = 5)
printf("Five of %c", suit);
else if (rank = 4)
printf("Four of %c", suit);
else if (rank = 3)
printf("Three of %c", suit);
else if (rank = 2)
printf("Two of %c", suit);
else
printf("Not A valid entry");
if(suit = 'H')
suit ="Hearts";
else if(suit = 'D')
suit = "Diamonds";
else if (suit = 'S')
suit = "Spades";
else if (suit = 'C')
suit = "Clubs";
else
printf("not a valid entry")
return 0;
}
#包括
int main()
{
//声明
char suit=“”;
int秩=0;
//输入
printf(“输入卡片:”);
scanf(“%d%c”,等级,诉讼);
//过程
如果(排名=14)
printf(“c的王牌”,西服);
否则,如果(排名=13)
printf(“c之王”,西服);
否则如果(秩=12)
printf(“c女王”,西服);
else if(秩=11)
printf(“c的杰克”,西服);
否则,如果(排名=10)
printf(“百分之十”,西服);
else if(秩=9)
printf(“c的九分之九”,西服);
否则如果(秩=8)
printf(“八分之三”,西服);
else if(秩=7)
printf(“七分之三”,诉讼);
else if(秩=6)
printf(“六分之三”,西服);
否则如果(排名=5)
printf(“c的五分之一”,诉讼);
else if(秩=4)
printf(“四分之三”,西服);
否则如果(秩=3)
printf(“三分之三的c”,西服);
else if(秩=2)
printf(“两份c”,西服);
其他的
printf(“非有效条目”);
如果(诉讼='H')
suit=“Hearts”;
否则,如果(诉讼='D')
suit=“钻石”;
否则,如果(诉讼='S')
suit=“黑桃”;
否则,如果(诉讼='C')
suit=“俱乐部”;
其他的
printf(“不是有效条目”)
返回0;
}
您需要在if语句中使用比较运算符=
,而不是赋值运算符=
e、 g
而不是
if(rank = 14)
谢谢你的评论!真的很有帮助!开关是从第一步开始并使其工作的方法:
#include <stdio.h>
int main()
//Declarations
{
char suit = 0;
int rank = 0;
//Input
printf("This program converts shorthand notation for playing cards and converts\n");
printf("it into a sentence. Enter 14 for Ace 13 for King 12 For Queen and 11 for Jack.\n");
printf("enter S for Spade, C for Club, D for Diamond, and H for Hearts\n");
printf("\nEnter card shorthand: ");
scanf("%d%c", &rank, &suit);
switch(rank)
{
case 14:
printf("Ace");
break;
case 13:
printf("King");
break;
case 12:
printf("Queen");
break;
case 11:
printf("Eleven");
break;
case 10:
printf("Ten");
break;
case 9:
printf("Nine");
break;
case 8:
printf("Eight");
break;
case 7:
printf("Seven");
break;
case 6:
printf("Six");
break;
case 5:
printf("Five");
break;
case 4:
printf("Four");
break;
case 3:
printf("Three");
break;
case 2:
printf("Two");
break;
default:
printf("Improper input");
break;
}
switch (suit) // this switch converts the char value (suit) into a string.
{
case 'h': case 'H':
printf(" of Hearts");
break;
case 'd': case 'D':
printf(" of Diamonds");
break;
case 's': case 'S':
printf(" of Spades");
break;
case 'c': case 'C':
printf(" of Clubs");
break;
default:
printf(" try again");
break;
}
return 0;
}
#包括
int main()
//声明
{
char-suit=0;
int秩=0;
//输入
printf(“此程序转换扑克牌的速记符号并\n转换”);
printf(“将其转换成一个句子。输入14代表王牌13代表王牌12代表皇后,输入11代表杰克。\n”);
printf(“输入S代表黑桃,输入C代表梅花,输入D代表钻石,输入H代表红桃\n”);
printf(“\n输入卡速记:”);
scanf(“%d%c”、&rank和suit);
转职(职级)
{
案例14:
printf(“Ace”);
打破
案例13:
printf(“国王”);
打破
案例12:
printf(“女王”);
打破
案例11:
printf(“十一”);
打破
案例10:
printf(“十”);
打破
案例9:
printf(“九”);
打破
案例8:
printf(“八”);
打破
案例7:
printf(“七”);
打破
案例6:
printf(“六”);
打破
案例5:
printf(“五”);
打破
案例4:
printf(“四”);
打破
案例3:
printf(“三”);
打破
案例2:
printf(“两个”);
打破
违约:
printf(“输入不当”);
打破
}
switch(suit)//此开关将字符值(suit)转换为字符串。
{
案例“h”:案例“h”:
printf(“红心”);
打破
案例“d”:案例“d”:
printf(“钻石”);
打破
案例:案例:
printf(“黑桃”);
打破
案例“c”:案例“c”:
printf(“俱乐部”);
打破
违约:
printf(“重试”);
打破
}
返回0;
}
在C语言中,字符用单引号表示。空白将是'
。一个H
将是,'H'
,等等。如果您尝试,char suit=“”代码>正试图将字符串分配给字符,正如错误所指出的。如果您需要表示“无套件”的内容,只需使用零或空白即可。在完成这项编程任务之前,您可能希望通过书本或教程来复习您的C基础知识。这个程序有很多基本问题。scanf(“%d%c”,等级,适合)代码>=>scanf(“%d%c”、&rank和suit)代码>if(秩=xxx)
=>if(秩=xxx)
。西服也是如此。suit=“Spades”代码>是错误的。西装是char
,您不能为其分配字符串。此外,您也不能分配这样的字符串。如果suits=='C'
->suits=“Clubs”
没有意义,反之亦然,因为您正在将suits
分配给不同的类型,并且您的程序必须表现非常糟糕。事实上,char=“”
是错误的,如果使用编译器警告,您就会知道。
#include <stdio.h>
int main()
//Declarations
{
char suit = 0;
int rank = 0;
//Input
printf("This program converts shorthand notation for playing cards and converts\n");
printf("it into a sentence. Enter 14 for Ace 13 for King 12 For Queen and 11 for Jack.\n");
printf("enter S for Spade, C for Club, D for Diamond, and H for Hearts\n");
printf("\nEnter card shorthand: ");
scanf("%d%c", &rank, &suit);
switch(rank)
{
case 14:
printf("Ace");
break;
case 13:
printf("King");
break;
case 12:
printf("Queen");
break;
case 11:
printf("Eleven");
break;
case 10:
printf("Ten");
break;
case 9:
printf("Nine");
break;
case 8:
printf("Eight");
break;
case 7:
printf("Seven");
break;
case 6:
printf("Six");
break;
case 5:
printf("Five");
break;
case 4:
printf("Four");
break;
case 3:
printf("Three");
break;
case 2:
printf("Two");
break;
default:
printf("Improper input");
break;
}
switch (suit) // this switch converts the char value (suit) into a string.
{
case 'h': case 'H':
printf(" of Hearts");
break;
case 'd': case 'D':
printf(" of Diamonds");
break;
case 's': case 'S':
printf(" of Spades");
break;
case 'c': case 'C':
printf(" of Clubs");
break;
default:
printf(" try again");
break;
}
return 0;
}