[英]How can I use what is returned to me in a method within a println?
Snippet of my code: 我的代码段:
...
System.out.println("\t" + "MONTH |" + "\t" + "HIGH | " + "\t" + "LOW |" + "\t" + "AVERAGE |" + "\t" + "RANGE");
System.out.println("\t" + "______________________________________________");
main.averageMonthOne(hightemp, lowtemp);
in1.close();
out.close();
}//end of main
private static double averageMonthOne (int hightemp, int lowtemp)
{
double avgM = (hightemp + lowtemp)/2;
System.out.println(avgM);
return avgM;
}
I want to be able to use the average I received from averageMonthOne
and place it respectively under a the word "AVERAGE" in the println. 我希望能够使用我从
averageMonthOne
收到的平均值并将其分别放置在println中的单词“ AVERAGE”下。 Is that possible? 那可能吗?
Expected Output: 预期产量:
MONTH | HIGH | LOW | AVERAGE | RANGE
_____________________________________
30.0
使用System.out.printf()
并相应地格式化!
Very possible Just change your code to be like the following: 很有可能,只需将您的代码更改为如下所示:
...
System.out.println("\t" + "MONTH |" + "\t" + "HIGH | " + "\t" + "LOW |" + "\t" + "AVERAGE |" + "\t" + "RANGE");
System.out.println("\t" + "______________________________________________");
double myresult = main.averageMonthOne(hightemp, lowtemp);
System.out.println("\t\t\t\t" + myresult);
in1.close();
out.close();
}//end of main } // main的结尾
private static double averageMonthOne (int hightemp, int lowtemp) { double avgM = (hightemp + lowtemp)/2; 私人静态double averageMonthOne(int高温,int低温){double avgM =(高温+低温)/ 2; System.out.println(avgM);
System.out.println(avgM); return avgM;
返回avgM; }
}
If I where you I would create an Array of Strings where you put the numbers you get. 如果我在哪里,我会创建一个字符串数组,在其中放置数字。 For example:
例如:
try{
BufferedReader bufferedReader = new BufferedReader(new FileReader("fileName.txt"));
String[] numbers = new String[5];
for(int i=0; i<5; i++){ // here you only read 5 values from a file, line by line. If the file is formatted differently you have to read them differently.
numbers[i] = bufferedReader.readLine();
}
}catch(IOException io){ ... }
Then you format the array of strings into one single String line like this: 然后将字符串数组格式化为单个String行,如下所示:
String printedNumbers = "";
for(int i=0; i<numbers; i++){
printNumbers+="\t"; // every table cell is seperated by a TAB
if(numbers[i] == null){ // if the cell is empty
printNumbers +=\t; // fill it with a TAB
}else{
printNumbers +=numbers[i]; // otherwise put the number in
}
}
This may be a little much for only 5 values, but you shouldn't have to fill the gaps between your "table cells" with whitspaces. 对于5个值,这可能有点多,但是您不必用whitspace填充“表单元”之间的空隙。 In the end to print your table do:
最后打印表格:
System.out.println("\t" + "MONTH |" + "\t" + "HIGH | " + "\t" + "LOW |" + "\t" + "AVERAGE |" + "\t" + "RANGE");
System.out.println("\t" + "______________________________________________");
System.out.println(printNumbers);
This works, if you have a fixed number of Strings, that can be filled or empty. 如果您有固定数量的字符串,则可以使用该字段,可以填充或为空。 Have a bunch of variables ready like this:
准备好一堆变量,如下所示:
String avg = "", high = "", low = "", ...;
Next, fill those you need with values: 接下来,用值填充所需的内容:
avg = averageMonthOne(hightemp, lowtemp);
high = ...;
low = ...;
Lastly, call the String.format method to gather it all up: 最后,调用String.format方法来收集所有信息:
String s = String.format("%s %s %s", avg, high, low);
System.out.println(s);
You could shorten the above to 您可以将以上内容缩短为
System.out.format("%s %s %s", avg, high, low);
According to http://docs.oracle.com/javase/6/docs/api/java/util/Formatter.html , there are lots of options for formatting and spacing the output, just change the first argument of the format method accordingly. 根据http://docs.oracle.com/javase/6/docs/api/java/util/Formatter.html ,有很多用于格式化和分隔输出的选项,只需相应地更改format方法的第一个参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.