关于将if语句放入Java数组中

huangapple go评论56阅读模式
英文:

About putting the if statement in the array of java

问题

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("\nSelect another floor or -1 to exit.");
                }

            }

        }

    }
}
英文:

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&lt;=3; num1++) {
for (int num2 = 0; num2&lt;=2; num2++) {
System.out.print(&quot;Enter the floor (select from 1 to 4) : &quot;);
int floor = scan.nextInt();
if (floor==-1) {
System.out.println(&quot;You had succesfully exited!&quot;);
} else {
System.out.print(&quot;Enter the number of the bedroom (select from 0 to 2) : &quot;);
int bedroom =  scan.nextInt();
System.out.println(&quot;The rent per month is (floor : &quot; + floor + &quot;  bedroom : &quot; + bedroom  + &quot;) &quot; + &quot;$&quot; + numberArray[floor-1][bedroom] + &quot;.&quot;);
System.out.println(&quot;\n&quot; + &quot;Select another floor or -1 to exit.&quot;);
}
}
}
}

答案1

得分: 1

你考虑过使用break语句来退出/中断循环吗?如果在循环中使用break,循环将会结束。在你的情况下,可以使用一个if语句来检查用户是否输入了-1,然后使用break语句退出循环。我给你提供了下面的示例。由于你可能想要退出外部循环,你需要在外部循环上添加一个标签(这里我添加了“OuterLoop:”),并将标签名称添加到你的break语句中(这里是break OuterLoop;)。如果没有标签,只有break语句,你将只会退出内部循环,外部循环会继续执行。

OuterLoop:    // 外部循环的标签
    for (int num1 = 0; num1 <= 3; num1++) {
        for (int num2 = 0; num2 <= 2; num2++) {
            System.out.print("输入楼层(从1到4选择):");
            int floor = scan.nextInt();

            if (floor == -1) break OuterLoop; // 中断外部循环

            System.out.print("输入卧室号码(从0到2选择):");
            int bedroom =  scan.nextInt();

            System.out.println("每月租金为(楼层:" + floor + "  卧室号码:" + bedroom  + ") $" + numberArray[floor-1][bedroom] + "。");
            System.out.println("\n" + "选择另一个楼层或输入-1退出。");
        }
    }
英文:

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&lt;=3; num1++) {
for (int num2 = 0; num2&lt;=2; num2++) {
System.out.print(&quot;Enter the floor (select from 1 to 4) : &quot;);
int floor = scan.nextInt();
if (floor == -1) break OuterLoop; // break the outer loop
System.out.print(&quot;Enter the number of the bedroom (select from 0 to 2) : &quot;);
int bedroom =  scan.nextInt();
System.out.println(&quot;The rent per month is (floor : &quot; + floor + &quot;  bedroom : &quot; + bedroom  + &quot;) &quot; + &quot;$&quot; + numberArray[floor-1][bedroom] + &quot;.&quot;);
System.out.println(&quot;\n&quot; + &quot;Select another floor or -1 to exit.&quot;);
}
}

答案2

得分: 0

只需将内部的for循环放置如下:

for (int num2 = 0; num2 <= 2; num2++) {
    System.out.print("输入楼层(选择从1到4):");
    int floor = scan.nextInt();

    if (floor == -1) {
        System.exit(0);
    }

    System.out.print("输入卧室数量(选择从0到2):");
    int bedroom = scan.nextInt();

    System.out.println("每月租金是(楼层:" + floor + " 卧室:" + bedroom + ") $" + numberArray[floor-1][bedroom] + "。");
    System.out.println("\n选择另一个楼层或输入-1退出。");
}
英文:

Just put the inner for loop like this:

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

答案3

得分: 0

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.....
英文:

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&lt;=3; num1++) {
for (int num2 = 0; num2&lt;=2; num2++) {
System.out.print(&quot;Enter the floor (select from 1 to 4) : &quot;);
int floor = scan.nextInt();
if(floor == -1){
System.out.println(&quot;Choice is -1. so exiting.....&quot;);
System.exit(0);
} else {
System.out.print(&quot;Enter the number of the bedroom (select from 0 to 2) : &quot;);
int bedroom =  scan.nextInt();                
System.out.println(&quot;The rent per month is (floor : &quot; + floor + &quot;  bedroom : &quot; + bedroom  + &quot;) &quot; + &quot;$&quot; + numberArray[floor-1][bedroom] + &quot;.&quot;);
}
System.out.println(&quot;\n&quot; + &quot;Select another floor or -1 to exit.&quot;);
}
}
}
}

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..... 

huangapple
  • 本文由 发表于 2020年7月24日 01:01:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63059497.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定