相关疑难解决方法(0)

在Arduino上将int或String转换为char数组

我从我的Arduino上的一个模拟引脚获取一个int值.如何String将其连接到a 然后将其转换Stringchar[]

有人建议我尝试char msg[] = myString.getChars();,但我收到一条getChars不存在的消息.

string arduino char

77
推荐指数
3
解决办法
28万
查看次数

"if"条件下的变量赋值

我最近失去了一些时间来弄清楚我的代码中的错误是由一个错字引起的:

if(a=b)
Run Code Online (Sandbox Code Playgroud)

代替:

if(a==b)
Run Code Online (Sandbox Code Playgroud)

我想知道是否有任何特殊情况你想要在if语句中为变量赋值,或者如果没有,为什么编译器不会发出警告或错误?

c++ if-statement

29
推荐指数
7
解决办法
6万
查看次数

PHP用户类(登录/注销/注册)

开始尝试构建类,我开始将用户注册/登录转换为单个类.想要在走得太远之前停下来寻求反馈.

class UserService
{
    private $_email;
    private $_password;

    public function login($email, $password)
    {
        $this->_email = mysql_real_escape_string($email);
        $this->_password = mysql_real_escape_string($password);

        $user_id = $this->_checkCredentials();
        if($user_id){
            $_SESSION['user_id'] = $user_id;
            return $user_id;
        }
        return false;
    }

    protected function _checkCredentials()
    {
        $query = "SELECT *
                    FROM users
                    WHERE email = '$this->_email'";
        $result = mysql_query($query);
        if(!empty($result)){
            $user = mysql_fetch_assoc($result);
            $submitted_pass = sha1($user['salt'] . $this->_password);
            if($submitted_pass == $user['password']){
                return $user['id'];
            }
        }
        return false;
    }   
}
Run Code Online (Sandbox Code Playgroud)

我与课程有关的一个问题是:我应该像这样构建它:

$User = new UserService();
$User->login($_POST['email'], $_POST['password']);
Run Code Online (Sandbox Code Playgroud)

login方法自动调用_checkCredentials方法的位置.或者它应该像:

$User = new …
Run Code Online (Sandbox Code Playgroud)

php oop login class

26
推荐指数
2
解决办法
8万
查看次数

无意中使用=而不是==

看起来

if (x=y) { .... }
Run Code Online (Sandbox Code Playgroud)

代替

if (x==y) { ... } 
Run Code Online (Sandbox Code Playgroud)

是许多邪恶的根源.

为什么并非所有编译器都将其标记为错误而不是可配置警告?

我有兴趣找出构造if (x=y)有用的案例.

c c++ compiler-construction programming-languages

12
推荐指数
8
解决办法
2482
查看次数

为什么Java API在if语句中有看似奇怪的赋值?

我是编程和Java的新手.我注意到,在Java API中,if语句中有一些奇怪的赋值方法.

以下是Map界面的示例:

default V replace(K key, V value) {
    V curValue;
    if (((curValue = get(key)) != null) || containsKey(key)) {
        curValue = put(key, value);
    }
    return curValue;
}
Run Code Online (Sandbox Code Playgroud)

以这种方式嵌套赋值是否有某种好处?这纯粹是一种风格选择吗?为什么不在curValue第一次声明时进行赋值?

// why not do it like this?
default V replace(K key, V value) {
    V curValue = get(key); // not nested
    if (curValue != null || containsKey(key)) {
        curValue = put(key, value);
    }
    return curValue;
}
Run Code Online (Sandbox Code Playgroud)

我在Map界面和其他地方的许多新添加的Java 8方法中都注意到了这一点.这种嵌套分配的形式似乎是不必要的.

编辑:Map界面中的另一个示例:

default V computeIfAbsent(K key,
        Function<? super K, ? extends …
Run Code Online (Sandbox Code Playgroud)

java java-8

10
推荐指数
1
解决办法
263
查看次数

我应该检查malloc()是否成功?

应该在每个malloc()之后检查它是否成功?malloc()是否有可能失败?那么会发生什么?

在学校我们被告知我们应该检查,即:

arr = (int) malloc(sizeof(int)*x*y);
if(arr==NULL){
    printf("Error. Allocation was unsuccessful. \n");
    return 1;
}
Run Code Online (Sandbox Code Playgroud)

有什么做法?我可以这样做吗:

if(!(arr = (int) malloc(sizeof(int)*x*y))
    <error>
Run Code Online (Sandbox Code Playgroud)

c malloc error-handling runtime-error allocation

7
推荐指数
2
解决办法
1万
查看次数

更简洁的方式来编写以下语句

是否有更简洁的方法来编写以下C++语句:

int max = 0;

int u = up();
if(u > max)
{
 max = u;
}

int d = down();
if(d > max)
{
max = d;
}

int r = right();
max = r > max ? r : max;  
Run Code Online (Sandbox Code Playgroud)

具体是有一种方法可以在if语句/三元运算符中嵌入函数返回的赋值吗?

c++

5
推荐指数
1
解决办法
404
查看次数

比较值与'='时,'=='运算符的用途是什么?

首先,请注意我理解==用于比较两个表达式,而=用于为变量赋值.但是,python是一种干净的语言,语法要求极低,这似乎是一个简单的操作员.此外,我不是要开始辩论或讨论,而是要了解是否有一些我缺少的东西可以提高我的编程知识.

就像(在python中)我们不需要将变量声明为int或字符串,并且语言根据分配的值确定这个,为什么'if'语句不能简单地确定它=是a比较,不是作业?

另一个例子是python在其他语言中删除了许多{}和[]并简单地使用了缩进,因为缩进和使用大括号是多余的.在我看来,这if foo == goo:也是多余的.但也许我还没有意识到这一点.因此问题!

python

5
推荐指数
2
解决办法
157
查看次数

是否有理由不在while块声明中分配变量?

每当我做这样的事情......

var obj;

while (obj = doSomething()) {
  // something with obj
}
Run Code Online (Sandbox Code Playgroud)

JSHint告诉我warning 84| Expected a conditional expression and instead saw an assignment..但是,do会obj = doSomething()返回doSomething()赋值期间返回的值,因此以这种方式编写while循环是有意义的.

有没有一个特定的原因,JSHint警告我,更重要的是,有没有理由不这样做?或者,我可以告诉JSHint忽略那些特定警告的行吗?

javascript node.js

2
推荐指数
1
解决办法
106
查看次数

在条件中分配表达是一种不好的做法?

这篇文章解释了为什么我使用这样的代码时会发出警告:

var htmlCollection = document.getElementsByClassName("class-name"),
    i = htmlCollection.length,
    htmlElement;

// Because htmlCollection is Live, we use a reverse iteration.
while (htmlElement = htmlCollection[--i]) { // **Warning?! Why?!**
    htmlElement.classList.remove("class-name");
}
Run Code Online (Sandbox Code Playgroud)

但这并没有解释为什么在一段时间内分配表达式是一种不好的做法?».

我也读过这个stackoverflow的答案,指出这种做法是好的.所以...

while (element = element.parentNode)类似语法的性能问题还是样式代码推荐?


顺便说一句,似乎«--i»运算符也是一种不好的做法.我在这篇文章中读到:

已知++(递增)和 - (递减)运算符通过鼓励过多的诡计来导致错误的代码.

这是某种玩笑?

javascript while-loop assignment-operator

2
推荐指数
1
解决办法
408
查看次数