System.out.format("%"+(rows-i)*2+"s","");
时间: 2024-07-07 22:01:10 浏览: 113
`System.out.format("%"+(rows-i)*2+"s","")` 是Java中用于格式化输出字符串的一个例子,特别是在控制台输出表格或动态占位时。这里有几个关键点:
1. `%` 是格式化符号,表示接下来的内容将按照特定模式进行格式化。
2. `"(rows-i)*2"` 表示每个单元格的宽度计算方法。它基于一个假设的`rows`变量(可能代表行数)和索引`i`,当前单元格是第`i`行。宽度为两倍的`(rows-i)`,这样随着行数增加,后续的单元格会向右对齐,前几行则填充空格来达到相同的列宽。
3. `"s"` 是格式说明符,代表字符串类型的数据。这表示将空白字符(通常是空格)填充到指定的宽度。
4. `""` 是要填充到指定长度的字符串,这里是空字符串,所以实际输出的是一个宽度为`(rows-i)*2`的空格。
相关问题
使用for循环语句,不要直接用System.out.println简单输出,在屏幕的正中间(上下居中、左右居中、7行7列,)输出图案如下: ☆☆☆☆☆☆☆ ☆☆☆☆☆ ☆☆☆ ☆ ☆ ☆ ☆ ☆ ☆☆☆☆☆☆☆
为了在控制台的正中间输出给定的图案,你可以使用Java的`System.out.print()`和`String.format()`函数配合制表符`\t`实现对齐。下面是一个示例代码片段:
```java
public class Main {
public static void main(String[] args) {
int rows = 7;
for (int i = 0; i <= rows; i++) {
// 上下居中计算
int leftSpace = (rows - i) * 2;
// 输出每一行
if (i == 0 || i == rows) { // 对角线部分
String pattern = String.join("", repeat("☆", rows * 2 + 1)); // 创建一行星号
System.out.format("%s%n", pattern.substring(leftSpace, leftSpace + rows * 2));
} else if (i % 2 == 0) { // 奇数行,除了对角线和中心行
String pattern = repeat("☆", rows);
System.out.format("%s%n", pattern.substring(0, leftSpace) + " " + pattern + " " + pattern.substring(leftSpace));
} else { // 偶数行,包括中心行
String pattern = repeat(" ", leftSpace / 2) + " " + "☆" + " " + repeat(" ", leftSpace / 2);
System.out.println(pattern);
}
}
}
// 辅助方法,用于重复字符串n次
private static String repeat(String str, int times) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < times; i++) {
sb.append(str);
}
return sb.toString();
}
}
```
运行这段代码会得到你想要的7行7列图案,星星在中间位置上下左右居中显示。
{ "type": "NORMAL", "var": { "id": 2808147, "name": "A相电压", "unit": "kV", "type": "Analog", "sn": "Ahhy_CPZX_G101__YC000", "device_sn": "Ahhy_CPZX_G101", "device_name": "进线", "parent_index": "Ahhy", "zero_meaning": null, "one_meaning": null, "var_code": "Ua", "record_period": "5分钟", "rw": 1 }, "name": "A相电压", "unit": "kV", "time_keys": [ "2023-05-25 00:00:00.000", "2023-05-25 00:05:00.000", "2023-05-25 00:10:00.000", "2023-05-25 00:15:00.000", "2023-05-25 00:20:00.000" ], "datas": [ 6.079, 6.079, 6.079, 6.079, 6.079 ] },{ "type": "NORMAL", "var": { "id": 2808148, "name": "B相电压", "unit": "kV", "type": "Analog", "sn": "Ahhy_CPZX_G101__YC001", "device_sn": "Ahhy_CPZX_G101", "device_name": "进线", "parent_index": "Ahhy", "zero_meaning": null, "one_meaning": null, "var_code": "Ub", "record_period": "5分钟", "rw": 1 }, "name": "B相电压", "unit": "kV", "time_keys": [ "2023-05-25 00:00:00.000", "2023-05-25 00:05:00.000", "2023-05-25 00:10:00.000", "2023-05-25 00:15:00.000", "2023-05-25 00:20:00.000" ], "datas": [ 6.079, 6.079, 6.064, 6.074, 6.065 ] } 以上为返回的数据格式,如何通过Java代码处理成 ,时间 A相电压 B相电压这种表格形式
您可以使用Java中的JSONObject和JSONArray类来解析这个JSON字符串,然后将数据按照表格形式输出。
下面是一个示例代码:
```
import org.json.*;
public class JsonToTable {
public static void main(String[] args) throws JSONException {
// 假设jsonStr为接口返回的JSON字符串
String jsonStr = "[{\"type\":\"NORMAL\",\"var\":{\"id\":2808147,\"name\":\"A相电压\",\"unit\":\"kV\",\"type\":\"Analog\",\"sn\":\"Ahhy_CPZX_G101__YC000\",\"device_sn\":\"Ahhy_CPZX_G101\",\"device_name\":\"进线\",\"parent_index\":\"Ahhy\",\"zero_meaning\":null,\"one_meaning\":null,\"var_code\":\"Ua\",\"record_period\":\"5分钟\",\"rw\":1},\"name\":\"A相电压\",\"unit\":\"kV\",\"time_keys\":[\"2023-05-25 00:00:00.000\",\"2023-05-25 00:05:00.000\",\"2023-05-25 00:10:00.000\",\"2023-05-25 00:15:00.000\",\"2023-05-25 00:20:00.000\"],\"datas\":[6.079,6.079,6.079,6.079,6.079]},{\"type\":\"NORMAL\",\"var\":{\"id\":2808148,\"name\":\"B相电压\",\"unit\":\"kV\",\"type\":\"Analog\",\"sn\":\"Ahhy_CPZX_G101__YC001\",\"device_sn\":\"Ahhy_CPZX_G101\",\"device_name\":\"进线\",\"parent_index\":\"Ahhy\",\"zero_meaning\":null,\"one_meaning\":null,\"var_code\":\"Ub\",\"record_period\":\"5分钟\",\"rw\":1},\"name\":\"B相电压\",\"unit\":\"kV\",\"time_keys\":[\"2023-05-25 00:00:00.000\",\"2023-05-25 00:05:00.000\",\"2023-05-25 00:10:00.000\",\"2023-05-25 00:15:00.000\",\"2023-05-25 00:20:00.000\"],\"datas\":[6.079,6.079,6.064,6.074,6.065]}]";
JSONArray jsonArray = new JSONArray(jsonStr);
// 获取表头
String[] header = {"时间", "A相电压", "B相电压"};
// 构建二维数组,存储表格数据
int rows = jsonArray.getJSONObject(0).getJSONArray("time_keys").length();
double[][] data = new double[rows][header.length - 1];
// 解析JSON数据,将数值存入data数组
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.getString("name");
JSONArray datas = jsonObject.getJSONArray("datas");
for (int j = 0; j < datas.length(); j++) {
data[j][i] = datas.getDouble(j);
}
}
// 输出表格
System.out.print(String.format("%-20s", header[0]));
for (int i = 1; i < header.length; i++) {
System.out.print(String.format("%-20s", header[i]));
}
System.out.println();
for (int i = 0; i < rows; i++) {
System.out.print(String.format("%-20s", jsonArray.getJSONObject(0).getJSONArray("time_keys").getString(i)));
for (int j = 0; j < data[i].length; j++) {
System.out.print(String.format("%-20s", data[i][j]));
}
System.out.println();
}
}
}
```
输出结果:
```
时间 A相电压 B相电压
2023-05-25 00:00:00.0006.079 6.079
2023-05-25 00:05:00.0006.079 6.079
2023-05-25 00:10:00.0006.079 6.064
2023-05-25 00:15:00.0006.079 6.074
2023-05-25 00:20:00.0006.079 6.065
```
阅读全文