简体   繁体   English

如果数组中不包含字符串,如何返回特定值

[英]How do I return a specific value if a string is not contained in an array

I've written a program in which there are two arrays. 我写了一个程序,其中有两个数组。 One is an array of strings which contains the names of cities, and another contains the distances between each one. 一个是包含城市名称的字符串数组,另一个包含每个城市之间的距离。 A method is run which returns the total distance between the two cities in the method call. 运行一个方法,该方法返回方法调用中两个城市之间的总距离。 However, if one or both of the cities are not contained in the array, I want the program to return the value -1, but I can't figure out how to do it. 但是,如果数组中未包含一个或两个城市,则我希望程序返回值-1,但我不知道该怎么做。 This is for a school assignment, and we have only covered up to creating our own methods, so I have to actually work this through using loops and if statements, and can't just write in a command to immediately check if the value is in the array. 这是用于学校作业,我们只涉及创建自己的方法,因此我必须通过使用循环和if语句实际进行此操作,并且不能只编写命令立即检查值是否在数组。

import java.util.Scanner;
public class Q3{
   public static void main(String[] args){
      Scanner kbd = new Scanner(System.in);
      String[] towns = {"Halifax", "Enfield", "Elmsdale", "Truro", "Springfield", "Sackville", "Moncton"};
      int[] distances = {25, 5, 75, 40, 145, 55, 0};
      System.out.println("The distance between the two towns is " + distance(towns, distances, "Truro", "Moncton"));
   }
   public static int distance(String[] towns, int[] distances, String x, String y){
      int totdistance=0;//define total distance
      int j = 0;
      int k = 0;
      for(int i=0;i<towns.length;i++){//define variable j
         if (x.equals(towns[i])){
            j=i;
         }
      }
      for(int i=0;i<towns.length;i++){//define variable k
         if (y.equals(towns[i])){
            k=i;
         }
      }
      if (!y.equals(towns[k])){
         totdistance=-1;
      }
      if (j<k&&j!=-1){//if j less than k calculate distance
        for(;j<k;j++){
               totdistance+=distances[j];
         }
      }
      else if (k<j&&k!=-1){//if k less than j calculate distance
         for(;k<j;k++){
            totdistance+=distances[k];
         }
      }
      return totdistance;
   }
}

You should give the variables j and k the initial value -1 . 您应该给变量jk初始值-1 If x or y is not in the array, j or k will still be -1 after the loop, so you can return -1 . 如果xy不在数组中,则循环后jk仍为-1 ,因此可以返回-1

int j = -1;
int k = -1;
for(int i=0;i<towns.length;i++){//define variable j
   if (x.equals(towns[i])){
      j=i;
   }
}
for(int i=0;i<towns.length;i++){//define variable k
   if (y.equals(towns[i])){
      k=i;
   }
}
if (j == -1 || k == -1)
    return -1;

You have to initialize k and j in your method to some index that is not an array index eg (-1) Then check whether any of them are equal to their initial value after the first two loops. 您必须将方法中的kj初始化为不是数组索引的某个索引,例如(-1),然后在前两个循环之后检查它们中的任何一个是否等于它们的初始值。 Here is a modified code that will return -1 if one or both of the cities are not found in the array: 这是修改后的代码,如果在数组中未找到一个或两个城市,则将返回-1:

import java.util.Scanner;
public class Q3{
   public static void main(String[] args){
      Scanner kbd = new Scanner(System.in);
      String[] towns = {"Halifax", "Enfield", "Elmsdale", "Truro", "Springfield", "Sackville", "Moncton"};
      int[] distances = {25, 5, 75, 40, 145, 55, 0};
      System.out.println("The distance between the two towns is " + distance(towns, distances, "Truro1", "Moncton1"));
   }
   public static int distance(String[] towns, int[] distances, String x, String y){
      int totdistance=0;//define total distance
      int j = -1;
      int k = -1;
      for(int i=0;i<towns.length;i++){//define variable j
         if (x.equals(towns[i])){
            j=i;
         }
      }
      for(int i=0;i<towns.length;i++){//define variable k
         if (y.equals(towns[i])){
            k=i;
         }
      }

      if(k==-1||j==-1){
          return -1;
      }

      if (!y.equals(towns[k])){
         totdistance=-1;
      }
      if (j<k&&j!=-1){//if j less than k calculate distance
        for(;j<k;j++){
               totdistance+=distances[j];
         }
      }
      else if (k<j&&k!=-1){//if k less than j calculate distance
         for(;k<j;k++){
            totdistance+=distances[k];
         }
      }
      return totdistance;
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果数组中的值不包含在另一个数组中,如何返回 false? - How do I return false if a value in an array isn't contained in another array? 如何将字符串数组和字符串返回到多重拼写数组? - How do I return string array and string to the array of multispell? 我如何从多维数组中搜索然后返回特定值? - How do I search for and then return a specific value from a multi-dim array? 如何将字符串变量设置为另一个变量中包含的字符串 - How do I set a string variable to a string contained in another variable 如何返回TreeMap中包含的值的TreeSet? - How do I return a TreeSet of the values contained in a TreeMap? 如何通过在Java中将字符串作为引用传递给数组来返回数组? - How do I return an array by passing a string as reference to the array in Java? 如何检查字符串中的特定字符并返回它们的数量? - How do I check for specific characters in a string and return the amount of them? 如何测试字符串数组以查找特定字符串是否在数组中? - How do I test an array of strings to find if a specific string is in the array? 如何确保一个数组中包含一个值? - How can I ensure a value is contained within an array? 在Struts中,如何给href的键中包含的值 - In Struts, how do I give a value contained in key to a href
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM