Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby Can';除非';值是否可以增加?_Ruby - Fatal编程技术网

Ruby Can';除非';值是否可以增加?

Ruby Can';除非';值是否可以增加?,ruby,Ruby,我正在测试保留字,除非使用以下代码,这会增加x的值 x = 1 unless x >= 2 puts "x is less than 2" else puts "x is greater than 2" x = x + 1 end 输出为: x is less than 2 除非支持x的增量,否则是否支持?您的问题不清楚。但很明显,代码中唯一增加变量的地方是: x = x + 1 不会执行,因为总是满足条件,除非x>=2。x=x+1仅在逻辑的else部分中调用,因此它不会

我正在测试保留字
,除非使用以下代码
,这会增加
x
的值

x = 1
unless x >= 2
  puts "x is less than 2"
else
  puts "x is greater than 2"
  x = x + 1
end
输出为:

x is less than 2

除非
支持
x
的增量,否则
是否支持?

您的问题不清楚。但很明显,代码中唯一增加变量的地方是:

x = x + 1

不会执行,因为总是满足条件
,除非x>=2

x=x+1
仅在逻辑的
else
部分中调用,因此它不会递增,因为它获取了它的除非子句(并跳过else块)。你在寻找什么样的控制流?我想你正在混淆
循环
条件语句
你在寻找什么?