[英]How do I use an array in a switch-case function?
I ask the user to give me 10 integer values for an array A1 and then I print those values onto the console screen.我要求用户为数组 A1 提供 10 个整数值,然后我将这些值打印到控制台屏幕上。 I did/do the same for another array B1.
我对另一个数组 B1 做了/做同样的事情。 I then print a list of options, for instance option 1: "Sort A1 values in ascending order".
然后我打印一个选项列表,例如选项 1:“按升序对 A1 值排序”。 Since this is a school assignment I have to use a switch-case statement but I am having trouble accessing the arrays with the same values again and the program is not giving me the option to even press 1, for option one.
由于这是一项学校作业,我必须使用 switch-case 语句,但是我再次访问具有相同值的数组时遇到问题,并且程序没有给我甚至按 1 的选项,作为选项一。
I've tried to understand some codes online but with no success, this is what i got so far.我试图在网上理解一些代码,但没有成功,这就是我到目前为止所得到的。
int option;
scanf("%d", &option);
switch (option)
{
case '1':
for( int i= 0; i<10; i++)
{
for(int x = i +1; x<10; x++)
{
if (a[x]<a[i])
{
int temp = a[i];
a[i]= a[x];
a[x] = temp;
}
}
{
printf("\n\n Ascending Order For A1: ");
for (int i = 0; i < 10; i++)
{
printf(" %d ", a[i]);
}
THIS IS HOW FAR I AM WITH THE OUTPUT <<
这是我与输出的距离<<
Please Enter 10 Values For A1: 2 3 4 5 9 8 7 6 4 8请为 A1 输入 10 个值:2 3 4 5 9 8 7 6 4 8
10 Values Chosen For A1: 2 3 4 5 9 8 7 6 4 8为 A1 选择的 10 个值:2 3 4 5 9 8 7 6 4 8
Please Enter 10 Values For B2: 4 3 7 2 6 8 9 5 8 12请为 B2 输入 10 个值:4 3 7 2 6 8 9 5 8 12
10 Values Chosen For B2: 4 3 7 2 6 8 9 5 8 12为 B2 选择的 10 个值:4 3 7 2 6 8 9 5 8 12
Please choose one of the following options: *请选择以下选项之一:*
1: Sort A1 values in Ascending Order * 1:按升序对 A1 值进行排序 *
1 <<<<<<<<<<< THIS IS SUPPOSED TO EXECUTE OPTION 1, AND OUTPUT VALUES OF A1 IN ASCENDING ORDER. 1 <<<<<<<<<<< 这应该执行选项 1,并按升序输出 A1 的值。
Program ended with exit code: 0程序以退出代码结束:0
you are storing the input in an int
value, but in your case you are using a char
, so 1(int)
is not equal to 1(char)
that is the reason the case '1':
is not executing.您将输入存储在
int
值中,但在您的情况下,您使用的是char
,因此1(int)
不等于1(char)
这就是case '1':
未执行的原因。 Try尝试
case 1:
On the reason why it is not asking for the input to enter the 1
to sort, it might have something in the buffer from previous input, without the rest of the code it is difficult to help you with that part.由于它不要求输入输入
1
进行排序的原因,它可能在缓冲区中有来自先前输入的内容,如果没有其余代码,则很难帮助您完成该部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.