简体   繁体   中英

Printing ASCII table java

This is the ASCII table printing part of my code for a overall program. I am prompting the user to enter desired roles and columns but I can't get the user input connected to the actual production of the table. I know that my variables aren't matching correctly, how should I go about changing my variable so that rs, re, cs, ce is used in the implementation of the table? I confused myself quite a bit so I am asking help on here. Thanks!

    else if(input == 3)
    {
        System.out.println("Table B.1 ASCII Character Set, p.741 of textbook");
        System.out.println("What is starting row (0-12): ");
            int rs = keyboard.nextInt();
        System.out.println("What is end row (0-12): ");
            int re = keyboard.nextInt();
        System.out.println("What is start col (0-9): ");
            int cs = keyboard.nextInt();
        System.out.println("What is end col (0-9)");
            int ce = keyboard.nextInt();

        if (rs<0 || rs>12)
        {
            System.out.println( "Must be int between (0-12)");
            rs = keyboard.nextInt();
            if (!keyboard.hasNextInt())
            {
            System.out.println("Must be int between (0-12)");
            keyboard.nextInt();
            }
        }
        if (re<0 || re>12)
        {
            System.out.println( "Must be int between (0-12)");
            re = keyboard.nextInt();
            if (!keyboard.hasNextInt())
            {
            System.out.println("Must be int between (0-12)");
            keyboard.nextInt();
            }
        }
        if (cs<0 || cs>9)
        {
            System.out.println( "Must be int between (0-9)");
            cs = keyboard.nextInt();
            if (!keyboard.hasNextInt())
            {
            System.out.println("Must be int between (0-9)");
            keyboard.nextInt();
            }
        }
        if (ce<0 || ce>9)
        {
            System.out.println( "Must be int between (0-9)");
            ce = keyboard.nextInt();
            if (!keyboard.hasNextInt())
            {
            System.out.println("Must be int between (0-9)");
            keyboard.nextInt();
            }
        }
        keyboard.next();

        char hex; 
        char ascii = 0*20; 
        int row = 2;
        int column;

        System.out.print("\n\n");
        System.out.print("                            ");
        System.out.println("ASCII Table");
        System.out.print("                            ");
        System.out.print("\n    ");

        for (hex = '0'; hex<= '9'; hex++)
          System.out.print("  " + hex);
        for (hex = 'A'; hex<= 'F'; hex++)
          System.out.print("  " + hex);

        System.out.println("\n");

        while (ascii < 0*80) 
        {
          System.out.print("  " + row);
          for (column = 0; column < 16; column++) 
          {
            System.out.print("  " + ascii);
            ascii++;
          }
          System.out.print("\n\n");
          row++;
        }
    }

This line

keyboard.next();

asks for input without a prompt, and the ASCII table doesn't appear yet until something is entered. You can safely remove it.

The hexadecimal literals towards the bottom are not specified correctly. 0*20 is zero times twenty, not a hexadecimal literal. A hexadecimal literal is a zero followed by the letter x , eg 0x20 . Change your two hexadecimal literals from 0*20 and 0*80 to 0x20 and 0x80 , respectively.

With these changes, I get this output:

Table B.1 ASCII Character Set, p.741 of textbook
What is starting row (0-12): 
0
What is end row (0-12): 
12
What is start col (0-9): 
0
What is end col (0-9)
9


                            ASCII Table

      0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F

  2     !  "  #  $  %  &  '  (  )  *  +  ,  -  .  /

  3  0  1  2  3  4  5  6  7  8  9  :  ;  <  =  >  ?

  4  @  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O

  5  P  Q  R  S  T  U  V  W  X  Y  Z  [  \  ]  ^  _

  6  `  a  b  c  d  e  f  g  h  i  j  k  l  m  n  o

  7  p  q  r  s  t  u  v  w  x  y  z  {  |  }  ~  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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