Visual Studio 2015中的if-else语句问题

Rel*_*ind -2 c if-statement

我是编程的初学者.我在C#中创建了一个简单的程序,但它无法正常工作.当我键入"rezistenta"时,它应该运行条件当我输入"capacity"时, if (valoare=="rezistenta") 它应运行第二个if: if(valoare=="capacitate") 在两种情况下程序运行最后一个,如果条件,它会跳过前两个.

该程序:

#define _CRT_SECURE_NO_WARNINGS  //directive preprocesor
#include<stdio.h>
#include<conio.h>


void main(void)
{
    char valoare[100];
    float C1, C2, CS, CP;
    float R1, R2, Rs, Rp;


    printf("\nCapacitate sau Rezistenta? ");
    scanf("%s", &valoare);
    printf("\nAti introdus= %s", valoare);

    if (valoare == "rezistenta")
    {
        printf("\nIntroduceti valorile rezistentelor: ");
        scanf("%f%f", &R1, &R2);
        Rs = R1 + R2;
        printf("\nRezistenta echivalenta serie este: *%6.3f*", Rs);
        Rp = (R1*R2) / (R1 + R2);
        printf("\nRezistenta echivalenta paralel: *%6.3f*", Rp);
    }
    else if (valoare == "capacitate")
    {
        printf("\nIntroduceti valorile capacitatilor: ");
        scanf("%f%f", &C1, &C2);
        CS = (C1*C2) / (C1 + C2);
        printf("\nValoarea capacitatilor serie este = *%-6.4f*", CS);
        CP = C1 + C2;
        printf("\nValoarea capacitatilor in paralel este= *%-6.4f*", CP);
    }
    else
        printf("\nSunteti nehotarat vi le dau pe amandoua");



    printf("\nIntroduceti valorile rezistentelor: ");
    scanf("%f%f", &R1, &R2);
    Rs = R1 + R2;
    printf("\nRezistenta echivalenta serie este: *%6.3f*", Rs);
    Rp = (R1*R2) / (R1 + R2);
    printf("\nRezistenta echivalenta paralel: *%6.3f*", Rp);

    printf("\nIntroduceti valorile capacitatilor: ");
    scanf("%f%f", &C1, &C2);
    CS = (C1*C2) / (C1 + C2);
    printf("\nValoarea capacitatilor serie este = *%-6.4f*", CS);
    CP = C1 + C2;
    printf("\nValoarea capacitatilor in paralel este= *%-6.4f*", CP);



    _getch();



}//end main
Run Code Online (Sandbox Code Playgroud)

小智 5

你确定这是C#吗?它看起来像C.对于C,它使用printf(""),但C#应该是Console.WriteLine("")或Console.Write("")

无论如何,如果你使用C,你不能像这样进行字符串比较:

if (valoare == "rezistenta")     //this is wrong
Run Code Online (Sandbox Code Playgroud)

正确的方法应该是:

if(strcmp(valoare, "rezistenta") == 0)
Run Code Online (Sandbox Code Playgroud)

当然,您必须将库包含在最顶层:

#include <string.h>
Run Code Online (Sandbox Code Playgroud)

请好好试试.