idklife
idklife

Reputation: 11

About putting the if statement in the array of java

While I was writing this code to print out the specific values that are listed in the array, I want to make the below loop stop when the user types -1. I'm thinking of using an 'if' statement, but I'm not so sure where I can put that statement. I tried putting it inside the 'for' statement, however, it did not work and kept repeating the process. It would be helpful if there are instructions toward this situation.

Edit) So I tried to put the if statement right after asking the user to prompt the integer.The statement showed up that the user has successfully exited, although ,I don't want the conditions in the else statement to not be shown. What should I do?

import java.util.Scanner;

public class CalculateRents {

    public static void main(String[] args) {
    
    int[][]numberArray = new int [4][3];
    
    numberArray[0][0] =750;
    numberArray[0][1] =900;
    numberArray[0][2] =1200;

    numberArray[1][0] =850;
    numberArray[1][1] =1000;
    numberArray[1][2] =1300;

    numberArray[2][0] =950;
    numberArray[2][1] =1100;
    numberArray[2][2] =1400;

    numberArray[3][0] =1050;
    numberArray[3][1] =1300;
    numberArray[3][2] =1500;
    
    Scanner scan = new Scanner(System.in);
    for (int num1 =0; num1<=3; num1++) {
        for (int num2 = 0; num2<=2; num2++) {
            System.out.print("Enter the floor (select from 1 to 4) : ");
            int floor = scan.nextInt();
            if (floor==-1) {
                    System.out.println("You had succesfully exited!");
                } else {
            System.out.print("Enter the number of the bedroom (select from 0 to 2) : ");
            int bedroom =  scan.nextInt();
            
            System.out.println("The rent per month is (floor : " + floor + "  bedroom : " + bedroom  + ") " + "$" + numberArray[floor-1][bedroom] + ".");
            System.out.println("\n" + "Select another floor or -1 to exit.");
                }
        
    }

}

}

Upvotes: 1

Views: 62

Answers (3)

Som
Som

Reputation: 1610

Full Tested Code :

    import java.util.Arrays;
    import java.util.*; 
    import java.lang.*; 
    
    public class CalculateRents {
        public static void main(String[] args) {
            int[][]numberArray = new int [4][3];
        
        numberArray[0][0] =750;
        numberArray[0][1] =900;
        numberArray[0][2] =1200;
    
        numberArray[1][0] =850;
        numberArray[1][1] =1000;
        numberArray[1][2] =1300;
    
        numberArray[2][0] =950;
        numberArray[2][1] =1100;
        numberArray[2][2] =1400;
    
        numberArray[3][0] =1050;
        numberArray[3][1] =1300;
        numberArray[3][2] =1500;
        
        Scanner scan = new Scanner(System.in);
        for (int num1 =0; num1<=3; num1++) {
            for (int num2 = 0; num2<=2; num2++) {
                System.out.print("Enter the floor (select from 1 to 4) : ");
                int floor = scan.nextInt();
                if(floor == -1){
                    System.out.println("Choice is -1. so exiting.....");
                    System.exit(0);
                } else {
                    System.out.print("Enter the number of the bedroom (select from 0 to 2) : ");
                    int bedroom =  scan.nextInt();                
                    System.out.println("The rent per month is (floor : " + floor + "  bedroom : " + bedroom  + ") " + "$" + numberArray[floor-1][bedroom] + ".");
                }
                System.out.println("\n" + "Select another floor or -1 to exit.");
            
            }
        
        }
    }
}

Sample Output :

Enter the number of the bedroom (select from 0 to 2) : 2                                                                                                     
The rent per month is (floor : 2  bedroom : 2) $1300.                                                                                                        
                                                                                                                                                             
Select another floor or -1 to exit.                                                                                                                          
Enter the floor (select from 1 to 4) : -1                                                                                                                    
Choice is -1. so exiting..... 

            

Upvotes: 0

NeilArmstrong9000
NeilArmstrong9000

Reputation: 50

did you consider using break to exit/break the loop? If you use break in a loop the loop will end. In your case it would be possible to use an if-statement to check if user typed in -1 and then exit the loop with break-statement. I gave you an example below. As you probably want to exit the outer loop you have to add a label to the outer loop (here I added "OuterLoop:") and add the name of the label to your break-statement (here break OuterLoop;). Without the label and the just break-statement you would only exit the inner loop. The outer loop would continue.

OuterLoop:    // Label for the outer loop
        for (int num1 =0; num1<=3; num1++) {
            for (int num2 = 0; num2<=2; num2++) {
                System.out.print("Enter the floor (select from 1 to 4) : ");
                int floor = scan.nextInt();

                if (floor == -1) break OuterLoop; // break the outer loop

                System.out.print("Enter the number of the bedroom (select from 0 to 2) : ");
                int bedroom =  scan.nextInt();

                System.out.println("The rent per month is (floor : " + floor + "  bedroom : " + bedroom  + ") " + "$" + numberArray[floor-1][bedroom] + ".");
                System.out.println("\n" + "Select another floor or -1 to exit.");

            }
        }

Upvotes: 1

Ritter7
Ritter7

Reputation: 166

Just put the inner for loop like this:

for (int num2 = 0; num2<=2; num2++) {
        System.out.print("Enter the floor (select from 1 to 4) : ");
        int floor = scan.nextInt();
        
        if(floor == -1){
        System.exit(0);
        } 

        System.out.print("Enter the number of the bedroom (select from 0 to 2) : ");
        int bedroom =  scan.nextInt();
        
        System.out.println("The rent per month is (floor : " + floor + "  bedroom : " + bedroom  + ") " + "$" + numberArray[floor-1][bedroom] + ".");
        System.out.println("\n" + "Select another floor or -1 to exit.");
        
    }

Upvotes: 0

Related Questions