簡體   English   中英

從鍵盤輸入並輸出星號

[英]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();
        }
    }           
}

是你想要的嗎?

  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(); } } 

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM