通过if else语句显示特定的输出

Show specific output through if else statement

本文关键字:输出 显示 if else 语句 通过      更新时间:2023-09-26

我正在研究如何在数据中显示下拉列表中的条目。基于用户输入的gui界面,但我遇到的问题与数据无关。GUI编程,而不是我缺乏javascript技能。

我有两个列表,一个显示Starsystem01作为名称。另一个是Planets,显示了Starsystem内部的特定行星。我如何编写这条语句,使这两个数组只在选择0或1时显示?

var jumpSwitcher = 0;
    this.Starsystems = 'dat.gui';
//Starsystem list
    gui.add(this, 'Starsystems', ['0', '1']).onChange(function(value) {
        if (value === '1') {
            jumpSwitcher = 1;
        }
    });
    this.Planets = 'dat.gui';
//if the var jumpSwitcher is 0, display these variables in the Planets array.
    if (jumpSwitcher === 0) {
        Planets = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune'];
    } 
//if the jumpSwitcher is 1, meaning if the user has clicked on "1" under the Starsystem, show these planets. 
    else if (jumpSwitcher === 1) {
        Planets = ['Ilos', 'Tuchanka', 'Palaven', 'Illium'];
    }
    gui.add(this, 'Planets', Planets).name("Planets in this system").onChange(function(value) {
    });

我想说,这应该工作,但不知何故它没有。当我简单地在语句中设置条件,如if (1>0) {show 1} else {show 2}时,它就会这样做。

PS:

var jumpSwitcher = 1;  
    this.Starsystems = 'dat.gui';
gui.add(this, 'Starsystems', ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']).onChange(function(value) {
    jumpSwitcher = +value;
});
this.Planets = 'dat.gui';
switch (jumpSwitcher) {
    case 1:    
        gui.add(this, 'Planets', ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune']).name("Planets in this system").onChange(function(value) {
        });    
        break;
    case 2:    
        gui.add(this, 'Planets', ['Ilos', 'Tuchanka', 'Illium', 'Palaven']).name("Planets in this system").onChange(function(value) {
        });
        break;    
}

需要重置jumpSwitcher

改变
if (value === '1') {
    jumpSwitcher = 1;
}

jumpSwitcher = +value;

设置jumpSwitcher为一个数值。

另一种解决方案是,如果数组内的值连接在一起,则将数组内的值更改为数字值而不是字符串。

gui.add(this, 'Starsystems', [0, 1]).onChange(function(value) {
//                           ^^^^^^
    jumpSwitcher = value;
});