嗨,我正在尝试从键盘输入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();
}
}
}
答案 0 :(得分:1)
是你想要的吗?!
当您要求没有参数时,您可以在类中使用静态变量。我还将名称更改为更有意义的变量名称,因为正确选择变量的好名称总是一个好主意。
公共类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();
}
}
}