2012-03-24 58 views
1

您好那裏我試圖從鍵盤輸入3個整數,並打印星號的行等於從鍵盤輸入的整數。我很感激,如果有人可以提供幫助,請提前致謝。從鍵盤輸入並打印出星號

public class Histogram1 { 
    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     Scanner in = new Scanner(System.in); 
     System.out.print("Please input the first integer for histogram1: "); 
     int a1 = in.nextInt(); 

     System.out.print("Please input the second integer for histogram1: "); 
     int b1 = in.nextInt(); 
     System.out.print("Please input the third integer for histogram1: "); 
     int c1 = in.nextInt(); 
     histogram1();    
    } 

    public static void histogram1(){    
     int n =0; 
     for (int i = 0; i <= n; i++) 
     { 
      for(int j = 1; j <=n; j++) 
      { 
       System.out.println("*"); 
      } 
      System.out.println(); 
     } 
    }   
} 

回答

1

是你想要的嗎?!

  1. 你的N個變量總是= 0:我覺得你想要一個說法,而不是
  2. 你有2路疊瓦狀:您在打印輸出(n *(N-1))*,每行一個(但作爲n = 0沒有出現)
  3. 爲此目的,你真的需要一個掃描儀?.. System.in.read()可以完成這項工作,而且你不必管理你的掃描儀。
  4. 如你所要求的沒有參數,你可以在你的類中使用靜態變量。我還將名稱更改爲更有意義的變量名稱,因爲正確地爲變量選擇正確名稱總是一個好主意。

    公共類Histogram1 {

    static int nb_stars=0; 
    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
    
        Scanner in = new Scanner(System.in); 
        System.out.print("Please input the first integer for histogram1: "); 
        nb_stars = in.nextInt(); 
        show_histogram(); 
    
        System.out.print("Please input the second integer for histogram1: "); 
        nb_stars = in.nextInt(); 
        show_histogram(); 
    
        System.out.print("Please input the third integer for histogram1: "); 
        nb_stars = in.nextInt(); 
        show_histogram(); 
    
    } 
    public static void show_histogram(){ 
         for(int j=1; j<=nb_stars; ++j) 
         { 
          System.out.print("*"); 
         } 
         System.out.println(); 
        } 
    } 
    

    }

+1

是。非常感謝。你解決了我的困境。 :) – 2012-03-24 20:08:00

+1

謝謝,我真的需要一個掃描儀和方法histogram1不應該有一個參數,這將如何可能? – 2012-03-24 20:20:00

+1

感謝您的幫助! :) – 2012-03-24 20:33:43