用Fortran,从键盘输入实型变量a, b,如果变量a小于变量b,交换a,b的值。打印输出a,b的值。用case结构
时间: 2024-02-20 11:58:20 浏览: 156
下面是用Fortran语言编写的程序,该程序从键盘输入实型变量a和b,如果a小于b,就交换它们的值,最后打印输出a和b的值。使用了case结构。
```
program swap_numbers
implicit none
real :: a, b, temp
integer :: choice
print *, "请输入实数a和b的值:"
read *, a, b
! 判断a是否小于b
if (a < b) then
! 交换a和b的值
temp = a
a = b
b = temp
endif
print *, "请选择您想要输出的值:"
print *, "1. 输出a和b的值"
print *, "2. 输出b和a的值"
read *, choice
! 使用case结构输出a和b的值
select case (choice)
case (1)
print *, "a = ", a
print *, "b = ", b
case (2)
print *, "b = ", b
print *, "a = ", a
case default
print *, "无效的选择!"
end select
end program swap_numbers
```
注意,该程序中使用了if-endif语句判断a是否小于b,并使用了temp变量来交换a和b的值。使用select case语句输出a和b的值,根据用户的选择输出不同的结果。
阅读全文
相关推荐

















