2014-05-02 48 views
0

對於類項目,我必須編寫一個代碼,用於打印由用戶指定長度的星號框。我們必須製作兩種類型的盒子;基本和對角線。基本的盒子只是一個由星號組成的普通盒子,我已經做過。對角線箱子必須有它的內部對角線,看起來像這樣: http://prntscr.com/3fbbot在Java框中打印對角線

這裏是我的代碼至今:

public static void main(String[] args) { 

    Scanner type = new Scanner(System.in); 
    Scanner number = new Scanner(System.in); 
    Boolean f = true; 
    while (f) { 
     System.out.print("Enter a box type, basic or diagonal: "); 
     String g = type.nextLine(); 
     if (g.equals("basic") || g.equals("diagonal")) { 
     } 
     else { 
      continue; 
     } 
     System.out.print("Enter a number between 2 - 16: "); 
     try { 
      int boxSize = number.nextInt(); 
      if (g.equals("basic")) { 
       if (boxSize >= 2 && boxSize <= 16) { 
        for (int i = 0; i < boxSize * boxSize;i++) { 
         int row = i/boxSize; 
         int col = i % boxSize; 

         if (row == 0 && col < boxSize-1) { 
          System.out.print("*"); 
         } 
         else if (col == 0) { 
          System.out.print("*"); 
         } 
         else if (col == (boxSize -1)) { 
          System.out.println("*"); 
         } 
         else if (row == (boxSize - 1)) { 
          System.out.print("*"); 
         } 
         else { 
          System.out.print(" "); 
         } 
        } 
       } 
       else { 
        System.out.println("Please use a proper integer."); 
       } 
       System.out.print("Make another square? Type yes or no: "); 
       Scanner answer = new Scanner(System.in); 
       if (answer.nextLine().equals("no")) { 
        System.out.print("Thanks for playing!"); 
        System.exit(0); 
       } 
      } 
      else { 

      } 
     } 
     catch(Exception e){ 
      System.out.println("RESETTING. Please type an integer this time."); 
     } 
    } 
} 

如果你需要我更加具體或需要更多細節,請問。提前致謝。

+0

你會想問一個具體問題,儘可能詳細說明你的困惑點。越詳細越好。現在,我們所看到的只是一些需求和一些代碼,就是這樣。 –

回答

0
public static void main(String[] args) { 
    Scanner input = new Scanner(System.in); 
    while (true) { 
     System.out.print("Enter a box type, basic or diagonal: "); 
     String g = input.nextLine(); 
     if (!(g.equals("basic") || g.equals("diagonal"))) { 
      continue; 
     } 

     System.out.print("Enter a number between 2 - 16: "); 
     try { 
      int boxSize = Integer.parseInt(input.nextLine()); 
      if (boxSize >= 2 && boxSize <= 16) { 
      for (int i = 0; i <boxSize; i++) { 
       for (int j = 0; j < boxSize; j++) { 
        if (i == j && g.equals("diagonal")) { 
         System.out.print("*"); 
        } else if (i == 0 || i == boxSize-1) { 
         System.out.print("*"); 
        } else if (j == 0 || j == boxSize-1) { 
         System.out.print("*"); 
        } else { 
         System.out.print(" "); 
        } 
       } 
       System.out.println(); 
      } 
     } catch(Exception e){ 
       System.out.println("RESETTING. Please type an integer this time."); 
     } 

     System.out.print("Make another square? Type yes or no: "); 
     if (input.nextLine().equals("no")) { 
      System.out.print("Thanks for playing!"); 
      System.exit(0); 
     } 
    } 
} 
+0

您正在創建多個不必要的掃描儀。另外,對角線就是當行索引==列索引時。這段代碼應該做你想做的。 – jianweichuah

0

簡短而親切的回答可能是:

Scanner input = new Scanner(System.in); 
    System.out.println("Enter the size of the square: "); 
    int n = input.nextInt(); 

     for(int i=1;i<=n;i++){ 
       for(int j=1;j<=n;j++){ 
        if(i>1 && i<n && j>1 && j<n){ 
         if(i==j){ 
          System.out.print(" * "); 
         }else{ 
         System.out.print(" "); 
         } 
        } else{ 
         System.out.print(" * "); 
        } 
       } 
    System.out.println(); 
     } 

讓我知道,如果別人需要更多這方面的幫助。 謝謝。