java - 我在我的代码中输入了一些针对控制台的内容,但我找不到我搞砸的地方

标签 java

<分区>

我正在对我的作业进行测试以确保一切正常,我不小心在我的代码中输入了一个用于控制台的输入,结果输出不再正确。这是代码:

import java.util.*;

public class JTCalendar {
    //This defines how big the calendar is
    public static final int Width = 5;
       
       public static void drawMonth(int month,int day) {
          
          //Ascii art of a robot man
          System.out.println("    /\\     /\\");
          System.out.println("   /  \\   /  \\");
          System.out.println(" ---------------");
          System.out.println("(| (o)     (o)  |)");
          System.out.println(" |    wwww      |");
          System.out.println(" ----------------");
          
          //This finds the center
          for(int i = 1; i <= 1; i++) {
             for(int j = 1; j <= (Width * 7) / 2; j++) {
             System.out.print(" ");
             }   
             System.out.print(month);
             for(int d = 1; d <= (Width * 7) / 2 ; d++) {
             System.out.print(" ");
             }
             System.out.println();

我相信错误是在这个区域的某个地方出现的,因为这是一个输出困惑的区域

      }
      //Size calculator
   for(int g =1; g <= Width * 7; g++) {
      System.out.print("=");
   }
   System.out.println();
   drawRow(5); 
   displayDate(month,day); 
   }
      //Start of the rows
      public static void drawRow(int row) {
      int day = 1;
         for(int i = 0; i < row; i++) {
            
            System.out.print("|");
            for(int a = 1; a <= 7; a++) {
               
               int length=String.valueOf(day).length();
               for(int j = 1; j < Width - length; j++) {
                  System.out.print(" ");
               }
                  
            System.out.print(day);  
            day++;
            System.out.print("|");
            }  
         System.out.println();
         for(int t = 1; t < (Width /2); t++) {
            System.out.print("|");
            for(int h = 1; h<=7; h++) {
         
               for(int f = 1; f <= Width - 1; f++) {
                     System.out.print(" ");
               }
            System.out.print("|");
            }
         }
         for(int e = 1; e <= Width * 7; e++) {
            System.out.print("=");
         }
      System.out.println();
      }
      //End of the rows
   }
   //Below displays the date
   public static void displayDate(int month, int day) {
      System.out.println("Month: " + month + "\nDay: " + day);
   }
   //End display
   //Below seperates month and day into two ints
   public static int monthFromDate(String date) {
      String tokens[] = date.split("/");
      return Integer.parseInt(tokens[0]);
   }

我相信低于此的任何内容都是正确的,但我仍将其包括在内

public static int dayFromDate(String date) {
      String tokens[] = date.split("/");
      return Integer.parseInt(tokens[1]);
   }
   //End seperation of month and day
   
   public static void main(String[] args) {
      //Main scanner console
      Scanner console = new Scanner(System.in);
      System.out.print("What date would you like to look at? (mm/dd): ");
      String date = console.next();
      int day = dayFromDate(date);
      int month = monthFromDate(date);      
      drawMonth(month, day);
      //This draws the ascii calendar
      
      Calendar cal = Calendar.getInstance();
         day = cal.get(Calendar.DATE);
         month = cal.get(Calendar.MONTH) + 1;
         System.out.println("\nToday is: " + month + "/" + day + "\n");
         drawMonth(month,day);
      //This draws the calandar for the current date
   }
}

错误的输出:

    /\     /\
   /  \   /  \
 ---------------
(| (o)     (o)  |)
 |    wwww      |
 ----------------
                 2                 
===================================
|   1|   2|   3|   4|   5|   6|   7|
|    |    |    |    |    |    |    |===================================
|   8|   9|  10|  11|  12|  13|  14|
|    |    |    |    |    |    |    |===================================
|  15|  16|  17|  18|  19|  20|  21|
|    |    |    |    |    |    |    |===================================
|  22|  23|  24|  25|  26|  27|  28|
|    |    |    |    |    |    |    |===================================
|  29|  30|  31|  32|  33|  34|  35|
|    |    |    |    |    |    |    |===================================
Month: 2
Day: 25

Today is: 2/13

    /\     /\
   /  \   /  \
 ---------------
(| (o)     (o)  |)
 |    wwww      |
 ----------------
                 2                 
===================================
|   1|   2|   3|   4|   5|   6|   7|
|    |    |    |    |    |    |    |===================================
|   8|   9|  10|  11|  12|  13|  14|
|    |    |    |    |    |    |    |===================================
|  15|  16|  17|  18|  19|  20|  21|
|    |    |    |    |    |    |    |===================================
|  22|  23|  24|  25|  26|  27|  28|
|    |    |    |    |    |    |    |===================================
|  29|  30|  31|  32|  33|  34|  35|
|    |    |    |    |    |    |    |===================================
Month: 2
Day: 13

良好的输出:

    /\     /\
   /  \   /  \
 ---------------
(| (o)     (o)  |)
 |    wwww      |
 ----------------
                 2                 
===================================
|   1|   2|   3|   4|   5|   6|   7|
|    |    |    |    |    |    |    |
===================================
|   8|   9|  10|  11|  12|  13|  14|
|    |    |    |    |    |    |    |
===================================
|  15|  16|  17|  18|  19|  20|  21|
|    |    |    |    |    |    |    |
===================================
|  22|  23|  24|  25|  26|  27|  28|
|    |    |    |    |    |    |    |
===================================
|  29|  30|  31|  32|  33|  34|  35|
|    |    |    |    |    |    |    |
===================================
Month: 2
Day: 25

Today is: 2/13

    /\     /\
   /  \   /  \
 ---------------
(| (o)     (o)  |)
 |    wwww      |
 ----------------
                 2                 
===================================
|   1|   2|   3|   4|   5|   6|   7|
|    |    |    |    |    |    |    |
===================================
|   8|   9|  10|  11|  12|  13|  14|
|    |    |    |    |    |    |    |
===================================
|  15|  16|  17|  18|  19|  20|  21|
|    |    |    |    |    |    |    |
===================================
|  22|  23|  24|  25|  26|  27|  28|
|    |    |    |    |    |    |    |
===================================
|  29|  30|  31|  32|  33|  34|  35|
|    |    |    |    |    |    |    |
===================================
Month: 2
Day: 13

最佳答案

你需要在绘制每一行之后添加一个System.out.println();:

         System.out.println();
         for(int t = 1; t < (Width /2); t++) {
            System.out.print("|");
            for(int h = 1; h<=7; h++) {
         
               for(int f = 1; f <= Width - 1; f++) {
                     System.out.print(" ");
               }
            System.out.print("|");
            }
         }
        
         System.out.println(); // <== ADDED PRINTLN HERE

         for(int e = 1; e <= Width * 7; e++) {
            System.out.print("=");
         }
      System.out.println()

关于java - 我在我的代码中输入了一些针对控制台的内容,但我找不到我搞砸的地方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66193096/

相关文章:

java - 如何在Java中从ElasticSearch响应中解析GeoPoint值?

java - 无法在 FragmentPagerAdapter 中使用方法 "getString(int resId)"

java - 在 Java 启动时加载 DLL

java - Controller 中的所有 JavaFX FXML 对象均为 null

java - 我忘记了哪个库?

java - Gson将字段反序列化为具有不同名称的变量,尽管具有带有字段名称的变量

java - 在Android中动态查找 fragment

java - 我们可以在android中对音频文件使用加密/解密吗

java - ReentrantReadWriteLock - 在 if - else block 中锁定/释放

java - 这个数组的括号里应该写什么?