在JAVA中如何获得这样的输出?

发布于 2025-01-15 08:48:19 字数 1285 浏览 0 评论 0原文

我需要这样的输出:-

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$

之后我想要输入酒店 ID、相关套餐以及客户想要多少个套餐。然后我需要计算全额。

这是我的代码;-

但是这个代码的输出永远无法满足我的要求

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));
});
    }
}

I need output like this :-

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. then I need to calculate the full amount.

This is my code;-

But this code's output never fullfill my requirement

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));
});
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

棒棒糖 2025-01-22 08:48:19

假设您指的是短语“这样输出”的嵌套缩进:

您可以使用制表符 \t 来缩进文本。对于嵌套缩进,您需要跟踪缩进级别,因为每个换行符都会重置缩进。

然后,您可以为打印的每一行计算一个前缀。

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

您可能需要将缩进级别传递给其他函数和/或根据需要增加它。

Assuming you mean the nested indentations whith the phrase "output like this":

you can use the tab character \t in order to indent text. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文