【问题标题】:bash script to find the product of two numbers in succession up to a max numberbash脚本连续查找两个数字的乘积,直到最大数字
【发布时间】:2021-03-09 15:58:14
【问题描述】:

我使用了 shellcheck.net,脚本运行但输出与我预期的不一样,这里是脚本和输出,我再次尝试让脚本显示 1*2 2* 的结果3 3*4 等...直到范围的给定最大数,并显示一个计数,如 1. 2. 3. 等...任何输入都会有很大帮助。

#!/bin/bash
#A script to find the products of two nonnegative numbers in succession to each other up to a maximum number
a=1
b=2
prod=$((a*b))
count=1
echo "Input The max number to find product of numbers in succession with each other up to."
read -r maxnum
echo "Ok all the products of numbers in succesion to each other from 1 to $maxnum are as follows."
for (( b=2; b<=maxnum; ++b ))
do
        for (( a=1; a<b; ++a ))
        do
                for (( count=1; count<=maxnum; ++count ))
                do
                        echo "$count. $prod"
                done
        done
done
echo "You have now reached the end of your range of products with integers in succession to each other"

输出

Input The max number to find product of numbers in succesion with each other up to.
5
Ok all the products of numbers in succesion to each other from 1 to 5 are as follows.
1. 2
2. 2
3. 2
4. 2
5. 2
1. 2
2. 2
3. 2
4. 2
5. 2
1. 2
2. 2
3. 2
4. 2
5. 2
1. 2
2. 2
3. 2
4. 2
5. 2
1. 2
2. 2
3. 2
4. 2
5. 2
1. 2
2. 2
3. 2
4. 2
5. 2
1. 2
2. 2
3. 2
4. 2
5. 2
1. 2 
2. 2
3. 2
4. 2
5. 2
1. 2
2. 2
3. 2
4. 2
5. 2
1. 2
2. 2
3. 2
4. 2
5. 2
You have now reached the end of your range of products with integers in succesion to each other

【问题讨论】:

  • 请仅使用2 作为您的论点重新编辑您的Q。然后请包括您对该输入的预期输出。祝你好运。
  • 看起来你在循环之前设置了prod,但从不更新它;因此,它永远不会从“2”改变值。
  • 当您执行a=1; b=2; prod=$((a*b)) 时,它将使用ab 的当前值,并等效于prod=2。如果您更改ab 并希望prod 成为新产品,您应该再次更改prod=$((a*b))

标签: bash shell scripting git-bash


【解决方案1】:

从这个(和上一个)问题中收集各种 cmets,并进行一些清理:

$ cat abc.bash
#!/bin/bash

echo "Input The max number to find product of numbers in succession with each other up to."

read -r maxnum

echo "Ok all the products of numbers in succesion to each other from 1 to $maxnum are as follows."

for (( a=1; a<maxnum; a++ ))
do
    printf "%s.%s\n" "${a}" $((a*(a+1)))
done

echo "You have now reached the end of your range of products with integers in succession to each other"

几个示例运行:

$ abc.bash
Input The max number to find product of numbers in succession with each other up to.
3
Ok all the products of numbers in succesion to each other from 1 to 3 are as follows.
1.2
2.6
You have now reached the end of your range of products with integers in succession to each other

$ abc.bash
Input The max number to find product of numbers in succession with each other up to.
5
Ok all the products of numbers in succesion to each other from 1 to 5 are as follows.
1.2
2.6
3.12
4.20
You have now reached the end of your range of products with integers in succession to each other

注意:可能需要返回并根据 OP 所需的输出调整代码(仍有待添加到问题中)。

【讨论】:

    【解决方案2】:

    如果我理解正确,你会喜欢这样的:

    #!/bin/bash
    #A script to find the products of two nonnegative numbers in succession to each other up to a maximum number
    echo "Input The max number to find product of numbers in succession with each other up to."
    read -r maxnum
    echo "Ok all the products of numbers in succesion to each other from 1 to $maxnum are as follows."
    for (( count=1; count<maxnum; ++count ))
    do
        a=$count
        b=$((count+1))
        prod=$((a*b))
        echo "$count. $prod"
    done
    echo "You have now reached the end of your range of products with integers in succession to each other"
    

    没有必要使用这么多 for 循环。只需一次迭代计数,您就可以定义连续数字 a=count 和 b=count+1,直到达到 maxnum。这也是您正确索引迭代的唯一方法,因为在您的代码中,您正在重复计算两个不必要的循环迭代 a 和 b。

    此外,您应该建立一个条件,即您的最大读取数不应小于 2,否则您将没有两个连续的数字可以相乘。

    【讨论】:

      猜你喜欢
      • 2021-03-09
      • 1970-01-01
      • 2014-02-27
      • 2013-12-31
      • 1970-01-01
      • 2015-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多