CShell 脚本中的语法错误

发布于 2024-08-16 09:16:16 字数 487 浏览 5 评论 0原文

编写脚本读取正整数,然后计算以下序列: 如果数字是偶数,则减半 如果是奇数,则将其乘以 3 并添加 1

您应该重复此过程,直到该值为 1,打印出每个值以及您执行了多少次操作。

#! bin\csh

echo "please enter any integer number :) "

set count=0

set number=$<

while($number != 1)

   if($number % 2) then

       @ number = number * 3 + 1 

   else

       @ number = number / 2

   endif

   echo " $number "

   @ count = count ++

end

echo I performed these operations $count times

当我运行脚本时,出现以下错误: @:表达式语法。

Write Script to read a positive integer number then it computes the following sequence:
If the number is even, halve it
If it is odd multiply it by 3 and add1

You should repeat this process until the value is 1, printing out each value and how many of these operations you performed.

#! bin\csh

echo "please enter any integer number :) "

set count=0

set number=
lt;

while($number != 1)

   if($number % 2) then

       @ number = number * 3 + 1 

   else

       @ number = number / 2

   endif

   echo " $number "

   @ count = count ++

end

echo I performed these operations $count times

When I run the script I get the following error:
@: Expression Syntax.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恋竹姑娘 2024-08-23 09:16:16

我相信它的 $ 变量可以获取其值

$
    Obtains the value of the variable. 

@ var = $a + $x * $z

http://www.eng.hawaii。 edu/Tutor/csh.html

所以那就是

@count = $count +1 

这句话

#!bin\csh 

让我颤抖

I believe its $variable to obtain its value

$
    Obtains the value of the variable. 

@ var = $a + $x * $z

source http://www.eng.hawaii.edu/Tutor/csh.html

So that would be

@count = $count +1 

And the line

#!bin\csh 

makes me shiver

謌踐踏愛綪 2024-08-23 09:16:16

我得到正确的解决方案:)

#! /bin/csh

echo "please enter any integer number :) "

set count=0

set inc=1

set number=
lt;

while($number != 1)

   if($number % 2) then

       @ number = $number * 3 + 1 

   else

       @ number = $number / 2

   endif

   @ count = $count + $inc

   echo " $number "


end

echo I performed these operations $count times

i get correct solution :)

#! /bin/csh

echo "please enter any integer number :) "

set count=0

set inc=1

set number=
lt;

while($number != 1)

   if($number % 2) then

       @ number = $number * 3 + 1 

   else

       @ number = $number / 2

   endif

   @ count = $count + $inc

   echo " $number "


end

echo I performed these operations $count times
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文