C ++ If / Else语句问题

时间:2011-03-25 02:18:38

标签: c++ function if-statement

我对这些If / Else语句的语法做错了什么 我很确定它与我的大括号放置有关。 我书中的所有例子都只包含非常原始/简单的例子(并不是说这段代码不简单; b / c就是这样)。我想我的一般问题也是你如何将这两个单独的陈述分开。当我执行时,似乎两个独立的函数混合在一起。

  //
    tens = rand / 10;
    if (tens =  2){
            cout << "twenty ";
    else if (tens = 3)
            cout << "thirty ";
    else if (tens = 4)
            cout << "forty ";
    else if (tens = 5)
            cout << "fifty ";
    else if (tens = 6)
            cout << "sixty ";
    else if (tens = 7)
            cout << "seventy ";
    else if (tens = 8)
            cout << "eighty ";
    else if (tens = 9)
            cout << "ninety ";
    }

    //
    ones = rand % 10;
    if (ones =  0){
            cout << "\n";
    else if (ones = 1)
            cout << "one\n";
    else if (ones = 2)
            cout << "two\n";
    else if (ones = 3)
            cout << "three\n";
    else if (ones = 4)
            cout << "four\n";
    else if (ones = 5)
            cout << "five\n";
    else if (ones = 6)
            cout << "six\n";
    else if (ones = 7)
            cout << "seven\n";
    else if (ones = 8)
            cout << "eight\n";
    else if (ones = 9)
            cout << "nine\n";
    }

9 个答案:

答案 0 :(得分:6)

我认为,对于这个特殊问题 if-else 甚至不需要!

我宁愿推荐这个解决方案:

const char *stens[] = {"", "", "twenty", "thirty", "forty", "fifty",
                               "sixty", "seventy", "eighty", "ninty"};
const char *sones[] = {"", "one", "two", "three", "four", "five", 
                               "six", "seven", "eigth", "nine"};

//make sure  0<= rand <= 99
 cout << stens[ rand / 10 ] << " " << sones[ rand % 10 ] << endl;

在线演示:http://www.ideone.com/K7HxS

对于您使用 if-else 所面临的问题,您使用分配运算符,而不是 equality ,因为每个人已经指出了。

答案 1 :(得分:5)

单个等号是一个赋值,而不是相等的测试。

您应该将ones == N用于某个号码N

您似乎没有正确使用大括号({}),您的if语句应该如下所示:

tens = rand / 10;
if (tens == 2){
        cout << "twenty ";
}else if (tens == 3){

请注意else之前和3)之后的大括号。

此外,我建议在此方案中使用switch语句,这可能会导致一些更易于阅读和更易于管理的代码:

tens = rand / 10;
switch(tens){
  case 2: cout << "twenty "; break;
  case 3: cout << "thirty "; break;
  case 4: cout << "fourty "; break;
  // ...
  case 9: cout << "ninety "; break;
}

答案 2 :(得分:3)

使用“==”而不是“='。”=“是赋值运算符;而”==“逻辑等于。

答案 3 :(得分:2)

是的,你是以错误的方式使用花括号。这样做:

if (tens ==  2)
{
        // your commands
}
else
{
        // your commands
}

请使用“==”运算符进行相等比较,而不是“=”。

答案 4 :(得分:1)

是的,您需要在第一个if之后关闭括号。

答案 5 :(得分:1)

为什么不使用SWITCH CASE,认为你必须将案例计算值转换为int即

ones = int (rand % 10);
ones = int (rand / 10);

开关案例的例子

switch (x) {
  case 1:
    cout << "x is 1";
    break;
  case 2:
    cout << "x is 2";
    break;
  default:
    cout << "value of x unknown";
  }

答案 6 :(得分:0)

另外,你的“if”之后的大括号应该在第一个“else”之前与之匹配

if (something) {
   something
} else if (something) {
   something
}

答案 7 :(得分:0)

赋值运算符=通常总是返回true,因为它返回值赋值的成功。等于运算符==是你的追求。它将测试两个值的等效性。

此外,if语句后跟单个语句或代码块。在您的示例中,您缺少终止括号。

// This is the assignment operator with a single statement.
if ( a = b )
    cout << "I am always true!" << endl;

// This is the equality operator with a code block.
if ( a == b ) {
    cout << "I am sometimes true!" << endl;
} else {
    cout << "I am more likely to be true." << endl;
}

答案 8 :(得分:0)

你必须在每个if或else if子句之后关闭花括号,如

tens = rand / 10;
if (tens =  2) {
        cout << "twenty "; }
else if (tens = 3) {
        cout << "thirty ";}
else if (tens = 4)
        cout << "forty "; }
else if (tens = 5) {
        cout << "fifty "; }
else if (tens = 6) {
        cout << "sixty "; }
else if (tens = 7) {
        cout << "seventy "; }
else if (tens = 8) {
        cout << "eighty "; }
else if (tens = 9) {
        cout << "ninety "; }
//
ones = rand % 10;
if (ones =  0) {
        cout << "\n"; }
else if (ones = 1) {
        cout << "one\n"; }
else if (ones = 2) {
        cout << "two\n"; }
else if (ones = 3) {
        cout << "three\n"; }
else if (ones = 4) {
        cout << "four\n"; }
else if (ones = 5) {
        cout << "five\n"; }
else if (ones = 6) {
        cout << "six\n"; }
else if (ones = 7) {
        cout << "seven\n"; }
else if (ones = 8) {
        cout << "eight\n"; }
else if (ones = 9) {
        cout << "nine\n"; }