How to assign a number to a variable?

Hello!

I’m beginner user of Fortran Language and I was trying to assign a number for an input, for example:

INTEGER material
REAL K, k1, k2, k3
        
WRITE (*,*) "Type the number for the desired material:"
WRITE (*,*) "1 - Aluminum"
WRITE (*,*) "2 - Steel"
WRITE (*,*) "3 - Cast Iron"
READ*, material

k1 = 10
k2 = 20
k3 = 30

If the people type 1 then the variable K will be k1 (that is 10) and so on.
Could somebody help me, please?

Thank you!

1 Like

Select case(material)
Case(1)…

There are several ways. Maybe what you want is an array.

integer :: a(3) = [10,20,30]
...
write(*,*) 'a(material}=', a(material)

How about

k = 10*material

?

Thank you, for all the answers, I tryied with array, but it doesn’t function :frowning: the I tried with Select Case and it worked! @mecej4 it would work, but the numbers I’ve used was just an examples.

The code:

         k1=10
         k2=20
         k3=30
         write (*,*) "Type the number for the desired material:"
         write (*,*) "1 - Aluminum"
         write (*,*) "2 - Steel"
         write (*,*) "3 - Cast Iron"
         read *, material
         select case (material)
            case (1)
               K=k1
            case (2)
               K=k2
            case (3)
               K=k3
         end select

         write (*,*) k

You could investigate including “case default” in the latest code example, which would provide you the opportunity to identify if material is not 1, 2 or 3.