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

huangapple 未分类评论55阅读模式
英文:

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

问题

  1. SO...我这里有一段代码
  2. class secondary
  3. {
  4. enum DocDetails
  5. {
  6. DrHowlett(500, "Orthopedist"),
  7. DrMurdock(300, "ENT specialist"),
  8. DrNarendra(1000, "Nephrologist"),
  9. DrWhite(10000, "Pulmonologist"),
  10. DrSummers(1500, "Opthalmologist"),
  11. DrStark(12000, "Cardiologist"),
  12. DrBanner(20000, "Radiologist");
  13. private int x;
  14. private String z;
  15. private DocDetails(int y, String a)
  16. {
  17. x=y;
  18. z=a;
  19. }
  20. }
  21. public static void main(String args[])
  22. {
  23. for(DocDetails d:DocDetails.values())
  24. {
  25. System.out.println(d + " " + d.x + " " + d.z);
  26. }
  27. }
  28. }

我想要打印出如下格式:

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

但这是代码的实际输出:

  1. DrHowlett
  2. 500
  3. Orthopedist
  4. DrMurdock
  5. 300
  6. ENT specialist
  7. DrNarendra
  8. 1000
  9. Nephrologist
  10. DrWhite
  11. 10000
  12. Pulmonologist
  13. DrSummers
  14. 1500
  15. Opthalmologist
  16. DrStark
  17. 12000
  18. Cardiologist
  19. DrBanner
  20. 20000
  21. Radiologist

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

英文:

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

  1. class secondary
  2. {
  3. enum DocDetails
  4. {
  5. DrHowlett(500, "Orthopedist"),
  6. DrMurdock(300, "ENT specialist"),
  7. DrNarendra(1000, "Nephrologist"),
  8. DrWhite(10000, "Pulmonologist"),
  9. DrSummers(1500, "Opthalmologist"),
  10. DrStark(12000, "Cardiologist"),
  11. DrBanner(20000, "Radiologist");
  12. private int x;
  13. private String z;
  14. private DocDetails(int y, String a)
  15. {
  16. x=y;
  17. z=a;
  18. }
  19. }
  20. public static void main(String args[])
  21. {
  22. for(DocDetails d:DocDetails.values())
  23. {
  24. System.out.println(d);
  25. System.out.println(d.x);
  26. System.out.println(d.z);
  27. }
  28. }
  29. }

And I want to print out like this

  1. DrHowlett 500 Ortopedist
  2. DrMurdock 300 ENT specialist
  3. ....and so on.

But this is the actual output of the code

  1. DrHowlett
  2. 500
  3. Orthopedist
  4. DrMurdock
  5. 300
  6. ENT specialist
  7. DrNarendra
  8. 1000
  9. Nephrologist
  10. DrWhite
  11. 10000
  12. Pulmonologist
  13. DrSummers
  14. 1500
  15. Opthalmologist
  16. DrStark
  17. 12000
  18. Cardiologist
  19. DrBanner
  20. 20000
  21. 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 语句的末尾添加空格。

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

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.

  1. public static void main(String args[])
  2. {
  3. for(DocDetails d:DocDetails.values())
  4. {
  5. System.out.print(d + " ");
  6. System.out.print(d.x + " ");
  7. System.out.println(d.z);
  8. }
  9. }

}

答案2

得分: 0

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

  1. 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:

  1. 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://java.coder-hub.com/61067781.html
匿名

发表评论

匿名网友

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

确定