gpt4 book ai didi

linux - Loop on Unix,几何图形的基本计算器

转载 作者:太空宇宙 更新时间:2023-11-04 10:09:05 26 4
gpt4 key购买 nike

我正在尝试创建一个几何图形计算器,但遇到了一个循环。我在 shell 脚本中使用菜单,但到目前为止运气不好。

有人可以帮我吗?

当前脚本是:

#!/bin/bash

echo "Welcome, please enter username:"

read $username

echo "WELCOME $( getent passwd "$USER" | cut -d: -f5 | cut -d, -f1), This is a basic calculator of square,circle and rectangle. "


echo "Please choose from 1,2 or 3 to continue: "

echo "1. Rectangle"
echo "2. Circle"
echo "3. Square"
echo "4. Quit"

read choice

if [ $choice -eq 1 ]
then
echo "You choose rectangle"
echo "Enter the height:"
read height
echo "Enter the width:"
read width
area=`echo "$height * $width"|bc`
echo "The area of the rectangle is:"$area

else
if [ $choice -eq 2 ]
then
echo "You choose circle"
echo "Enter the radius of the circle:"
read radius
area1=`echo "3.141 * $radius * $radius"|bc`
echo "The area of the circle is:"$area1

else
if [ $choice -eq 3 ]
then
echo "You choose square"
echo "Enter the width of the square:"
read w
area2=`echo "$w * $w"|bc`
echo "The area of the circle is:"$area2

else
If [ $choice -eq 4 ]
then "You choose quit"

"Quit")
break
;;
*) echo invalid option;;

如您所见,我不知道如何停止循环并“退出”。

谢谢!

最佳答案

#!/bin/bash

echo "Welcome, please enter username:"

read $username

echo "WELCOME $( getent passwd "$USER" | cut -d: -f5 | cut -d, -f1), This is a basic calculator of square,circle and rectangle. "

while true
do
echo "Please choose from 1,2 or 3 to continue: "

echo "1. Rectangle"
echo "2. Circle"
echo "3. Square"
echo "4. Quit"

read choice

if [ $choice -eq 1 ]
then
echo "You choose rectangle"
echo "Enter the height:"
read height
echo "Enter the width:"
read width
area=`echo "$height * $width"|bc`
echo "The area of the rectangle is:"$area

elif [ $choice -eq 2 ]
then
echo "You choose circle"
echo "Enter the radius of the circle:"
read radius
area1=`echo "3.141 * $radius * $radius"|bc`
echo "The area of the circle is:"$area1

elif [ $choice -eq 3 ]
then
echo "You choose square"
echo "Enter the width of the square:"
read w
area2=`echo "$w * $w"|bc`
echo "The area of the circle is:"$area2

elif [ $choice -eq 4 ]
then
break
else
echo "invalid option"
fi
done

我想你想这样做。这将无限循环,直到您按 4。

关于linux - Loop on Unix,几何图形的基本计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50200799/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com