2015-11-10 59 views
-2

我是編程的初學者。 我在C#中做了一個簡單的程序,但它不能正常工作。 當我鍵入「rezistenta」它應該運行的條件 if (valoare=="rezistenta") 當我鍵入「與能力」它應該運行第二個如果: if(valoare=="capacitate") 在這兩種情況下,程序運行的最後一樣,它跳過第一2,如果條件。如果在Visual Studio 2015中的else語句麻煩

程序:

#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 
+0

你確定它的C#?不是C++? – vahnevileyes

+3

它看起來不像'C#'檢查這個問題比較字符串在C:http://stackoverflow.com/questions/8004237/how-do-i-properly-compare-strings-in-c –

+0

看起來像C編程給我。 – TejSoft

回答

5

你確定這是C#?它看起來像C.對於C,它用printf( 「」),但C#應該是Console.WriteLine( 「」)或Console.Write( 「」)

總之,如果你使用的是C,你不能做字符串比較是這樣的:

if (valoare == "rezistenta")  //this is wrong 

正確的方法應該是:

if(strcmp(valoare, "rezistenta") == 0) 

當然,你必須包括在上面的庫:

#include <string.h> 

請嘗試一下。

+0

謝謝你的工作,你說得對,就是C.我認爲C和C#是一樣的。祝你今天愉快! – RelaxedMind

2

在這裏你不能像這樣比較。 這裏

if(strcmp(valoare,"rezistenta")==0) 
{ 
//... 
} 

,幷包含一個頭文件

#include<string.h> 
+0

謝謝,它的工作原理 – RelaxedMind