Write out Java code that generates the following number sequence using a while l
ID: 3773948 • Letter: W
Question
Write out Java code that generates the following number sequence using a while loop and saves them in an array called myNumbers. The program should then print out all the entries in the array myNumbers. You are given an array of strings called letters and contains the following: {"t", "h", "i", "s", :", "i", "s", "", "a", "", "m", "e", "s", "a", "g", "e", "1", "2" "3", "4", "5"} (the blank entries are space characters). Using a combination of 1 or more while loop, System.out.println and System.out.print statements, write a Java program that will output the following (as read from the array letters). Note that this output is on 2 (and only 2) separate lines:Explanation / Answer
Question 1:
Answer:
DisplauNumbers.java
public class DisplauNumbers {
public static void main(String[] args) {
int myNumbers[]= new int[16];
int i=0;
int oddIndexValue = 6;
int evenIndexValue = 1;
while(i<myNumbers.length){
if(i % 2 == 0){
myNumbers[i] = evenIndexValue;
evenIndexValue = evenIndexValue + 2;
}
else{
myNumbers[i] = oddIndexValue;
oddIndexValue = oddIndexValue + 2;
}
i++;
}
System.out.println("Array elements are: ");
i=0;
while(i<myNumbers.length){
System.out.print(myNumbers[i]+" ");
i++;
}
}
}
Output:
Array elements are:
1 6 3 8 5 10 7 12 9 14 11 16 13 18 15 20
Question 2
Answer:
DisplayString.java
public class DisplayString {
public static void main(String[] args) {
String letters[] = {"t","h","i","s"," ","i","s"," ","a"," ","m","e","s","s","a","g","e","1","2","3","4","5"};
int i=0;
while(i<letters.length){
if(letters[i] == "1"){
System.out.println();
}
System.out.print(letters[i]);
i++;
}
}
}
Output:
this is a message
12345
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.