Ruby and Shell scripts to achieve a pass function for judging grades _ruby topics

Source: Internet
Author: User

Requirements for: input points from the keyboard, in order to judge, 0-59 for the failure, print "You did not pass, please try next time!", 60-79 for passing, print "Your grades pass, please work harder!" ", 80-100 for good results, print" Your results for the excellent, please make persistent efforts! "If you enter a score other than 0-100, please print" Your results for excellence, please make efforts! "

If you use Ruby, the code is simpler, as follows:

Copy Code code as follows:

#!/usr/bin/ruby
Print "Please enter your score:"
Grade = Gets.to_i
#grade = Name.to_i
Case Grade
When 0..60
Puts "You did not pass, please try next time!"
When 61..79
Puts "Your grades have passed, please work harder!"
When 80..100
Puts "Your achievements for the outstanding, please make efforts!"
Else
Puts "The number you entered is incorrect, please enter a value between 0-100!!"
End

In the shell, if you select a method with multiple branches, the code is:

Copy Code code as follows:

#!/bin/bash
Read-p "Please input your score:" Score
If [$Score-ge 0-a $Score-le];then
echo "You didn't pass, please try again next time!"
elif [$Score-ge 60-a $Score-le];then
echo "Your grades pass, please work harder!"
elif [$Score-ge 80-a $Score-le];then
echo "Your results are excellent, please make efforts!"
Else
echo "You have entered a score that exceeds the reasonable value, please re-enter!"
Fi

In the shell, if the method of using the case is more troublesome, the code is:

Copy Code code as follows:

#!/bin/bash
Read-p "Please enter your test score:" Score
Case "$score" in
[0-9])
echo "You didn't pass, please try again next time!"
;;
[1-5] [0-9])
echo "You didn't pass, please try again next time!"
;;
[6-7] [0-9])
echo "Your grades pass, please work harder!"
;;
[8-9] [0-9])
echo "Your results are excellent, please make efforts!"
;;
100)
echo "Your results are excellent, please make efforts!"
;;
*)
echo "You have entered a score that exceeds the reasonable value, please re-enter!"
;;
Esac

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.