简体   繁体   English

显示从最低到最高的数字系列

[英]Display series of numbers from lowest to highest

I am taking a BlueJ (terminal for c#) class in high school and I have a programming practice problem that I can't figure out. 我正在高中读一个BlueJ(c#终端)课,我有一个编程练习题,我无法弄清楚。

Write a program that has the computer randomly choose three numbers from the range 1 to 50. Have the computer produce the output as shown. 编写一个程序,让计算机随机选择1到50范围内的三个数字。让计算机产生如图所示的输出。

The first number chosen is 35
The second number chosen is 23
The third number chosen is 6

From lowest to highest: 6 23 35

The part I don't get is figuring out how display the variables in order from lowest to highest. 我没有得到的部分是弄清楚如何按从低到高的顺序显示变量。 I'm only supposed to use IF statements, no arrays or loops. 我只应该使用IF语句,没有数组或循环。 (We aren't far enough in the course to use those) (我们在课程中使用它们还不够远)

If you can only use if or else if , then use the fact that there are six possible ways that three numbers can be permuted (FIRST = equals first number entered, SECOND = second number, THIRD = third number): 如果您只能使用ifelse if ,那么使用以下事实:有三种可能的方式可以置换三个数字(FIRST =等于输入的第一个数字,SECOND =第二个数字,THIRD =第三个数字):

  • FIRST SECOND THIRD 第一第二第三
  • FIRST THIRD SECOND 第一个第二个
  • SECOND FIRST THIRD 第二个第一个
  • SECOND THIRD FIRST 第二个第一个
  • THIRD FIRST SECOND 第三个第二个
  • THIRD SECOND FIRST 第三个第一个

Because this is homework, I won't give you the code, but once you figure out the first line, it's trivial to do the other five. 因为这是作业,我不会给你代码,但是一旦你弄清楚了第一行,那么做其他五行是微不足道的。 So, start by writing an if statement to capture the FIRST SECOND THIRD situation. 因此,首先编写一个if语句来捕获FIRST第二个第三种情况。 (For example, if the user entered 5 10 15, in that order). (例如,如果用户按此顺序输入5 10 15)。 Hint: use <= (less than or equals operator). 提示:使用<= (小于或等于运算符)。

I would suggest grabbing a deck of cards it is great to think out problems like this and just talk your way through it. 我建议抓住一副牌,想出这样的问题并且只是通过它来说话就很棒。

If that doesn't fancy you and you just want an answer and not have to think about it, but this won't help you in the long run if you really want to learn. 如果这不是你想要的,你只是想要一个答案,而不必考虑它,但如果你真的想学习,这对你来说不会对你有所帮助。

http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap03/sort.html http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap03/sort.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM