gpt4 book ai didi

java - 切换问题,阵列打印

转载 作者:行者123 更新时间:2023-12-02 05:25:59 25 4
gpt4 key购买 nike

- 所以在情况 2 中,我试图让用户仅输入具有特定数组位置的元素(N,Y,P,D,G)。我在 switch 中使用了 switch,但是如果他输入了错误的元素,我该如何重新循环?

-在情况 3 中,我试图打印整个 2D 数组,但原始数组填充了元素 G。

-在情况 4 中,我尝试将 map View 更改为符号。

只是发现这是一种练习,试图在某种程度上学习初学者复杂的编程,因此任何帮助都将受到赞赏,任何批评都将受到高度赞赏,因为我想拥有良好的 java 基础知识:)

import java.util.Scanner;

public class Studyone {

public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int row;
int col;
int row1; //for case 2, so it would allow the user to input the element in the request array
int col1;
System.out.println("Enter the row: ");// knowing the row and the col of the 2D array to base the rest on.
row = input.nextInt();
System.out.println("Enter the col: ");
col = input.nextInt();
String sentence;
String sentence2;
String sentence3;
String tempelement;
String N;
String Y;
String G;
String P;
String D;
int choice;
System.out.println("The menu\n1)Enter the Elements in order\n2)Enter the the Col and row number spec.\n3)Show the Orig. map as is\n4)Show symbolic map.");
choice = input.nextInt();
System.out.println(choice);
String [][] map = new String[row][col];
switch (choice)
{
case 1:
sentence3=input.nextLine(); // for some reason it auto enters the first map [0][0] as null, this line allows the user to input the map[0][0]
map[0][0]=sentence3;
for (int i = 0; i < map.length; i++)
{
for (int j = 0;j < map[i].length; j++)
{
System.out.println("Enter the element");
sentence2 = input.nextLine();
map[i][j]=sentence2;
}
System.out.println(map[1][1]);
System.out.println(choice);

}
break;
case 2:
System.out.println("Enter the row");
row1 = input.nextInt();
System.out.println("Enter the col");
col1 = input.nextInt();
System.out.println("Enter the element you want to enter");
tempelement = input.nextLine();
switch (tempelement)
{
case "Y":
map[row1][col1] = tempelement;
case "N":
map[row1][col1] = tempelement;
case "P":
map[row1][col1] = tempelement;
case "D":
map[row1][col1] = tempelement;
case "G":
map[row1][col1] = tempelement;
}

System.out.println(tempelement); // test,does not let me enter any temp elements, lets it empty in the input without letting the use enter anything. Figure out error
System.out.println(choice); // test, prints the right choice, Correct.
break;
case 3:
for (int i=0;i > map.length;i++)
{
for (int j=0; j > map[i].length; j++)
{
map[i][j] = N;
// print statment, need to find how to do.
}
}**strong text**
break;
case 4:
// having symbols instead of the letters, N=* , P= ~ , Y=. , G= #, D=@
//
}
}
}

最佳答案

情况2:

我不知道为什么你要在其中使用 switch 语句来检查情况是否是 N、Y、P、D 或 G。为了检查输入是否正确,你可以使用 while 循环并中断当放置正确的输入时。

 System.out.println("Enter the row");
row1 = input.nextInt();
System.out.println("Enter the col");
col1 = input.nextInt();

while(true){
System.out.println("Enter the element you want to enter");
tempelement = input.nextLine();
if(tempelement.equals("N") || tempelement.equals("Y") || tempelement.equals("P") || tempelement.equals("D") || tempelement.equals("G")){
map[row1][col1] = tempelement;
break;
}
}

情况3:

要打印二维数组,您需要一个嵌套的 for 循环。它需要 2 个 for 循环。

for(int i=0; i<map.length; i++){
for(int j=0; j<map[i].length; j++){
System.out.print(map[i][j]);
}
System.out.print("\n"); // this is for spacing after a row of element prints
}

案例4:

这与情况 3 相同,只是在内部 for 循环内,您将有一个 if-else 语句 block 或 switch block 来打印符号而不是映射。例如,

...
for(int j=0; j<map[i].length; j++){
switch(map[i][j]){
case "N": System.out.print("Symbol"); break;
case "P": ... code
... code
}
}

关于java - 切换问题,阵列打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26008633/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com