将值与数组元素进行比较

Posted

技术标签:

【中文标题】将值与数组元素进行比较【英文标题】:compare value to array element 【发布时间】:2018-03-05 12:46:44 【问题描述】:

当我得到用户输入 cin >> x;cin >> y;

然后我想将此与数组“定位线”L1[i][j] 进行比较,但是当我使用 if 语句时 - if (x && y == L1[i][j]) - if (x == L1[i] && y == L1[j])

我没有得到我需要的“HIT”结果。 这是一种扫雷项目。任何帮助或指点将不胜感激!

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string>

char L1[8][8], L2[16][16], L3[24][24]; // grid location of cords
char B1[8][8], B2[16][16], B3[24][24]; // grid location of bombs
int i, j;

using namespace std;

// load and print grids L1 L2 L3
void loadgrid_L1()

    for ( i = 0; i < 8; i++)
    
        for ( j = 0; j < 8; j++)
        
            L1[i][j] = 'X';
        
    

void printgrid_L1() 

    for ( i = 0; i < 8; i++)
    
        for ( j = 0; j < 8; j++)
        
            cout << L1[i][j];
        
        cout << ("\n");
    


void loadgrid_L2()

    for (i = 0; i < 16; i++)
    
        for (j = 0; j < 16; j++)
        
            L2[i][j] = 'X';
        
    

void printgrid_L2()

    for (i = 0; i < 16; i++)
    
        for (j = 0; j < 16; j++)
        
            cout << L2[i][j];
        
        cout << ("\n");
    


void loadgrid_L3()

    for (i = 0; i < 24; i++)
    
        for (j = 0; j < 24; j++)
        
            L3[i][j] = 'X';
        
    

void printgrid_L3()

    for (i = 0; i < 24; i++)
    
        for (j = 0; j < 24; j++)
        
            cout << L3[i][j];
        
        cout << ("\n");
    


void bomb1() 

    L1[1][5] = 'O';


void bomb2()

    L2[3][4] = 'O';


void bomb3()

    L3[6][6] = 'O';


void menu() //need to work on this section

    string lvl, x, y;

    cout << "Please select a lvl to play L1, L2, L3 " << endl;
    getline(cin, lvl);

    if (lvl == "L1")
    
        loadgrid_L1();
        //moved bomb() from here

        cout << "Please enter your co-ordinates for your move" << endl;
        cout << "Cord 1: "; 
        getline(cin, x);
        cout << "Cord 2: ";
        getline(cin, y);

        if (x == L1[i])
        
            cout << "HIT!" << endl;
        
        else if (x != L1[i])
        
            cout << "SUCCESS!" << endl;
        

        bomb1();
        printgrid_L1();


    
    else if (lvl == "L2")
    
        loadgrid_L2();
        bomb2();
        printgrid_L2();
    
    else if (lvl == "L3")
    
        loadgrid_L3();
        bomb3();
        printgrid_L3();
    
    else if ((lvl != "L1") && (lvl != "L2") && (lvl != "L3"))
    
        cout << "You must pick a lvl" << endl;
        while ((lvl != "L1") && (lvl != "L2") && (lvl != "L3"))
        
            getline(cin, lvl);
            if (lvl == "L1")
            
                loadgrid_L1();
                bomb1();
                printgrid_L1();
            
            else if (lvl == "L2")
            
                loadgrid_L2();
                bomb2();
                printgrid_L2();
            
            else if (lvl == "L3")
            
                loadgrid_L3();
                bomb3();
                printgrid_L3(); // needs to stop
            
        
    


int main()
   
    menu();
    return 0;

【问题讨论】:

表达式lvl != "L1" || "L2" || "L3" 无法正常工作。它检查lvl != "L1" 是否为假,如果是,则检查表达式"L2" 是否为假,永远不会。 另一个问题,您在loadgrid_L1 函数和printgrid 函数中使用正确的数组索引,但在其他loadgrid 函数中没有。为什么? 完全通过了,现在将使用 printgrid 和 loadgrid 解决这个问题,谢谢 【参考方案1】:

您可以轻松地将每个 loadgrid 和 printgrid 函数更改为 2 个单独的函数:

void loadgrid( unsigned int lvl ) 
    if ( lvl < 0 || lvl > 3 ) 
        std::cout << "Error: invalid level entered: " << lvl << std::endl;
        return;
    

    int size = 0;
    if ( lvl == 1 )
        size = 8;
    else if ( lvl == 2 ) 
        size = 16;
    else if ( lvl == 3 )
        size = 24;

    for ( int i = 0; i < size; i++ ) 
        for ( int j = 0; j < size; j++ ) 
            if ( lvl == 1 ) 
                L1[i][j] = 'X';
            else if ( lvl == 2 )
                L2[i][j] = 'X';
            else if ( lvl == 3 )
                L3[i][j] = 'X';               
        
       

您可以按照上面相同的模式打印网格。

然后,当您调用该函数时,您只需传递要加载和/或打印的 lvl。

【讨论】:

那太好了,谢谢你,很干净,但是我会把它放在一边,直到我弄清楚这个程序的比较部分然后我可以清理它,但是很好的建议谢谢你

以上是关于将值与数组元素进行比较的主要内容,如果未能解决你的问题,请参考以下文章

使用 StartsWith 将值与字符串数组进行比较

sql 如何将值与SQL进行比较?

将值与可能较大的数据集进行比较

将值与mysql中表的所有字段进行比较[重复]

将值与mysql中表的所有字段进行比较[重复]

SQL将值与特定年份的平均值进行比较