I am trying to fill an array with the input words by the user. Each word should be one letter lower than the previous and one letter less than the next one. Their length is equal to the table row index, counting from 2. Words end up making a one-way pyramid, such as:
A
AB
ABC
ABCD
scanner sc = new scanner (System.in); System.out.println ("height of the array:"); Height = sc.nextInt (); String [] word = new string [height]; For (int i = 2; i i-2 || words [i-2] .length () & lt; term [i-3] .length ()) {words [i-2 ] = Sc.next (); }}
How can I limit the readings from the scanner to fill the requirements? Currently loop does not affect the scanner exactly: /
This is not the text of the house. I am trying to create a simple application and then GUI for it.
- read
height
toscanner
(What is the price?) - Are you sure you want to
list & lt; String & gt;
and other dynamically mountable data structures? - What if the length requirement is not met?
- Why
2
and-2
offset?- When
i = 2
, you can also reach theword [i-3]
. Which will throwArrayIndexOutOfBoundsException
This is a rewrite that clarifies the argument:
scanner sc = New Scanner (System.in); Println ("height?"); While (Sc.hasNextInt ()) {System.out.println ("int, please!"); Sc.next (); } Last int n = sc.nextInt (); String [] arr = new string [n]; {String S; For Int L = 1; L & lt; = N; L ++} Do {system.out.println ("length" + L + ", please!"); S = sc.next (); } While (s.length ()! = L); Arrival [L - 1] = S; } (String: arr) {System.out.println (s); }
Related Questions
- When
Comments
Post a Comment