如果不是'和' '或'

时间:2016-07-26 17:31:29

标签: c

您好,这是我的作业代码。

首先"如果"测试所有骰子是否相等,也等于游戏的圆数。

第二"(否则)如果"测试所有骰子是否相等,但它们不等于圆数。

第三"(否则)如果"测试至少一个骰子是否不等于圆数。

第四个是测试没有等于圆数的骰子。

注意:printf只是为了测试语句

但是现在程序给出了四个。我该如何修复它并更好地编写这段代码呢?

int dice1=1, dice2=1, dice3=1, round=3, point=0;

    if(   (dice1 == dice2) && (dice2 == dice3) && (dice1 == dice3) && (dice1 == round) )    printf("1");        
    else if(   (dice1 == dice2) && (dice2 == dice3) && (dice1 == dice3) && (dice1 == round) )   printf("2");    
    else if( dice1==round ? point++ : point  || dice2==round ? point++ : point  ||   dice3==round ? point++ : point ) printf("3");  
    else printf("4");

4 个答案:

答案 0 :(得分:1)

我猜你正在期待第二个条件,所有骰子等于但不等于圆形,以触发。但是你还在检查:

dice1 == round

即。它与第一个if检查完全相同。另外,不是检查1 == 2,2 == 3,1 == 3你可以检查:

1 == 2 && 2 == 3

因为如果两个条件都评估为真,则表示1必须等于3。

答案 1 :(得分:1)

(function(module) {
  // Permit constructor
  function Market (opts) {
    for (keys in opts) {
      this[keys] = opts[keys];
    }
  }

  Market.all = [];

  Market.getData = function() {
    webDB.execute('SELECT * FROM marketdata', function(rows) {
      if (rows.length) {
        console.log('Market.getData: inside if');
        Market.all = rows;
      } else {
        console.log('Market.getData: inside else');
        $.get('https://data.seattle.gov/resource/3c4b-gdxv.json',
        function(data) {
          Market.all = data;
          if (!rows.length) {
            Market.all.forEach(function(singleMarket) {
              var market = new Market(singleMarket);
              market.insertPermit();
            });
          }
        });
      }
    });
  };

  Market.createTable = function(next) {
    console.log('inside Market.createTable');
    webDB.execute(
      'CREATE TABLE IF NOT EXISTS marketdata (' +
        'id INTEGER PRIMARY KEY, ' +
        'address VARCHAR(255), ' +
        'city_feature VARCHAR(255);'

    );
    Market.getData();
  };

  Market.prototype.insertPermit = function () {
    webDB.execute(
      [
        {
          'sql': 'INSERT INTO markettdata(address, city_feature) VALUES (?, ?);',
          'data': [this.address, this.city_feature],
        }
      ]
    );
  };

  Market.createTable();

  module.Market = Market;
})(window);

答案 2 :(得分:0)

if ((dice1 == dice2) && (dice2 == dice3) && (dice1 == round)) {
    printf("1");
} else if ((dice1 == dice2) && (dice2 == dice3) && (dice1 != round)) {
    printf("2");
} else if ((dice1 != round) || (dice2 != round) || (dice3 != round)) {
    printf("3");
} else if ((dice1 != round) && (dice2 != round) && (dice3 != round)) {
    printf("4");
}

答案 3 :(得分:0)

你的第三个if语句正在检查点
的值 你初始化指向0
你正在使用point ++(后增量),这意味着在if检查之后,point的值将从0增加到1。您可以使用++ point而不是point ++来修复它 这将是一个预增量,由if检查的值将是1而不是0