PrintDemo.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import flanagan.io.Db;
  2. import java.util.Arrays;
  3. import java.time.ZonedDateTime;
  4. import java.time.ZoneId;
  5. import java.time.ZonedDateTime;
  6. import java.time.LocalDateTime;
  7. import java.time.Month;
  8. import java.util.Locale;
  9. public class PrintDemo {
  10. public static void main(String args[]) {
  11. String headerComment = "Datentyp";
  12. String[] comments = {"Integer (i)","Double (d)", "String (s)", "Datum", "(Vor-)Name"};
  13. String[] boxTitles = {"i", "d", "s", "Dat", "name"};
  14. int defaultBox = 1;
  15. int Box = Db.optionBox(headerComment, comments, boxTitles, defaultBox);
  16. if (Box == 1) {
  17. //int
  18. int i = Db.readInt("\nGeben sie den int i ein!\n", 123);
  19. char[] chars_dash = new char[80];
  20. Arrays.fill(chars_dash, '-');
  21. String s_dash = new String(chars_dash);
  22. System.out.format("%s\n", s_dash);
  23. System.out.printf( "|%d| |%d|%n" , i , -i ) ; // |123| |-123|
  24. System.out.printf( "|%5d| |%5d|%n" , i , -i ) ; // | 123| | -123|
  25. System.out.printf( "|%-5d| |%-5d|%n" , i , -i ) ; // |123 | |-123 |
  26. System.out.printf( "|%+-5d| |%+-5d|%n" , i, -i ) ; // |+123 | |-123 |
  27. System.out.printf( "|%05d| |%05d|%n%n", i, -i ) ; // |00123| |-0123|
  28. System.out.printf( "i =%6d%n", i);
  29. System.out.printf( "1i =%6d i2 =%+7d%n", i, i);
  30. System.out.printf( "1i =%+-7di2 =%+-7d%n", i, -i);
  31. System.out.printf( "1i = %-6di2 = %-6di3 =%6d%n%n", i, i, -i);
  32. System.out.printf( "|%X| |%x|%n", 0xabc, 0xabc ); // |ABC| |abc|
  33. System.out.printf( "|%08x| |%#x|%n%n", 0xabc, 0xabc ); // |00000abc| |0xabc|
  34. System.exit(0);
  35. }
  36. if (Box == 2) {
  37. //Double
  38. double d = Db.readDouble("\nGeben sie deine Double Zahl d ein!\n", 12345.678000);
  39. System.out.printf( "|%f| |%f|%n" , d, -d ); // |12345,678000| |-12345,678000|
  40. System.out.printf( "|%.2f| |%.2f|%n" , d, -d ); // |12345,68| |-12345,68|
  41. System.out.printf( "|%,10f| |%,10f|%n" , d, -d ); // |1234,567800| |-1234,567800|
  42. System.out.printf( "|%10.2f| |%10.2f|%n" , d, -d ); // | 12345,68| | –12345,68|
  43. System.out.printf( "|%010.2f| |%010.2f|%n",d, -d ); // |0012345,68| |-012345,68|
  44. System.exit(0);
  45. }
  46. if (Box == 3) {
  47. //String
  48. String s = Db.readLine("\nGeben sie einen String ein!\n", "Monsterbacke");
  49. System.out.printf( "%n|%s|%n", s ) ; // |Monsterbacke|
  50. System.out.printf( "|%20s|%n", s ) ; // | Monsterbacke|
  51. System.out.printf( "|%-20s|%n", s ) ; // |Monsterbacke |
  52. System.out.printf( "|%7s|%n", s ) ; // |Monsterbacke|
  53. System.out.printf( "|%.7s|%n", s ) ; // |Monster|
  54. System.out.printf( "|%20.7s|%n", s ); // | Monster|
  55. System.exit(0);
  56. }
  57. if (Box == 4) {
  58. String loc = Db.readLine("\nLanguage: \n\nChinese = zh \nEnglish = en" +
  59. "\nFrenc fr \nGerman de \nItalian it \nJapanese = ja" +
  60. "\nKorean = ko \nPortuguese = pt \nSpanish = es" +
  61. "\nSwedish = sv\n\n", "de");
  62. Locale mylocale = new Locale(loc);
  63. ZoneId zoneId = ZoneId.of("Europe/Zurich");
  64. ZonedDateTime currentTime = ZonedDateTime.now();
  65. System.out.printf(mylocale, "%tA %<tB %<te, %<tY %n", currentTime);
  66. System.out.printf(mylocale, "%TA %<TB %<te, %<tY %n", currentTime);
  67. System.out.printf(mylocale, "%tD %n", currentTime);
  68. System.out.printf(mylocale, "%tF %n", currentTime);
  69. System.out.printf(mylocale, "%tc %n", currentTime);
  70. System.out.printf(mylocale, "%Tc %n", currentTime);
  71. System.out.printf(mylocale, "Es ist: %tA %<te %<tB %<tY, %<tH:%<tM %n", currentTime);
  72. }
  73. if (Box == 5) {
  74. String name = Db.readLine("\nGeben sie ihren Namen ein!\n", "Peter");
  75. System.out.println("Ihr Name lautet: " + name);
  76. try {
  77. System.out.println("Ihr Vorname lautet: " + name.substring(0,name.indexOf(" ")));
  78. System.out.println("Ihr Nachname lautet: " + name.substring(name.indexOf(" ") + 1));
  79. } catch (StringIndexOutOfBoundsException e) {
  80. System.out.println("Fehler: Sie haben nur einen einzigen Namen angegeben");
  81. }
  82. }
  83. }
  84. }