代码之家  ›  专栏  ›  技术社区  ›  LalaJava

Java方法字符模式-初学者

  •  -1
  • LalaJava  · 技术社区  · 7 年前

    我正在上java的第一个学期,我的诵读困难症让这变得更加困难,但我坚持。我需要获得用户输入的行数、字符数和字符类型。这是给hw的,所以任何建议都很感激。我需要使用一个方法(调用)创建一个模式,如下所示,并使用相应的输入变量打印字符总数:

    行数:3
    字符数:6
    字符:X

    XXXXXX
    XXXXXX
    XXXXXX

    字符总数:18
    以下是我目前掌握的代码:

    static Scanner console = new Scanner(System.in);  
      public static void main (String[] args)  
      {  
        int totalChar;  
        int numLn = console.nextInt();          //Should I assign here or  
        int charPerLn = console.nextInt();      //after sys.println?  
        char symbol = console.next().charAt(0);  
    
        System.out.println("# of lines: " +numLn);
    
        System.out.println("# of characters: "+charPerLn);
    
        System.out.println("Character: "+symbol);
    
        System.out.print(Pattern);    //Pretty sure this is wrong or at
                                      //least in the wrong place to call
    
        System.out.println("Total Characters: "+totalChar);
        totalChar = numOfLines * charPerLine;
    }
    
    public static int Pattern(int x)
    {
        int pattern;
    
        //I need a nested while or for loop here but I'm not sure how to 
        //assign the main's values for #lines and #character
        //Am I over-thinking this?  
    
        return pattern;
    }
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Justin Albano    7 年前

    这个 Pattern 方法应类似于以下内容:

    public static int Pattern(int lineCount, int charCount, char character) {
    
        for (int i = 0; i < lineCount; i++) {
            // Repeat the supplied character for charCount
        }
    }
    

    这个 图案 方法现在包括行数和每行字符数的参数,因为这些值将指示迭代的次数。我将把正确打印每行字符数的逻辑留给您(这将给出整个答案),但是循环(带索引 i )迭代行数。一般来说,当你想重复某事时 n 次,创建具有以下基本结构的循环:

    for (i = 0; i < n; i++) { /* ... */ }
    

    这意味着在第一次迭代中, 等于 0 然后 1 然后 2 ,依此类推,直到 达到 n-1 。此时,迭代停止。这将导致迭代 n 次数,其中 具有以下值 [0, 1, 2, ..., n-1]

    问题的其余部分可以通过再次应用相同的原则来解决。

        2
  •  0
  •   mab    7 年前
    static Scanner console = new Scanner(System.in);  
    public static void main (String[] args)  
    {  
        int totalChar;  
        int numOfLines = console.nextInt(); 
        int charPerLine = console.nextInt(); // keep your variable names consistent by using either "charPerLn" or "charPerLine", not both :)
        char symbol = console.next().charAt(0);  
    
        System.out.println("# of lines: " + numOfLines);
        System.out.println("# of characters: " + charPerLine);
        System.out.println("Character: " + symbol);
        System.out.println(getPattern( /* [use, the, parameters, from, above, here] */ ));
    
        totalChar = numOfLines * charPerLine; // first assign to totalChar, then you can print it
        System.out.println("Total Characters: " + totalChar);
    }
    
    public static String getPattern(int numOfLines, int charPerLine, char symbol) // you want to return a string, so we change "int" to "String" here
    {
        String pattern = "";
    
        /* As "that other guy" said, nested for loops go here */
        for(/* ... */)
        {
            /* ... */
            for(/* ... */)
            {
             /* ... */
            }
            /* ... */
        }
    
        return pattern;
    }