I have to write a program that will fill up the array with random numbers between 1 and 100. The user will input the size of the array. Fill the array up. Then the user will input the number they are looking for and the program will return where that number is located in the array, if it is not in the array, return the -1. Continue doing this until the use enters -1.
This is what I have so far:
Scanner kb = new Scanner (System.in);
System.out.println("Please enter row size");
int row = kb.nextInt();
System.out.println("Please enter column size");
int column = kb.nextInt();
int [][] Array = new int [row][column];
System.out.println();
for ( int r = 0; r < Array.length; r++)
{
for (int c = 0; c < Array[r].length ; c++)
{
Array [r][c] = (int) (Math.random()*99+1);
System.out.print(Array[r][c]+ " ");
}
System.out.println();
System.out.println();
}
System.out.println ("Please input a number to look for or enter \"-1\" to stop ");
int find = kb.nextInt();
findValue (Array, find);
}
public static int findValue (int [][] Array, int find)
{
if (find !=-1)
{
for ( int x = 0; x < Array.length; x++)
{
for (int y = 0; y < Array[x].length ; y++)
{
if (Array[x][y] == find)
{
System.out.println("The number is located in the "+ (x+1) + " row, and the " + (y+1) + " column");
}
}
}
}
}
}
}
However, I can't figure out how to return "-1" if the number that the user asked for is not in the Array.. Also, I do not know how to keep asking "Please input a number..." and stop when user inputs "-1". Thank you so much for help
Aucun commentaire:
Enregistrer un commentaire