简体   繁体   English

我该如何解决这个数字加倍和减半的问题?

[英]How can I solve this number doubling and halving problem?

I feel kind of dumb for asking this but I can't figure out this code challenge.问这个问题我觉得有点愚蠢,但我无法弄清楚这个代码挑战。 I have to initialize a number with a value of one.我必须初始化一个值为 1 的数字。 Then I have to double this number till it's higher than 1000. After which it will be halved until it reaches 1 again, vice versa.然后我必须将这个数字加倍直到它高于 1000。之后它将减半直到再次达到 1,反之亦然。

This is what I have so far.这就是我到目前为止所拥有的。 Obviously, it doesn't work as once it goes over 1000 and gets halved it will meet the requirement to get doubled again.显然,它不起作用,因为一旦超过1000并减半,它将满足再次加倍的要求。 I feel pretty stupid for not seeing the solution.没有看到解决方案,我觉得很愚蠢。 Any help would be appreciated.任何帮助,将不胜感激。

session_start();

$getal = 1;

if(isset($_SESSION['getal'])){
    if($_SESSION['getal'] <= 1000){
        $_SESSION['getal'] *= 2;
    }elseif ($_SESSION['getal'] > 1000){
        $_SESSION['getal'] *= 0.5;
    }
}
else{
    $_SESSION['getal'] = $getal;
}

You need to keep a "direction" in session.您需要在 session 中保持“方向”。

Keep the multiplier to 2 until the value 1000 is reached.将乘数保持为 2,直到达到值 1000。 Then, change the multipler to.5 until you reach the value 1 (or less than 2).然后,将乘数更改为.5,直到达到值 1(或小于 2)。

session_start();

$getal = 1;

if (isset($_SESSION['getal'])) {
    if ($_SESSION['mult'] > 1 && $_SESSION['getal'] >= 1000) {
        $_SESSION['mult'] = .5;
    }
    elseif ($_SESSION['getal'] < 2) {
        $_SESSION['mult'] = 2;
    }

    $_SESSION['getal'] *= $_SESSION['mult'];
}
else{
    $_SESSION['getal'] = $getal;
    $_SESSION['mult'] = 2;
}

You need to store & check current direction (up/down), working code would be您需要存储和检查当前方向(上/下),工作代码将是

<?php

session_start();

$getal = 1;

if (!isset($_SESSION['dir'])) {
    $_SESSION['dir'] = 'up';
}

if(isset($_SESSION['getal'])){
    if ($_SESSION['getal'] <= 1000 && $_SESSION['dir'] === 'up') {
        $_SESSION['getal'] *= 2;

        $_SESSION['dir'] = 'up';
    } else {
        $_SESSION['getal'] *= 0.5;

        $_SESSION['dir'] = 'down';

        if ($_SESSION['getal'] == 1) {
            $_SESSION['dir'] = 'up';
        }
    }
} else {
    $_SESSION['getal'] = $getal;
}

echo $_SESSION['getal'];
echo $_SESSION['dir'];

Your code is almost there, you just need to store the number you are multiplying by and check it, and you will be all good.您的代码几乎就在那里,您只需要存储要乘以的数字并检查它,一切都会好起来的。

The basic idea is to store the multiplier as a session variable as well.基本思想是将乘数也存储为 session 变量。 You will need to change the multiplier whenever you get to 1 or above 1,000.每当您达到 1 或超过 1,000 时,您都需要更改乘数。 I used a ternary statement to do this, because it is just assigning a variable value (which is what ternaries are for), but an if/then would work just as well.我使用三元语句来执行此操作,因为它只是分配一个变量值(这是三元的用途),但 if/then 也可以。

<?php 

    session_start();

    if (isset($_SESSION['getal'])) {
        //the math
        $_SESSION['getal'] *= $_SESSION['multiplier'];
        //set the multiplier for the next iteration
        $_SESSION['multiplier'] = $_SESSION['getal'] > 1000 ? .5 : ($_SESSION['getal'] <= 1 ? 2 : $_SESSION['multiplier']);
    }
    else { 
        //initialize
        $_SESSION['getal'] = 1;
        $_SESSION['multiplier'] = 2;
    }

    echo $_SESSION['getal'];
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM