Switch语句-类型'number'与类型'string'不具有可比性]]

问题描述 投票:0回答:1

我似乎在试图弄清楚如何使它正常工作时遇到问题-尝试传递字符串并检测字符串是否具有特定的运算符时,我继续遇到此错误,如果这样,则使用switch语句运行操作-有没有更好的方法让我使用switch语句在字符串中搜索运算符?

看到错误:Error

我的打字稿功能:

function getAns(){

    var FinalSum = (<HTMLInputElement>document.getElementById('input')).value

    switch(FinalSum){
        case FinalSum.indexOf("%"):     //Type 'number' is not comparable to type 'string'.
           try{
            getPercent();
           }catch(Error){
            InvalidEntry()
           }

           break;
        case FinalSum.indexOf("^"):   //Type 'number' is not comparable to type 'string'.
           try{
            getPercent();
           }catch(Error){
            InvalidEntry()
           }

           break;
        default:
           try{
                 var inputSum = (<HTMLInputElement>document.getElementById('input'))
                 var ans = eval(inputSum.value);
                (<HTMLInputElement>document.getElementById('answer')).value = ans;
                (<HTMLInputElement>document.getElementById('input')).placeholder = (<HTMLInputElement>document.getElementById('input')).value;
                (<HTMLInputElement>document.getElementById('input')).value = "";
           }catch(Error){
                InvalidEntry();
           }  
           break;
    }
}

[我似乎在试图弄清楚如何使它正常工作时遇到问题-尝试传递字符串并检测字符串是否具有特定的运算符,如果是这样的话,我仍然会遇到此错误,如果是这样...

typescript
1个回答
2
投票

switch (FinalSum) {表示将每种情况与finalSum进行比较。如果大小写表达式为===finalSum,则将满足大小写。

© www.soinside.com 2019 - 2024. All rights reserved.