[英]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
. 您应该给变量
j
和k
初始值-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
. 如果
x
或y
不在数组中,则循环后j
或k
仍为-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. 您必须将方法中的
k
和j
初始化为不是数组索引的某个索引,例如(-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.