【问题标题】:How do I display the largest number in an array in Ruby? [duplicate]如何在 Ruby 中显示数组中的最大数字? [复制]
【发布时间】:2018-09-20 09:58:06
【问题描述】:

我有一项家庭作业需要完成。我认为大部分代码都在工作,但我在最后一部分遇到了麻烦。我需要显示用户输入的最大数字(到数组中)。以下是我到目前为止的代码。我愿意接受任何建议。提前致谢。

这是作业的描述: 编写一个允许用户输入一系列 10 个整数并确定并打印最大整数的 Ruby 应用程序。您的程序应至少使用以下三个变量: a) 计数器:计数到 10 的计数器(即,跟踪输入了多少数字并确定何时处理了所有 10 个数字)。 b) number:用户最近输入的整数。 c) 最大:迄今为止发现的最大数字。

class Screen
  def cls
    puts ("\n")
    puts "\a"
  end
def pause
   STDIN.gets
 end
end

class Script
  def display_instructions 
      Console_Screen.cls
      print "This script will take the user input of 10 integers and then 
      will 
      print the largest."
      print "\n\nPress enter to continue."
      Console_Screen.cls
      Console_Screen.pause
   end

  def getNumber #accepts user input
    list = Array.new
    10.times do
      Console_Screen.cls
      print "This script accepts 10 integers."
      print "\n\nPlease type an integer and press enter."
      input = STDIN.gets
      input.chop!
      list.push(input)
     end
   end

  def display_largest(number) #displays the largest integer entered by the 
    user
    Console_Screen.cls
    print "The largest integer is " + 
  end

 def runScript
    number = getNumber
    Console_Screen.cls
    display_largest(number)
 end
end

 #Main Script Logic
 Console_Screen = Screen.new
 LargestNum = Script.new
 answer = "" 
 loop do
   Console_Screen.cls
   print "Are you ready to start the script? (y/n): "
   print "\n\nWould you like instructions on how this script works? (h): "
   answer = STDIN.gets
   answer.chop!
   break if answer =~ /y|n|h/i
 end

 if answer == "h" or answer == "H"
  LargestNum.display_instructions
  print "Are you ready to start the script? (y/n): "
  answer = STDIN.gets
  answer.chop!
  end

 if answer == "n" or answer == "N"
  Console_Screen.cls
  puts "Okay, maybe another time.\n\n"
  Console_Screen.pause
 else
  loop do
    LargestNum.runScript
    print "\n\nEnter Q to quit or press any key to run the script again: "
    runAgain = STDIN.gets
    runAgain.chop!
    break if runAgain =~ /Q/i
  end
end

【问题讨论】:

  • 我的意思是,即使没有内置函数,迭代和设置当前最大值似乎是一件相当简单的事情,不是吗?你有没有尝试过任何东西
  • a) 您不需要计数器,只需使用times。 b) 为什么需要最后一个数字输入? c) 使用max
  • “我认为大部分代码都在工作”不是一个足够精确的错误描述,我们无法帮助您。 什么不起作用? 如何不起作用?你的代码有什么问题?您收到错误消息吗?错误信息是什么?你得到的结果不是你期望的结果吗?你期望什么结果,为什么,你得到的结果是什么,两者有什么不同?您正在观察的行为不是期望的行为吗?期望的行为是什么,为什么,观察到的行为是什么,它们有何不同?
  • 您“认为”它正在工作是什么意思?它有效还是无效?另外,请确保您提供一个 minimal reproducible example 来证明您的问题。特别是,您的示例未通过 minimal 部分;我 100% 确定它不需要 70 行来证明你的问题,事实上,我愿意打赌它可以在 2 内完成。
  • 我为我的含糊道歉。我的意思是我相信代码正在接受输入并将其添加到数组中。我的问题是能够显示用户输入的最大数字。我一直在试图弄清楚如何基本上显示用户输入到脚本中的信息并将其输出为字符串以显示给用户。

标签: ruby


【解决方案1】:

这个问题已经被问过很多次了。我个人认为,正如this 回答所暗示的那样,内置的.max 是最好的解决方案。

[1, 3, 5].max #=> 5

【讨论】:

    【解决方案2】:

    您了解 for 循环了吗?您必须遍历数组。对于一个非常简单的示例,您可以执行类似

    的操作
    max = 0
    for element in list 
        if element > max
            max= element
    return max
    

    【讨论】:

      猜你喜欢
      • 2020-12-07
      • 2019-09-03
      • 2012-09-26
      • 2021-04-10
      • 1970-01-01
      • 1970-01-01
      • 2021-03-08
      • 1970-01-01
      • 2019-02-03
      相关资源
      最近更新 更多