简体   繁体   English

如何用Java格式化这些嵌套循环?

[英]How do I format these nested loops in Java?

The Goal 目标

Hello! 你好! I am trying to learn how to use nested for loops in AP Computer Science at school, and I am having trouble with this lab :/ I have made some progress towards getting the correct result. 我在学校尝试学习如何在AP Computer Science中使用嵌套的for循环,但在该实验室遇到了麻烦:/我在取得正确结果方面取得了一些进展。 However, I am having issues on my path. 但是,我在前进的道路上遇到了问题。 The image above shows what I need to do. 上图显示了我需要做的事情。

Sample Data: 样本数据:

C 4 C 4

A 5 A 5

Sample Output: 样本输出:

CCCC DDD EE F CCCC DDD EE F

CCCC DDD EE CCCC DDD EE

CCCC DDD CCCC DDD

CCCC 中国CCCC


AAAAA BBBB CCC DD E AAAAA BBBB CCC DD E

AAAAA BBBB CCC DD AAAAA BBBB CCC DD

AAAAA BBBB CCC AAAAA BBBB CCC

AAAAA BBBB AAAAA BBBB

AAAAA AAAAA

also above is the I/OI need. 上面也是I / OI需求。

The code I have written so far is as follows: 到目前为止,我编写的代码如下:

import java.util.Scanner;

public class LettersAndNumbers {
    public static void main(String args []) {
    int times;
    String character;//I know this sounds stupid
    Scanner scan = new Scanner(System.in);
    System.out.print("Enter a character and integer");
    character = scan.nextLine();
    times = scan.nextInt();

    String output ="";
    for(int i=times; i>=0;i--) {
        for(int j=i;j>=1;j--){
            for(int x = j; x>=1; x--)
            {
                output=output + character;


            }
            output=output+" ";

        }           
        output=output +'\n';
        int charValue = character.charAt(0);
        character = String.valueOf( (char) (charValue + 1));    
    }
    System.out.println(output);
  }
}

It yields the following output (I'm getting close): 它产生以下输出(我正在接近):

Enter a character and integer C 输入一个字符和整数C

4 4

CCCC CCC CC C CCCC CCC CC C

DDD DD D DDD DD D

EE E EE E

F F

Any help you can provide is greatly appreciated! 您能提供的任何帮助将不胜感激!

For the scanner, I would use useDelimiter("\\s") (meaning split on spaces) and next() to get the character and int out for the string. 对于扫描仪,我将使用useDelimiter(“ \\ s”)(意思是在空格上分割)和next()来获取字符并为字符串int输出。 I'll leave this for you to figure out on your own. 我将留给您自己解决。 For the loop part of it. 对于循环部分。 Assuming you have a starting character and times. 假设您有一个开始的角色和时间。 You can print 您可以列印

for(int i = times; i > 0; i--){
    for(int j = 0; j < i; j++){
         int numRepeats = i - j;
         String output = "";
         String repeatedCharacter = String.valueOf(startingCharacter + j);
         for(int k = 0; k < numRepeats; k++){
             output += repeatedCharacter;
         }
         System.out.print(output + " ");
    }
     System.out.println("");
}

Working solution: 工作解决方案:

Scanner scanner = new Scanner(System.in);
    String userInputChar=scanner.next();
    char userChar=userInputChar.charAt(0);      
    int userInputDigit=scanner.nextInt();       
    for(int i=1;i<=userInputDigit;i++){
        char charProcess=userChar;          
        for(int j=(userInputDigit-i)+1,l=userInputDigit;j>=1;j--,l--){              
            for(int k=l;k>=1;k--){
                System.out.print(charProcess);
            }
            charProcess=(char) (charProcess+1);
            System.out.print(" ");              
        }
        System.out.print("\n");
    }
    scanner.close();

Let me know how it goes. 让我知道事情的后续。 Good Luck. 祝好运。

Ok, here are two hints: 好的,这里有两个提示:

1) character = String.valueOf( (char) (charValue + 1)); 1) character = String.valueOf( (char) (charValue + 1)); Maybe see if there's a better place for this statement? 也许看看这种说法是否有更好的地方?

2) Perhaps you can use something for the variable 'character' other than String... 2)也许您可以为String以外的变量'character'使用一些东西...

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM