简体   繁体   English

如何创建使用循环格式化数组输出的方法

[英]How to create a method that formats output of an array with a loop

I am creating a class that calls methods on an array of unknown size and unknown values. 我正在创建一个类,该类在未知大小和未知值的数组上调用方法。 I have the entire thing made except the method that formats the output. 除了格式化输出的方法之外,我已完成全部工作。 The class is about locker rentals. 这堂课是关于储物柜租金的。 If the first locker has been rented for two days, then this would be true: 如果第一个储物柜已经租用了两天,那么这将是正确的:

    daysLeftRental[0] == 2;

If the locker is available to rent, then this would be true: 如果可以租用更衣室,那将是对的:

    daysLeftRental[0] == -1;

In other words, any lockers that are available hold a value of -1, since there is no way someone could rent a locker for -1 days. 换句话说,任何可用的储物柜的值为-1,因为有人无法租用-1天的储物柜。 I need to format my output in the following manner: 我需要按以下方式格式化输出:

    Locker 0: 3
    Locker 1: Available
    Locker 2: 30

And so on and so forth. 等等等等。 I have no idea how I would format my code so that when 我不知道如何格式化我的代码,以便何时

    i = position of locker;
    daysLeftRental[i] == -1;

my output says "Available" and when 我的输出显示“可用”,何时

    daysLeftRental[i] >= 0

my output says whatever value is stored in daysLeftRental[i]. 我的输出说什么值都存储在daysLeftRental [i]中。 For reference, this is what my code would look like if I could just return -1 instead of the string "available" 供参考,如果我只返回-1而不是字符串“ available”,这就是我的代码的样子

    public String toString() {
        String formatOutput = " ";
        for (int i = 0; i < daysLeftRental.length; i++) {
             formatOutput = formatOutput + "\nLocker" 
             + i + ": " + daysLeftRental[i];
        }
        return formatOutput;
    }

which would yield: 这将产生:

    Locker 0: 3
    Locker 1: -1
    Locker 2: 30

Is this the one you are trying? 这是您要尝试的吗?

public String toString() {
        String formatOutput = " ";
        for (int i = 0; i < daysLeftRental.length; i++) {
             formatOutput = formatOutput + "\nLocker " 
             + i + ": " + (daysLeftRental[i] == -1 ? "Available" : daysLeftRental[i].toString());
        }
        return formatOutput;
    }

you can do it like this: 您可以这样做:

public String toString() {
    String formatOutput = " ";
    for (int i = 0; i < daysLeftRental.length; i++) {
         if(daysLeftRental[i] >= 0){
             formatOutput = formatOutput + "\nLocker" 
             + i + ": " + daysLeftRental[i];
         }else{
             formatOutput = formatOutput + "\nLocker" 
             + i + ": Available";        
         }
    }
    return formatOutput;
}

or, you can prepare the line before the if-statement, then just append last segmant: 或者,您可以在if语句之前准备一行,然后只需附加最后一个段:

public String toString() {
    String formatOutput = " ";
    for (int i = 0; i < daysLeftRental.length; i++) {
         formatOutput = formatOutput + "\nLocker" + i + ": ";
         if(daysLeftRental[i] >= 0){
              formatOutput += Integer.toString(daysLeftRental[i]);
         }else{
             formatOutput += "Available";        
         }
    }
    return formatOutput;
}

if you don't like to write same line in both if and else, you can use a variable to hold the number or the word "Available" 如果您不希望在if和else中都写同一行,则可以使用变量来保存数字或单词“ Available”

public String toString() {
    String formatOutput = " ";
    String status = "";

    for (int i = 0; i < daysLeftRental.length; i++) {
         if(daysLeftRental[i] >= 0){
             status = Integer.toString(daysLeftRental[i]);
         }else{
             status = "Available";
         }

         //concatenate it:
         formatOutput = formatOutput + "\nLocker" + i + status;
    }
    return formatOutput;
}

and you can use inline-if as in Aruna's answer above (if you understand it) if not, i would recommend (option-2) , no much code repeated (option-1), no much variables declared (option-3). 并且您可以使用inline-if,如上面的Aruna的答案(如果您理解),否则, 我建议(option-2) ,不要重复太多代码(option-1),也不要声明太多变量(option-3)。

EDIT: - -- -- -- -- 编辑:-----

i don't want to exaggerate, but you can always use a method, if you know how to use them create a method that will return either the number or "Available"., the IF-statement goes to the method, and that's it 我不想夸大其词,但是您始终可以使用一种方法,如果您知道如何使用它们,则可以创建一个将返回数字或“可用”的方法。IF语句就可以使用该方法,仅此而已

public String getStatus(int x){

    if(x>=0){
        return Integer.toString(x);
    }else{
        return "Available";
    }
}

in the for loop, don't use IF-statement at all. 在for循环中,根本不要使用IF语句。 just concatenate the result of getStatus() call to the line 只需将getStatus()调用的结果连接到该行

formatOutput = formatOutput + "\nLocker" + i + ": " + getStatus(daysLeftRental[i]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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