输出给出了不正确的答案。

huangapple go评论62阅读模式
英文:

Output gives incorrect answer

问题

我已经被困在这里有一段时间了,这段代码是用来使用嵌套的if语句计算工资的。
用户将输入他们的工作年限,并输入'B'表示蓝领工作,'W'表示白领工作。
我的代码可以运行,但它给了我两个不同的工资。

例如:
如果我输入3年的工作经验和'B'(蓝领工作)
工资应该是12,000,但在我的代码中它打印出10,000和12,000

这里有一个小提示以避免混淆:

<2年和'B' = 10,000

<2年和'W' = 20,000

<5年和'B' = 12,000

<5年和'W' = 40,000

5年以上和'B' = 15,000

5年以上和'W' = 75,000

#include <stdio.h>
int main () {
    char kind;
    int years;

    //输入
    printf("输入工作年限:");
    scanf("%d",&years);
    printf("输入工作类型(B表示蓝领,W表示白领):");
    scanf(" %c",&kind);

    //条件
    if(years<=1) {
        if(kind=='B')
            printf("工资:10,000");
        else 
            printf("工资:20,000");
    }
    
    if(years<=2 || years>=4) {
        if(kind=='B')
            printf("工资:12,000");
        else 
            printf("工资:40,000");
    }

    if(years>=5) {
        if(kind=='B')
            printf("工资:15,000");
        else 
            printf("工资:75,000");
    }
    
    return 0;
}
英文:

so I've been stuck here for a while, this code is to compute their salary using nested if.
the user will input their years in work and enter either 'B' for blue collar and 'W' for white collar job.
my code runs but it gives me 2 diff salary

for example:
if I enter 3 years of work and 'B' (blue collar)
the salary should be 12,000 but in my code it prints 10,000 and 12,000

here's a little guide to avoid confusion:

<2 yrs and 'B' = 10,000

<2 yrs and 'W' = 20,000

<5 yrs and 'B' = 12,000

<5 yrs and 'W' = 40,000

5yrs+ and 'B' = 15,000

5yrs+ and 'W' = 75,000

#include &lt;stdio.h&gt;
int main () {
char kind;
int years;

//input
printf(&quot;Enter years of work: &quot;);
scanf(&quot;%d&quot;,&amp;years);
printf(&quot;Enter kind of work: &quot;);
scanf(&quot; %c&quot;,&amp;kind);

//condition
if(years&lt;=1) {
    if(kind==&#39;B&#39;)
        printf(&quot;Salary: 10,000&quot;);
        else 
            printf(&quot;Salary: 20,000&quot;);
}

if(years&lt;=2 || years&gt;=4) {
    if(kind==&#39;B&#39;)
        printf(&quot;Salary: 12,000&quot;);
        else 
            printf(&quot;Salary: 40,000&quot;);
}

if(years&gt;=5) {
    if(kind==&#39;B&#39;)
        printf(&quot;Salary: 15,000&quot;);
        else 
            printf(&quot;Salary: 75,000&quot;);
}

return 0;

}

答案1

得分: 1

首先,你需要注意到这里的 "kind" 是一个字符,而不是字符串。如果要将它变成字符串,你需要这样写:

char kind[];

然后,你需要使用 "(string name here)" 来比较字符串,而不是 '(string name here)',后者用于指定字符。无论如何,如果你将它作为字符来扫描会更简单和方便:

char kind;
scanf(" %c", &kind);

第二个问题是嵌套的 if 语句。你需要在嵌套的 if 语句内部放置大括号,例如:

if (years < 2) {
    if (kind == 'B') {
        printf("Salary: 10,000");
    } else {
        printf("Salary: 20,000");
    }
}

在 C 语言中,如果你写成 "%c",程序将跳过输入,请记得写成 " %c"。

英文:

ok so firstly you need to see that "kind" here is a char and not string, to make it a string you have to write

char kind[];

and you need to put "(string name here)" to compare a string and not '(string name here)', which is used to specify character.
anyways, it would be much more simpler and convenient if you just scan it as a char

 char kind;
 scanf(&quot; %c&quot;,&amp;kind);

second problem here is with the nested if case.
you need to put the {} after the if cases inside the nested if
for example:

if(years&lt;2)
{
if(kind==&#39;B&#39;)
   {
    printf(&quot;Salary: 10,000&quot;);
   }
    else 
        printf(&quot;Salary: 20,000&quot;);
 }

in c there is a default problem if you write

  &quot;%c&quot;

then the program will skip the input so please remember to write

  &quot; %c&quot;

答案2

得分: 0

我喜欢用最具体的条件开始我的嵌套条件语句,有时如果它们排在最前面可能会产生一些后果。我稍微调整了一下顺序,并在开头增加了另一个检查,并将你的年限条件从 years > 2  years < 4 改成了 years >= 2  years <= 4,这样我们对要捕捉的条件就更具体了。我总是把嵌套条件看作是沿着梯子下降,最终正确的位置会“捕捉”到你,你只需确保确实被正确的语句捕捉到。我测试了这个代码,在我的编译器中运行得很好,但是在一些在线编译器中运行时似乎会出问题。老实说,我不知道为什么,但是 C 有时候确实很奇怪。

总之,我在嵌套条件中更喜欢的方法是,在开始时尽可能具体,把更宽泛的可能性留到最后。瞄准星星,最终也能落到月亮上,哈哈。

英文:

I like to start my nested conditionals with the input with the most specific conditions first, sometimes lose statements can have some consequences if they are first. I moved it around a bit and added another check in the start and changed your years > 2 and years < 4 to years >= 2 and years <= 4, so we are a little more specific with what were trying to catch. I always looked at nested conditionals as falling down a ladder, and eventually the right spot "catches" you and you just got to make sure you get caught by the correct statement for sure. I tested this and it seems to work fine for me in my compiler, but I ran it in a few online and they seemed to throw a fit. Honestly no idea why, but C is weird sometimes.

Main point the way I prefer to do it when nesting conditionals, be as specific as humanly possible at the beginning, save the wider possibilities for the end. shoot for the stars, and land amongst the moon lol

#include &lt;stdio.h&gt;
int main () {
char kind;
int years;

//input
printf(&quot;Enter years of work: &quot;);
scanf(&quot;%d&quot;,&amp;years);
printf(&quot;Enter kind of work: &quot;);
scanf(&quot;%s&quot;,&amp;kind);

//condition
// Start off with a check to rule out anything above 5
if ( years &lt; 5 ) {
    // Add the or equal too signs, to help tighten the conditions
    if ( years &lt;= 4 &amp;&amp; years &gt;= 2) {
        if ( kind == &#39;B&#39; ) {
            printf(&quot;Salary: 12,000&quot;);
        }
        else {
            printf(&quot;Salary: 40,000&quot;);
        }
    }
    // This is the tricky one, so we nest it deeper. for some reason
    // this likes to get triggered allot, we could possibly just do years &lt;= 1
    // to eliminate the number 2 entirely, since 2 is above
    else if ( years &lt; 2 ) {
        if ( kind == &#39;B&#39; ) {
            printf(&quot;Salary: 10,000&quot;);
        }
        else {
            printf(&quot;Salary: 20,000&quot;);
        }
    }
}
// Edited to ass the missing = for input = 5 that I forgot haha.
else if ( years &gt;= 5 ) {
    if ( kind == &#39;B&#39; ) {
        printf(&quot;Salary: 15,000&quot;);
    }
    else {
        printf(&quot;Salary: 75,000&quot;);
    }
}
return 0;
}

huangapple
  • 本文由 发表于 2023年3月7日 12:25:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75658032.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定