切换问题,阵列打印

时间:2014-09-24 04:36:08

标签: java switch-statement

- 在情况2中,我试图让用户仅使用特定的阵列位置输入元素(N,Y,P,D,G)。我在开关内部使用了开关,但是如果他输入错误的元素我该如何重新循环呢?

- 在案例3中,我正在尝试打印整个2D数组,但原始数组中填充了元素G.

- 在案例4中,我正在尝试将地图视图更改为符号。

刚刚发现这是一种练习,试图在某种程度上学习初学者复杂的编程,所以任何帮助都会受到赞赏,任何批评都会受到高度赞赏,因为我希望对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=@
            //
    }
}
}

3 个答案:

答案 0 :(得分:1)

案例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:

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

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语句块或开关块来打印符号而不是地图。例如,

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

答案 1 :(得分: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]

使用input.next()代替nextLine()

官方文件中描述的

Scanner.nextLine()是:

  

使此扫描程序超过当前行并返回该输入   被跳过。此方法返回当前行的其余部分,   排除末尾的任何行分隔符。该职位设定为   下一行的开头。

调用nextInt()后,它无法正确终止分配的内存行。因此,当第一次调用nextLine()时,它实际上终止了实际上具有值的前一行 - 通过nextInt()输入而不是接受新的String值。这就是sentence3获得null价值的原因。

  

所以在案例2中,我试图让用户输入元素   (N,Y,P,D,G)仅限于特定的阵列位置。我用过里面的开关   切换,但如果他输入了错误的元素,我该如何重新循环?

您可以尝试这种方式:

    boolean bVaildInput = true;
    do{
        System.out.println("Enter the element you want to enter");
        tempelement = input.next();
    switch (tempelement)
        {
        case "Y":
            map[row1][col1] = tempelement;
            break;
        case "N":
            map[row1][col1] = tempelement;
            break;
        case "P":
            map[row1][col1] = tempelement;
            break;
        case "D":
            map[row1][col1] = tempelement;
            break;
        case "G":
            map[row1][col1] = tempelement;
            break;
        default :
            System.out.println("Invalid Input");
            bValidInput = false;
        }
   }while(!bVaildInput);
  

在案例3中,我正在尝试打印整个2D阵列,但是原始的   数组用元素G填充。

        for (int i=0;i < map.length;i++)
        {
            for (int j=0; j < map[i].length; j++)
            {
                System.out.print(map[i][j]+"\t");
            }
            System.out.print("\n");
        }
  

在案例4中,我正在尝试将地图视图更改为符号。

付出一些努力。

答案 2 :(得分:0)

由于您使用此作为练习,我不会回答您的问题,但会提供一些有关正在发生的事情和一些链接的信息:

案例2: 你真的不需要第二个开关。看看交换机中每个案例的代码,它完全一样。您想要使用的是while循环并测试输入是否有效。这样,如果用户输入无效输入,它将要求他们再试一次。 See Example 3 in this post

案例3:有很多关于如何打印2D数组的例子 - 它取决于你想要它的外观。 I Suggest looking at this example

案例4:这实际上只是案例3的扩展,只是打印出存储的输入字母,打印出符号。这是第二个switch语句可能有用的地方。