简体   繁体   English

怎么才能在JAVA中得到这样的Output呢?

[英]How can I get Output like this in JAVA?

I need output like this:-我需要这样的 output:-

ID :- 1
    Hotel :- Hotel 1
    Packages :- 
            Package 1 = 10$
            Package 2 = 20$
            Package 3 = 30$
    
    ID :- 2
    Hotel :- Hotel 2
    Packages :- 
            Package 1 = 10$
            Package 2 = 20$
            Package 3 = 30$
    
    
    
    ID :- 3
    Hotel :- Hotel 3
    Packages :- 
            Package 1 = 10$
            Package 2 = 20$
            Package 3 = 30$

After that I want to get input as hotel ID, relevant package, and how many packages customers want.之后我想输入酒店 ID、相关的 package 以及客户想要多少套餐。 then I need to calculate the full amount.然后我需要计算全额。

This is my code;-这是我的代码;-

But this code's output never fullfill my requirement但是这段代码的 output 从来没有满足我的要求

import java.util.Arrays;
import java.util.LinkedHashMap;

public class HotelChainServicePublishImpl implements HotelChainServicePublish {
    
    LinkedHashMap<Integer, String[]> hotelList = new LinkedHashMap<Integer, String[]>();
    Double subTotal = 0.0;
    @Override
    public void getHotels() {
        hotelList.put(1, new String[] {"Hotel 1" , "100000.00"} );
        hotelList.put(2, new String[] {"Hotel 2" , "200000.00"} );
        hotelList.put(3, new String[] {"Hotel 3" , "300000.00"} );
    
        hotelList.forEach((k, v)  -> {System.out.println(" ");
        System.out.println(k + ")");
        Arrays.asList(v).stream().forEach(s -> System.out.println(s));
});
    }
}

Assuming you mean the nested indentations whith the phrase "output like this":假设你的意思是嵌套的缩进和短语“这样的输出”:

you can use the tab character \t in order to indent text.您可以使用制表符\t来缩进文本。 For nested indentation you will need to keep track of the indentation level as with each newline the indentation gets reset.对于嵌套缩进,您需要跟踪缩进级别,因为每个换行符都会重置缩进。

You can then compute a prefix for each line you print with然后,您可以为打印的每一行计算一个前缀

String prefix = "";
for (int i = 0; i < indentation_level; i++) {
    prefix += "\t";
}

You may need to pass the indentation level to other functions and/or increase it as needed.您可能需要将缩进级别传递给其他函数和/或根据需要增加缩进级别。

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

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