2012-03-24 52 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