在控制流中使用条件运算符时,比较运算符在Julia脚本文件中不起作用

在控制流中使用条件运算符时,比较运算符在Julia脚本文件中不起作用,julia,Julia,我尝试过不同的运算符,等等。当我将比较运算符与条件运算符组合在一起时,似乎没有任何效果 e = readline() if e == 0 println("e is equal to 0") else println("e is not equal to 0") end 预期结果是明显的,如果e=0,则e等于0,如果e!=0,则e不等于0 但是,它总是打印底线,e不等于0。这是因为readline返回一个字符串,该字符串从不等于整数(根据Julia使用的=的定义)

我尝试过不同的运算符,等等。当我将比较运算符与条件运算符组合在一起时,似乎没有任何效果

e = readline()

if e == 0
    println("e is equal to 0")
else
        println("e is not equal to 0")
end

预期结果是明显的,如果e=0,则e等于0,如果e!=0,则e不等于0


但是,它总是打印底线,e不等于0。

这是因为
readline
返回一个字符串,该字符串从不等于整数(根据Julia使用的
=
的定义)

以下是实现您所需的一些可能的方法:

  • 与字符串文字比较:
    如果e==“0”
  • 使用
    tryparse
    如果tryparse(Int,e)==0
    (如果
    e
    不是数字文字,则将返回
    nothing
  • 使用
    parse
    ,但如果使用
    try
    /
    catch
    ,而不是
    try
        p = parse(Int, e)
        p == 0 ? println("e is 0") : println("e is not 0")
    catch
        println("not even an integer.")
    end
    

    • 这是因为
      readline
      返回一个字符串,它永远不等于整数(根据Julia使用的
      =
      的定义)

      以下是实现您所需的一些可能的方法:

      • 与字符串文字比较:
        如果e==“0”
      • 使用
        tryparse
        如果tryparse(Int,e)==0
        (如果
        e
        不是数字文字,则将返回
        nothing
      • 使用
        parse
        ,但如果使用
        try
        /
        catch
        ,而不是
        try
            p = parse(Int, e)
            p == 0 ? println("e is 0") : println("e is not 0")
        catch
            println("not even an integer.")
        end
        

      if返回您不期望的分支的原因是@phg提供给您的分支(您得到了
      readline()提供的
      字符串

      对于我的代码,我使用以下函数解析终端中提供的用户提供的数据:

      function getUserInput(T=String,msg="")
        print("$msg ")
        if T == String
            return readline()
        else
          try
            return parse(T,readline())
          catch
           println("Sorry, I could not interpret your answer. Please try again")
           getUserInput(T,msg)
          end
        end
      end
      
      sentence = getUserInput(String,"Which sentence do you want to be repeated?");
      n        = getUserInput(Int64,"How many times do you want it to be repeated?");
      [println(sentence) for i in 1:n]
      println("Done!")
      

      if
      返回您不期望的分支的原因是@phg提供给您的分支(您得到了
      readline()
      提供的
      字符串)

      对于我的代码,我使用以下函数解析终端中提供的用户提供的数据:

      function getUserInput(T=String,msg="")
        print("$msg ")
        if T == String
            return readline()
        else
          try
            return parse(T,readline())
          catch
           println("Sorry, I could not interpret your answer. Please try again")
           getUserInput(T,msg)
          end
        end
      end
      
      sentence = getUserInput(String,"Which sentence do you want to be repeated?");
      n        = getUserInput(Int64,"How many times do you want it to be repeated?");
      [println(sentence) for i in 1:n]
      println("Done!")
      

      在C++中,我可以写<代码> int c;标准:cin>>c;如果(c==0){//dothis}
      在Julia中有没有办法读入非字符串?我知道了如何读入整数
      e=parse(Int64,readline())
      起作用。在C++中,可以使用<代码> PRINTLN(Type of(E))< /C> >进行测试,我可以写<代码> int c;标准:cin>>c;如果(c==0){//dothis}
      在Julia中有没有办法读入非字符串?我知道了如何读入整数
      e=parse(Int64,readline())
      起作用。您可以使用
      println(typeof(e))