我想要将枚举器数据以列形式或列表形式打印出来。

huangapple go评论70阅读模式
英文:

I want to print out the enumerator data in columns or as a list

问题

SO...我这里有一段代码

    class secondary
    {
        enum DocDetails
        {
            DrHowlett(500, "Orthopedist"),
            DrMurdock(300, "ENT specialist"),
            DrNarendra(1000, "Nephrologist"),
            DrWhite(10000, "Pulmonologist"),
            DrSummers(1500, "Opthalmologist"),
            DrStark(12000, "Cardiologist"),
            DrBanner(20000, "Radiologist");
        
        private int x;
        private String z;
        private DocDetails(int y, String a)
        {
            
            x=y;
            z=a;
        }
    
    }
        public static void main(String args[])
        {
            for(DocDetails d:DocDetails.values())
            {
                System.out.println(d + " " + d.x + " " + d.z);
            }
        }
    }

我想要打印出如下格式:

DrHowlett 500 Orthopedist
DrMurdock 300 ENT specialist
....以此类推。

但这是代码的实际输出:

DrHowlett
500
Orthopedist
DrMurdock
300
ENT specialist
DrNarendra
1000
Nephrologist
DrWhite
10000
Pulmonologist
DrSummers
1500
Opthalmologist
DrStark
12000
Cardiologist
DrBanner
20000
Radiologist

所有的内容都在下一行打印出来。我有一个项目,需要将细节存储在枚举中并将其打印出来。如果我只是将它们打印成一个长而无意义的列表,那是没有用的。我想要将属性并排显示,而不是所有内容都在下一行打印出来。

英文:

SO...I have this piece of code here.

    class secondary
{
	enum DocDetails
	{
		DrHowlett(500, "Orthopedist"),
		DrMurdock(300, "ENT specialist"),
		DrNarendra(1000, "Nephrologist"),
		DrWhite(10000, "Pulmonologist"),
		DrSummers(1500, "Opthalmologist"),
		DrStark(12000, "Cardiologist"),
		DrBanner(20000, "Radiologist");
	
	private int x;
	private String z;
	private DocDetails(int y, String a)
	{
		
		x=y;
		z=a;
	}

}
	public static void main(String args[])
	{
		for(DocDetails d:DocDetails.values())
		{
			System.out.println(d);
			System.out.println(d.x);
			System.out.println(d.z);
		}
	}
}

And I want to print out like this

DrHowlett 500 Ortopedist
DrMurdock 300 ENT specialist
....and so on. 

But this is the actual output of the code

DrHowlett
500
Orthopedist
DrMurdock
300
ENT specialist
DrNarendra
1000
Nephrologist
DrWhite
10000
Pulmonologist
DrSummers
1500
Opthalmologist
DrStark
12000
Cardiologist
DrBanner
20000
Radiologist

Everything is printed in the next line.
I have a project where I have to store details in an enumerator and print them out. And it would not be useful if I just print them out in a long meaningless list. I want to have the attributes side by side instead of everything being printed in the next line.

答案1

得分: 0

使用 System.out.print() 替换前两个打印语句。保持最后一个语句为 System.out.println();

还要在前两个 println 语句的末尾添加空格。

public static void main(String args[]) {
    for(DocDetails d : DocDetails.values()) {
        System.out.print(d + " ");
        System.out.print(d.x + " ");
        System.out.println(d.z);
    }
}
英文:

Use System.out.print() fort he first 2 print statements. Keep the last one as System.out.println();

Also add spaces to the end of the first 2 println statements.

public static void main(String args[])
{
    for(DocDetails d:DocDetails.values())
    {
        System.out.print(d + " ");
        System.out.print(d.x + " ");
        System.out.println(d.z);
    }
}

}

答案2

得分: 0

你正在使用“println”(即打印行),它会始终在每次调用结束时包含换行符。你可以使用System.out.print(它不包括换行符),但如果你使用String.format会更清晰:

System.out.println(String.format("%s\t%d\t%s", d, d.x, dz));

这将写入一行,每个字段之间用制表符分隔。

英文:

You're using "println" (i.e. print line) which will always include a newline at the end of each call. You could use System.out.print (which doesn't include the new line) but it is cleaner if you use String.format:

System.out.println("%s\t%d\t%s".format(d, d.x, dz));

That will write a line with each field separated by a tab.

huangapple
  • 本文由 发表于 2020年4月7日 03:46:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/61067781.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定