Jump to content

Problem with a Small Basic program

NeatSquidYT

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
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 comment
Share on other sites

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 comment
Share on other sites

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 comment
Share on other sites

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 comment
Share on other sites

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 comment
Share on other sites

Link to post
Share on other sites

  • 2 weeks later...


If num1 > Num2 AND Num1 > Num3 Then

Largest="Number 1" ORELSE _

Num2>Num1 AND NUm2> Num3 Then

Largest="Number 2" ORELSE _

Num3>Num1 AND Num3>Num2 Then

Largest="Number 3" Else_

Largest="invaid"

End IF

Link to comment
Share on other sites

Link to post
Share on other sites

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

×