arrays - Check if a character exists in an array in fortran
问题描述
So, I want to write some code so that the user cannot type in a city that is not in the city list, the city_list array is a one dimensional array of size 5 which values are A,B,C,D and E. So what I did was this
character, dimension(5) :: city_list
do i=1,5
city_list(i)= achar(i+64) !To give the array a,b,c values to its first five components
end do
character :: City1, ...
do while(ANY(City_list == City1) )
READ*, City1
PRINT*, "IT'S GOT TO BE A,B,C,D,E"
end do
So I used the ANY function that I saw in another post, but I can' get to use it well, it doesn't seem to do what I asked it to do
解决方案
另一种方法可能是使用带条件的无限循环exit
(使代码更简单......)
program main
implicit none
character :: city_list( 5 ), city1
city_list = ["A", "B", "C", "D", "E"]
do
print*, "Introduce a city that is on the list"
read *, city1
if ( any( city1 == city_list ) ) exit
enddo
print *, "Your city1 is ", city1
end
推荐阅读
- function - 八度 - fskipl:无效的流号
- tensorflow - 形状必须为 2 级,但 NonMaxSuppressionV3 为 3 级:错误
- vb.net - 如何使用 vbnet 代码清理和构建项目
- python - 无法同时使用 pyocr 和 pytesseract 打印从图像中提取的字符串
- slider - 使用 slick.js 的缩略图滑块,位置失真
- local-storage - Windows 10下的电脑上Microsoft Edge在哪里保存localsorage?
- r - 如何将 TIFF 文件转换为 R 中的二进制图像?
- python - 如何通过对非标量值求和来将不同类型的字典转换为熊猫系列?
- ssl - LDAP over SSL 636,在本地工作,但不在外部工作
- rest - Pod 之间的 GKE 延迟不佳