Java 从键盘输入并打印星号

Java 从键盘输入并打印星号,java,Java,您好,我正在尝试从键盘输入3个整数,并打印出与从键盘输入的整数相等的星号行。如果有人能帮上忙,我真的很感激,提前谢谢 public class Histogram1 { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner in = new Scanner(System.in); Syste

您好,我正在尝试从键盘输入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{
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
System.out.print(“请输入histogram1:”的第一个整数);
int a1=in.nextInt();
System.out.print(“请输入Historogram1:”的第二个整数);
int b1=in.nextInt();
System.out.print(“请输入histogram1:”的第三个整数);
int c1=in.nextInt();
组织图示1();
}
公共静态无效histogram1(){
int n=0;
对于(inti=0;i这是你想要的吗

  • 你的n变量总是=0:我想你想要一个参数
  • 您有两个重叠的循环:您正在打印(n*(n-1))*,每行一个(但当n=0时,没有出现)
  • 为此,您真的需要扫描仪吗?.System.in.read()可以完成这项工作,您不必管理扫描仪
  • 当您要求不使用参数时,可以在类中使用静态变量。我还将名称更改为更有意义的变量名称,因为正确选择好的变量名称始终是一个好主意

    公共类历史图1{

    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();
        }
    }
    
    static int nb_stars=0;
    /**
    *@param指定命令行参数
    */
    公共静态void main(字符串[]args){
    扫描仪输入=新扫描仪(系统输入);
    System.out.print(“请输入histogram1:”的第一个整数);
    nb_stars=in.nextInt();
    显示_直方图();
    System.out.print(“请输入Historogram1:”的第二个整数);
    nb_stars=in.nextInt();
    显示_直方图();
    System.out.print(“请输入histogram1:”的第三个整数);
    nb_stars=in.nextInt();
    显示_直方图();
    }
    公共静态无效显示_直方图(){
    对于(intj=1;j这是你想要的吗

  • 你的n变量总是=0:我想你想要一个参数
  • 您有两个重叠的循环:您正在打印(n*(n-1))*,每行一个(但当n=0时,没有出现)
  • 为此,您真的需要扫描仪吗?.System.in.read()可以完成这项工作,您不必管理扫描仪
  • 当您要求不使用参数时,可以在类中使用静态变量。我还将名称更改为更有意义的变量名称,因为正确选择好的变量名称始终是一个好主意

    公共类历史图1{

    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();
        }
    }
    
    static int nb_stars=0;
    /**
    *@param指定命令行参数
    */
    公共静态void main(字符串[]args){
    扫描仪输入=新扫描仪(系统输入);
    System.out.print(“请输入histogram1:”的第一个整数);
    nb_stars=in.nextInt();
    显示_直方图();
    System.out.print(“请输入Historogram1:”的第二个整数);
    nb_stars=in.nextInt();
    显示_直方图();
    System.out.print(“请输入histogram1:”的第三个整数);
    nb_stars=in.nextInt();
    显示_直方图();
    }
    公共静态无效显示_直方图(){
    
    对于(int j=1;jThanks,我真的需要一个扫描仪,方法histogram1不应该有一个参数,这怎么可能?您好,我非常感谢您的帮助,但现在它没有打印任何星号,并且说变量n不是方法histogram1中声明的变量,您能帮我吗??)我也在尝试实现同样的目标,但我的屏幕上只打印了一个星号。呃……编辑了我的代码,我忘了在show_histogram();)中更改n的名称。顺便说一句,避免在[1,n]之间执行循环,而更喜欢从[0,n[(即:for])运行它们(int i=0;iThanks,我真的需要一个扫描仪,方法histogram1不应该有一个参数,这怎么可能?您好,我非常感谢您的帮助,但现在它没有打印任何星号,并且说变量n不是方法histogram1中声明的,您能帮我吗??)我也在尝试实现同样的目标,但我的屏幕上只打印了一个星号。呃……编辑了我的代码,我忘了在show_histogram();)中更改n的名称。顺便说一句,避免在[1,n]之间执行循环,更喜欢从[0,n[(即:for(int I=0;I)]开始运行它们