小编Eri*_*tor的帖子

使用花括号来隔离我想在C++中多次使用的变量

在下面的代码中,我有一个while语句用于确保输入字符串少于10个字符.我已经声明了一个bool被叫cont,我用它来告诉while循环在我的条件满足后停止.

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

int main()
{
    using namespace std;

    cout << "Enter a string less than 10 characters long: ";

    string teststring;

    {
        bool cont(false);

        //if input is 10 or more characters, ask for input again until it is less
        while (!cont)
        {
            getline(cin, teststring);

            if (teststring.length() >= 10)
            {
                cout << "Too long, try again: ";
            }
            else
            {
                cout << "Thank you.\n\n";
                cont = true;
            }

        }
    }

    return 0; …
Run Code Online (Sandbox Code Playgroud)

c++ loops curly-braces while-loop

6
推荐指数
1
解决办法
132
查看次数

你能比较多个变量,看看它们在 JS 中是否都等于相同的值吗?

在 Javascript 中工作,我试图查看在给定时间是否有 5 个不同的变量都包含相同的值。该值可能是 6 个事物中的 1 个,但我需要查看它们是否都相同,无论它是哪个值。我试过这个:

if (die1 == die2 & die1 == die3 & die1 == die4 & die1 == die5) {
    yahtzeeQualify == true;
}
Run Code Online (Sandbox Code Playgroud)

和这个:

if (die1 == die2 == die3 == die4 == die5) {
    yahtzeeQualify == true;
}
Run Code Online (Sandbox Code Playgroud)

这两个都有效吗?如果是这样,我的代码中可能有其他地方的错误......如果没有,我真的很感激一些帮助。我在一个名为 dieArray 的数组中也有这些变量,如下所示:

var dieArray = [die1, die2, die3, die4, die5];
Run Code Online (Sandbox Code Playgroud)

学习一种通过数组来做到这一点的方法会很酷,但如果这不合逻辑,那就这样吧。我会继续尝试自己想办法,但直到现在我都被卡住了......

javascript compare equals

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

使用枚举类值在C++中调用数组元素

我的代码:

Enumerations.h

#ifndef ENUMERATIONS_H
#define ENUMERATIONS_H

enum class Employees
{
    ERIC,
    RYAN,
    EMILY
};

#endif
Run Code Online (Sandbox Code Playgroud)

Structs.h

struct Employee
{
    std::string name;
    double wage;
};
Run Code Online (Sandbox Code Playgroud)

Review.cpp

#include "stdafx.h"
#include <iostream>
#include <string>
#include "Enumerations.h"
#include "Structs.h"

Employee employeeArray[3];

    employeeArray[0] = { "Eric Sartor", 18.75 };
    employeeArray[1] = { "Ryan Ulch", 20.36 };
    employeeArray[2] = { "Emily Gover", 18.75 };

cout << employeeArray[Employees::RYAN].name;
Run Code Online (Sandbox Code Playgroud)

所以我正在尝试做一些我在C++教程中读到的东西,你可以通过枚举值调用数组元素(结构).在本教程的前面,建议如果您的编译器符合C++ 11(我的是),那么使用枚举类而不是常规枚举会更好.

我注意到当试图通过Employees::RYAN枚举值从我的数组中调用我的元素时,它给出了一个错误,表示"表达式必须具有整数或未整合的枚举类型".如果我从枚举中删除class关键字所以它只是enum Employees我将数组索引更改为RYAN,它工作正常.我错过了什么,或者这不适用于枚举类?

希望我足够清楚.在本教程的示例中,他实际上并没有使用枚举类,只是一个常规枚举,即使他早先明确说过,如果你可以做到这一点......希望有人可以为我澄清这一点!

c++ arrays enums struct c++11

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

如何通过箭头键连续/平滑地移动元素?

我正在与Javascipt进行一场原始游戏,以获得乐趣.按下箭头键时,我设置了移动角色的功能,如下所示:

document.getElementById("character").style.top = 0;
document.getElementById("character").style.left = 0;

document.body.onkeydown = function() {
var e = event.keyCode,
    charTop = parseInt(document.getElementById("character").style.top),
    charLeft = parseInt(document.getElementById("character").style.left);

    if (e == 40) { //down function
        document.getElementById("character").style.top = (parseInt(document.getElementById("character").style.top)) + 10 + "px";
    } else if (e == 37) { //left function
        document.getElementById("character").style.left = (parseInt(document.getElementById("character").style.left)) - 10 + "px";
    } else if (e == 39) { //right function
        document.getElementById("character").style.left = (parseInt(document.getElementById("character").style.left)) + 10 + "px";
    } else if (e == 38) { //up function
        document.getElementById("character").style.top = (parseInt(document.getElementById("character").style.top)) - …
Run Code Online (Sandbox Code Playgroud)

javascript onkeydown

3
推荐指数
2
解决办法
9412
查看次数

javascript 函数可以应用于某个 CSS 类的所有元素吗?

我有一个导航栏,其中每个按钮都会更改正文的背景。他们每个人都将其更改为不同的颜色。我创建onmouseoveronmouseout为每个按钮的功能来实现这一目标。但是,我想知道是否有一种方法可以只通过它们的类引用它们来编写每个函数中的一个?它们都具有相同的button. 有没有一种方法可以将函数应用于某个类的所有元素?我的代码:

function whichButton(x) {
    if (x==1)
        return "red";
    if (x==2)
        return "green";
    if (x==3)
        return "blue";
    if (x==4)
        return "orange";
    if (x==0)
        return initBG;
}

button1.onmouseover = function() {
    document.body.style.backgroundColor = whichButton(1);
}

button1.onmouseout = function() {
    document.body.style.backgroundColor = whichButton(0);
}

button2.onmouseover = function() {
    document.body.style.backgroundColor = whichButton(2);
}

button2.onmouseout = function() {
    document.body.style.backgroundColor = whichButton(0);
}

button3.onmouseover = function() {
    document.body.style.backgroundColor = whichButton(3);
}

button3.onmouseout = function() {
    document.body.style.backgroundColor = whichButton(0);
} …
Run Code Online (Sandbox Code Playgroud)

javascript css class function

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

PHP递归函数导致PHP耗尽内存?

Fatal error: Allowed memory size of 805306368 bytes exhausted (tried to
allocate 8192 bytes) in *directory* on line 6
Run Code Online (Sandbox Code Playgroud)

这是我尝试访问运行此脚本的页面时抛出的错误:

$root = '../public_html/';

function proccess($dir) {
    $items = scandir($dir);
    $result = array();
    foreach ($items as $item)
    {
        if (is_dir($item))
            $result[$item] = proccess($item);
        else
            array_push($result, $item);
    }
    return $result;
}

print_r(proccess($root));
Run Code Online (Sandbox Code Playgroud)

我想要完成的是构建一个关联数组,表示public_html我服务器上的目录中的目录树.我正在尝试为自己创建一个图形索引...主要是为了好玩,但这已经变成了关于递归的学习体验!

在我看来,这个函数看起来相当简单,我的服务器上没有那么多文件...所以除非我不小心创建了一个无限递归循环,否则我不明白为什么我的内存不足.

循环逻辑:扫描根目录,然后遍历生成的数组.如果找到另一个目录,请将其名称设置为$ result数组的键,然后再次运行proccess().如果找到文件,只需将文件名推送到$ result数组即可.

php

0
推荐指数
1
解决办法
428
查看次数