Hi guys a quick question. Im creating a Java menu below is a sample piece of the code. I am wondering if i set up 2 arrays in an “if” statement how can i refer to these arrays again. I know that because of scope the arrays get destroyed when moved out of the if statement. I wanted to refer to the arrays again if option P is selected.
public static void determineOption (char option) {
if (option == 'A') {
int class_number = Console.getInt("Enter how many students between 1-10");
while (class_number < 1 || class_number > 10)
class_number = Console.getInt("Enter how many students between 1-10");
System.out.println("");
String[] name = new String[class_number];
int[] score = new int[name.length];
for (int i=0; i<class_number; i=i+1)
{
name[i] = Console.getString("Enter Name");
score[i] = Console.getInt("Enter score");
}
}
else if (option == 'P') {
}
According to your logic it seems HashMap is suitable for this.
Your key is name and value is score. You could define this as class variable and use in both if else if cases.