【问题标题】:what is wrong if convert to conditional operator如果转换为条件运算符有什么问题
【发布时间】:2021-04-05 14:41:20
【问题描述】:

我想将“if”转换为“条件运算符 (:?)”。我试图写它,但我收到一个错误expected expression before "for"

原代码:

#include <stdio.h>
int main ()
{
    int n, i, k, a [10], new;
    printf ("n:");
    scanf ("% d", & n);
    printf ("Initial \ n");
    for (i = 0; i <n; i ++)
    {
        a [i] = i;
        printf ("% d. value:% d \ n", i, a [i]);
    }
    printf ("Enter the index value you want to change:");
    scanf ("% d", & k);
    if ((k> = 0) && (k <n))
    {
        for (i = 1; i <= n; i ++)
        {
            if (i == k)
            {
                printf ("What number will you replace with array index% d?:", a [i]);
                scanf ("% d", & new);
                a [i] = new;
            } // if (i == k) end
        } // end of for loop

                        printf ("Last state \ n");
            for (i = 0; i <n; i ++)
            {
                printf ("% d. value:% d \ n", i, a [i]);
            }
    } // end of if statement
    else
        printf ("You did not enter a value within the array boundaries. \ n");
    return 0;
}

我想把它转换成条件形式。

我试过了:

#include <stdio.h>
int main ()
{
    int n, i, k, a [10], new;
    printf ("n:");
    scanf ("% d", & n);
    printf ("Initial \ n");
    for (i = 0; i <n; i ++)
    {
        a [i] = i;
        printf ("% d. value:% d \ n", i, a [i]);
    }
    printf ("Enter the index value you want to change:");
    scanf ("% d", & k);
    if ((k> = 0) && (k <n))

        for (i = 1; i <= n; i ++)

        {
            (i == k)?printf("What number will you replace with array index% d?:", a [i]), scanf("% d", & new),(a [i] = new): for(i = 0; i < n; i ++), printf("Last state \ n"), printf("% d. value:% d \ n", i, a [i]), printf("You did not enter a value within the boundaries of the array. \ n" );
        }
    return 0;

}

我收到一个错误:expected expression before "for". 我该怎么写?

【问题讨论】:

  • this一样编辑帖子
  • 你想用另一个值替换数组中的值吗?
  • for 循环是一个语句。您只能在条件运算符的部分中使用 _expression_s。像这样重写大的 if 语句对可读性没有帮助。坚持使用 if 语句。
  • 是的,我想将值替换为另一个值的数组。但我想用我从键盘输入的值替换它
  • 请理解空格很重要:\ n\n 不同,&gt; =&gt;= 不同。

标签: c if-statement conditional-operator


【解决方案1】:

此处不要使用空格 scanf("%d", & n);尝试将 % 与 d 和 & 与 n 连接

#include <stdio.h>
int main ()
{
int n, i, index, a[10], value;
printf ("n:");
do
{
    scanf ("%d",&n);
}while(n<1||n>10);

printf ("Initial \ n");
for (i=0;i<n;i++)
{
    a[i]=i;
    printf ("\n%d. value:%d \n", i, a [i]);
}
printf ("Enter the index value you want to change:");
scanf ("%d",&index);
if ((index>=0)&&(index<n))// write like this : >= 
    {
         printf("What number will you replace with array index% d?:",a[index]);
         scanf("%d",&value);
         a[index]=value;
    }
    for (i=0;i<n;i++)
    {
        printf ("[%d]", a [i]);
    }

return 0;

}

【讨论】:

  • 谢谢,但我们没有办法在打印最后一个值时缩短吗?
  • @BüşraSarıkaya:数组中的最后一个值?
  • 不要使用if,还要使用条件运算符:?
  • 我不明白你到底想要什么
  • 转角。 do { scanf ("%d",&amp;n); }while(n&lt;1||n&gt;10); 是文件末尾的潜在无限循环。用于检查 scanf() 的返回值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-03
  • 2010-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-09
  • 1970-01-01
相关资源
最近更新 更多