简体   繁体   中英

How can I get the following code so that it debugs without error?

This code that I've written is coming up with errors that I don't understand how to fix. Here's what I want it to do: Create 3 cases in which the user is allowed to choose between case 1 or 2(any other case will result in the default case)

Case1: Take input from the user (in this case, a double input named zmultiplier), take that double, multiply it by each element in array z, and display the resulting array(named result).

Case2: I want the program to go through each array element, and print each array element that is evenly divisible by 2 or 5.

Default: Print "array z is not changed"

Here's what I've written:

package jm;

import java.util.Scanner;

public class Jm {

    public static void main(String[] args) {

Scanner input = new Scanner (System.in);

    int [][] z = {{8,15,22,28,36,40},{43,45,48,59,66,70}};  
    int Case;
    double zmultiplier;
    int arrayelement;
    int row=0;
    int column=0;

    System.out.println("Enter case: ");

    Case = input.nextInt(); 
    switch(Case)
    {
     case 1: 
            System.out.println("Enter number to multiply array z by: ");
    zmultiplier = input.nextDouble(); 
    double [][] result = new double [row][column];
    for (;row < z.length; row++ )
    { for (;column < z[row].length; column++ )
     result[row][column] = result[row][column] * zmultiplier;
        System.out.printf( "%d ", result[row][column]);
    }

            break;

        case 2:
           for ( int row = 0; row < array.length; row++);
            {
            for (int column = 0; column < array[row].length; column++);

                if (arrayelement%2 == 0) || (arrayelement%5 == 0);
                System.out.println(arrayelement); 
                     break;

                default: 
                     System.out.println("array z is not changed");
            break;
    }
}
}

If anyone can solve this for me and explain in detail(and Layman's terms) what I did wrong, I'd greatly appreciate it.

For a start both of these look wrong:

for (int column = 0; column < array[row].length; column++);

if (arrayelement%2 == 0) || (arrayelement%5 == 0);

Here you're using a ; as the entire body of both the if statement and the for loop. It's equivalent to:

for (int column = 0; column < array[row].length; column++) {
}

if (arrayelement%2 == 0) || (arrayelement%5 == 0) {
}

I suspect that's not actually what you meant. I strongly suggest that you always use braces for loops and if statements, even if you only have a single statement within those braces - it helps to keep things clear.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM