My 3D array in java is not printing out correctly...why? Here\'s the code: impor
ID: 3566290 • Letter: M
Question
My 3D array in java is not printing out correctly...why?
Here's the code:
import java.util.Scanner;
public class Array
{
public static void main( String[] args )
{
Scanner in = new Scanner(System.in);
System.out.println("Enter number of dimensions for array: 1, 2, or 3");
int x = in.nextInt();
int sum=0;
switch(x)
{
case 1:
System.out.println("How many integers do you want in the array: ");
int x1= in.nextInt();
int array1[] = new int[x1];
for (int i = 0; i < array1.length; i++)
{
System.out.println("Please enter element value " +(i+1)+ ":");
array1[i] = in.nextInt();
}
System.out.println( "Values in array1 by row are" );
for (int i=0; i {
System.out.printf( "%d ", array1[i] );
System.out.println();
}
for (int i=0; i {
sum+=array1[i];
}
System.out.printf("Sum of all elements entered: %d",sum);
System.out.print(" ");
break;
case 2:
System.out.println("How many rows do you want: ");
int row= in.nextInt();
System.out.println("How many columns do you want: ");
int column= in.nextInt();
int array2[][]= new int[row][column];
for ( row=0; row {
for (column=0; column {
System.out.println("Please enter element value on row " +(row+1)+ " in column " +(column+1)+ ":");
array2[row][column]=in.nextInt();
}
}
System.out.println( " Values in array2 by row are" );
for (row = 0; row < array2.length; row++ )
{
for ( column = 0; column < array2[ row ].length; column++ )
System.out.printf( "%d ", array2[ row ][ column ] );
System.out.println();
}
for ( row = 0; row < array2.length; row++ )
{
for (column = 0; column < array2[ row ].length; column++ )
{
sum+=array2[row][column];
}
}
System.out.printf("Sum of all elements entered: %d",sum);
System.out.print(" ");
break;
case 3:
System.out.println("How many rows do you want: ");
int r= in.nextInt();
System.out.println("How many columns do you want: ");
int c= in.nextInt();
System.out.println("How many layers do you want: ");
int layer= in.nextInt();
for (int l=0; l {
int array3[][]= new int[r][c];
for (r=0; r {
for (c=0; c {
System.out.println("Please enter element value on row " +(r+1)+ " in column " +(c+1)+ " on layer " +(l+1)+ ":");
array3[r][c]=in.nextInt();
}
}
System.out.println( " Values in array3 by row are" );
for (r = 0; r < array3.length; r++ )
{
for ( c = 0; c < array3[ r ].length; c++ )
{
System.out.print( array3[r][c]+" ");
System.out.println();
}
}
for ( r = 0; r < array3.length; r++ )
{
for (c = 0; c < array3[ r ].length; c++ )
{
int sum1 = 0;
sum1+=array3[ r ][ c ];
sum+=sum1;
}
}
}
System.out.printf("Sum of all elements entered: %d",sum);
System.out.print(" ");
break;
default:
System.out.println("Invalid input");
}
}
}
The 2D array prints correctly
(I.e. 2 rows, 3 columns, input 1 2 3 4 5 6
outputs
123
456 )
but my 3D array prints everything in a straingt line down, it doesn't print rows. How do I fix this?
And in each case statement I also need to print out the average of all elements in the array.
Explanation / Answer
number is declared in the tomato() method not in the sauce() method
you want to use array but by mistake you typed number
also you wrote of your tomato() and sauce() methods in the main() method
you have to define methods in the class but outside other methods
this is how it should look
class project
{
main()
{
....
}
tomato()
{
...
}
sauce()
{
....
}
}
Let me fix this for you
import java.util.Scanner;
public class Grades {
static int[] array=new int[10];
public static void main(String[] args) {
tomato();
sauce();
}
public static void tomato(){
Scanner reader = new Scanner(System.in);
int count = 0;
int i = 0;
while (count < array.length){
System.out.println("Enter the array numbers");
int number = reader.nextInt();
array [count] = number;
count++;}}
public static void sauce(){
for (int i = 0; i < array.length; i++)
if (array[i] <0)
System.out.println("Your negative numbers are: " + array[i]);}
}
edit: there where some typing mistakes, so i rewrote my program.. try it again
you cannot use variables of one method in the other method
variables in a method of local scope
you have to use static data members to use it in more than one functions
alternatively, you can pass it as a parameter to your method
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.