I work in a small task that allows the user to enter the regions of any country and store them in one array. In addition, each time he enters a region, the system will ask him to enter the neighbors of this entered region and save these regions.
My idea is that every time a user enters a region, the system will store him in arrya, and every time he enters a region, the system will ask him to enter the neighbors of this region before he enters the second region, and therefore they store these inputs are in the second array under the pointer of the corresponding entry in the first array.
what i did is just the beginning:
import java.util.Arrays;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
String [] regionArray = new String[5];
for (int i = 0; i < regionArray.length; i++) {
System.out.print("Please enter the region: ");
regionArray[i] = kb.next();
}
System.out.print("You have entered: ");
System.out.println(Arrays.toString(regionArray));
}
}
, , 4 : a, b, c, d a : b d. , , (a), , b d, , b ..
, ?
,
. .
Hashmap, , . , separatley, . , ?
:
import java.util.*;
import java.util.HashMap;
public class Test5{
public static void main(String[]args){
HashMap<String, String> neighbour = new HashMap<String, String>();
Scanner kb = new Scanner(System.in);
for(;{
System.out.println ("Enter the neighbours first then the region... and when finished press q");
String n = kb.nextLine();
if (n.equalsIgnoreCase("Q"))
break;
System.out.print("region: ");
String region = kb.nextLine();
neighbour.put(region, n);
}
System.out.println(neighbour);
}
}