java - 用户键入的名称以给 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("-----------------------------------------------------------------------");
}
解决方案
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;
}
}
}
推荐阅读
- c# - TagHelper 不呈现
- javascript - javascript eval 以一种方式工作,但不能以另一种方式工作,为什么?
- python - CNN 的 1080x1920 太大,如何优化?
- google-apps-script - 我在从电子表格的一个选项卡中复制一行并复制到另一个选项卡时遇到问题
- c - 如何在没有 C 中的指针的情况下检索超出范围的静态值?
- java - 仅本地使用的 Maven 依赖项
- python - 如何堆叠多个图像numpy ndarray
- javascript - 邀请不和谐服务器机器人时将服务器添加到数据库
- javascript - 使用 Javascript 或 Node js 加密 .rar 文件
- flutter - Flutter - 获取单个选项卡的 ID