Jump to content

Hi, I was doing my homework on "Comparing Three Numbers". For some reason, it ALWAYS picks number 3 for some reason, for both smallest and largest, and wondering why it did that.

TextWindow.WriteLine("Please enter your first number")num1 = TextWindow.ReadNumber()TextWindow.WriteLine("Please enter your second number")num2 = TextWindow.ReadNumber()TextWindow.WriteLine("Please enter your third number")num3 = TextWindow.ReadNumber()If num1 > num2 And num3 Then  largest = "Number 1"ElseIf num2 > num1 And num3 Then  largest = "Number 2"Else  largest = "Number 3"EndIfIf num1 < num2 And num3 Then  smallest = "Number 1"ElseIf num2 < num1 And num3 Then  smallest = "Number 2"Else  smallest = "Number 3"EndIf TextWindow.WriteLine("The smallest number was: "+smallest)TextWindow.WriteLine("The largest number was: "+largest)

Help is appreciated

Link to comment
https://linustechtips.com/topic/223197-problem-with-a-small-basic-program/
Share on other sites

Link to post
Share on other sites

Maybe try adding:

"ElseIf num3 > num1 And num2 Then"

Asrock 890GX Extreme 3 - AMD Phenom II X4 955 @3.50GHz - Arctic Cooling Freezer XTREME Rev.2 - 4GB Kingston HyperX - AMD Radeon HD7850 - Kingston V300 240GB - Samsung Spinpoint F3 1TB - Chieftec APS-750 - Cooler Master HAF912 PLUS


osu! profile

Link to post
Share on other sites

i would do something like this

varibles

 

num1, num2, num3, highest, lowest as integer

 

num1 = number input

 

set highest and lowest to num1

 

(otherwise lowest number will come out as 0 as it will default to 0)

 

 

num2 = user input

 

if num2 is < lowest then

lowest = num2

 

if num2 is > highest then

highest = num2

 

num3 = user input

 

if num3 is < lowest then

lowest = num3

 

if num3 is > highest then

highest = num3

 

print out highest and lowest number

 

You can do this with less code, it's what i would start with.

 

What language you coding in?

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites

i would do something like this

varibles

 

num1, num2, num3, highest, lowest as integer

 

num1 = number input

 

set highest and lowest to num1

 

(otherwise lowest number will come out as 0 as it will default to 0)

 

 

num2 = user input

 

if num2 is < lowest then

lowest = num2

 

if num2 is > highest then

highest = num2

 

num3 = user input

 

if num3 is < lowest then

lowest = num3

 

if num3 is > highest then

highest = num3

 

print out highest and lowest number

 

You can do this with less code, it's what i would start with.

 

What language you coding in?

 

 

What language is it?

Small Basic

Link to post
Share on other sites

 

You cannot say "num1 < num2 and num3" you have to say "num1 < num2 and num1 < num3"

If num1 > num2 And num1 > num3 Then  largest = "Number 1"ElseIf num2 > num1 And num2 > num3 Then  largest = "Number 2"Else  largest = "Number 3"EndIf

I would agree with this. The structure would be:

If (Condition is true) And (Condition is true) Then<Do Something>

I've never used small basic but I can only assume that it doesn't return true if a number is more than 0 when used as a boolean.

Link to post
Share on other sites

I would agree with this. The structure would be:

If (Condition is true) And (Condition is true) Then<Do Something>

I've never used small basic but I can only assume that it doesn't return true if a number is more than 0 when used as a boolean.

 

 

 

You cannot say "num1 < num2 and num3" you have to say "num1 < num2 and num1 < num3"

If num1 > num2 And num1 > num3 Then  largest = "Number 1"ElseIf num2 > num1 And num2 > num3 Then  largest = "Number 2"Else  largest = "Number 3"EndIf

I agree with these two :D By saying

If num1 < num2 And num3 Then

it's counting num3 as it's own condition. 

Link to post
Share on other sites

  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×