首页 > 解决方案 > 用户键入的名称以给 java 程序加注星标

问题描述

我是编程新手,被困在一项任务上。它说用户应该输入他们的名字和它的大小,然后它将以该大小的星星打印,而且它应该在这个程序中使用循环,而不仅仅是sout stars。我不知道如何解决这个问题,所以如果有人能给我一个想法,我将不胜感激:)

到目前为止我尝试过的是:(更新):我能够完成我的整个编码并达到我想要的东西,但是我被困在一个点上,如果用户更新大小为 n ,我怎么能在打印 Pattern(A) 打印字母的星版时在我的整个开关中使用它

import java.util.Scanner;

public class Project2 {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        int option = 0;


        while (option != 3) {

            System.out.println("Hello, Welcome to Işık NameIt program. ");
            System.out.println("----------------------------------------------------------------");
            System.out.println("The following options are available for you:\n" +
                    "1) Display a name\n" +
                    "2) Change the size\n" +
                    "3) Exit Program");
            System.out.print("Choose an option: ");

            option = input.nextInt();
            int n = 5;



            switch (option) {

                case 1:
                    System.out.print("What is your name?! ");
                    String name = input.next();

                    for(int i=0; i<=name.length()-1; i++) {
                       char letter =  name.charAt(i);

                       switch (letter){
                           case 'a':
                               PatternA(n);
                               break;
                           case 'y':
                               PatternY(n);
                               break;
                           case 'h':
                               PatternH(n)
                               break;
                           case 'm':
                               PatternM(n)
                               break;
                       }
                    }
                    break;

                case 2:
                    System.out.print("What is the new size? ");
                    int nupdate = input.nextInt();
            }

        }
    }

    public static void PatternA(int n) {
        // Outer for loop for number of lines
        for (int i = 0; i < n; i++) {

            // Inner for loop for logic execution
            for (int j = 0; j <= n / 2; j++) {

                // prints two column lines
                if ((j == 0 || j == n / 2) && i != 0 ||

                        // print first line of alphabet
                        i == 0 && j != 0 && j != n / 2 ||

                        // prints middle line
                        i == n / 2)

                    System.out.print("*");
                else
                    System.out.print(" ");
            }

            System.out.println();
        }
        System.out.println("-----------------------------------------------------------------------");
    }


标签: java

解决方案


public static void main(String[] args){
    Scanner input = new Scanner(System.in);

    System.out.println("Hello, Welcome to Işık NameIt program. ");
    System.out.println("----------------------------------------");
    System.out.println("The following options are available for you:\n" +
            "1) Display a name\n" +
            "2) Change the size\n" +
            "3) Exit Program");
    System.out.print("Choose an option: ");

    int option = input.nextInt();
    int size = option.length;

    while(option !=3){
    switch (option){

        case 1:
             for(int index = 0; index <= size; index++) {
                 System.out.print("*");
             }
             System.out.println(" ");
        case 2:
            System.out.print("What is the new size? ");
            size = input.nextInt();
            input.nextLine();
        case 3:
            break;

      }
   }
}

推荐阅读