[英]input from the keyboard and print out asterisks
嗨,我正在嘗試從鍵盤輸入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();
}
}
}
是你想要的嗎?
當您要求不帶參數時,可以在類中使用靜態變量。 我也將名稱更改為更有意義的變量名稱,因為正確選擇變量的好名稱始終是一個好主意。
公共類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(); } }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.