简体   繁体   English

数组的输入元素被识别为字符串类型而不是浮点类型

[英]Inputed elements of the array are being recognized as type string instead of type float

i'm trying to write a code that sorts 3 numbers inputed by the user (i cant use the sort() function) for a college activity.我正在尝试编写一个代码,对用户输入的 3 个数字进行排序(我不能使用 sort() 函数)用于大学活动。 I tried using parseFloat() to make it work but it still outputs an array of strings.我尝试使用 parseFloat() 使其工作,但它仍然输出字符串数组。

var readlineSync = require ('readline-sync');

var numeros = [];
var i = 0;
var aux = 0;

for (i = 0; i < 3; i++){
    numeros[i] = readlineSync.question ('Digite o numero ' + (i+1) +': ');
    parseFloat(numeros[i]);
}

console.log(numeros)

for (i = 0; i < 3; i++){
    for (j = i+1; j < 3; j++){
        if( numeros[i] > numeros[j]) {
                aux = numeros[i];
                numeros[i] = numeros [j];
                numeros[j] = aux;
            }
    }
}

console.log(numeros);

Any idea on how i can resolve this issue?关于如何解决这个问题的任何想法?

You are using parseFloat(numeros[i]);您正在使用parseFloat(numeros[i]); but you never assign the value to anything但你从不给任何东西赋值

const input = readlineSync.question ('Digite o numero ' + (i+1) +': ');
numeros[i] = parseFloat(input);

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

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