배열, 1~100을 입력받는다 입력 : 5개 힌트gets.chomp
1.합을구하고
2.작은값, 큰값 min max
to_i : int형으로 변환
#입력값이 0이하이거나 100이상인지 검사
def val(temp)
if temp < 0
return "shortage" #부족
elsif temp > 100
return "excess" #초과
else
return "success" #통과
end
end
# 총합
def add(arr)
a = 0
b = 0
while a <= arr.length
b = b + arr[a].to_i
a = a + 1
end
puts "합계 : #{b}"
end
arr = []
i = 0
while i< 5
temp = gets.chomp.to_i
result = val(temp)
if result == "shortage" || result == "excess"
puts "다시입력"
break
else
arr[i] = temp
i = i + 1
end
end
puts #{\n}
puts "큰값 : #{arr.max}"
puts #{\n}
puts "작은값 : #{arr.min}"
puts #{\n}
add(arr)
대리님 수정
#입력값이 0이하이거나 100이상인지 검사
def val(temp)
def val(temp)
if temp <= 0
return "shortage" #부족
elsif temp > 100
return "excess" #초과
else
return "success" #통과
end
end
return "shortage" #부족
elsif temp > 100
return "excess" #초과
else
return "success" #통과
end
end
# 총합
def add(arr)
a = 0
b = 0
while a <= arr.length
b = b + arr[a].to_i
a = a + 1
end
puts "합계 : #{b}"
end
arr = []
i = 0
while(true)
i = i + 1
temp = gets.chomp.to_i #입력
result = val(temp)
i = i + 1
temp = gets.chomp.to_i #입력
result = val(temp)
if result == "shortage" || result == "excess"
i -= 1
else
arr << temp
end
if arr.size == 5
break
end
end
i -= 1
else
arr << temp
end
if arr.size == 5
break
end
end
puts #{\n}
puts "큰값 : #{arr.max}"
puts #{\n}
puts "작은값 : #{arr.min}"
puts #{\n}
add(arr)
puts "큰값 : #{arr.max}"
puts #{\n}
puts "작은값 : #{arr.min}"
puts #{\n}
add(arr)
'Ruby on Rails' 카테고리의 다른 글
Ruby on Rails -Tip .. 등등 (0) | 2011.01.19 |
---|---|
Ruby on Rails 한빛미디어 브루스테이트, 힙스 저 (0) | 2011.01.12 |
Rails설치 및 Application생성 (0) | 2011.01.11 |