Prev | Current Page 499 | Next

Carl Reynolds and Paul Tymann

"Schaum's Outline of Principles of Computer Science"

");
}
}
}
Enter an integer: 115
115 is a multiple of 5.
5.4 Write a Java program that asks a user to enter five Strings, one at a time. Have it save the Strings in an
array of strings. Then have the program display the words in reverse order. Use a for, or a while, or a do
while loop to read in the Strings, and another for, while, or do while loop to print them out.
import java.util.Scanner;
public class FiveStrings {
//A Java program to read and display 5 Strings
public static void main( String[] args ) {
Scanner sc = new Scanner(System.in);
String[] stringList = new String[5];
for( int i=0; i< 5; i++ ) {
System.out.print( "Enter a String: " );
stringList[i] = sc.nextLine();
}
System.out.println( "In reverse order:" );
int n = 4;
while( n >= 0 ) {
System.out.println( stringList[n] );
n--;
}
}
}
5.5 Write a Java program that can categorize vehicles based on the number of wheels the vehicle has.
Your program should prompt the user for the number of wheels on the vehicle, and then read the number
into an int variable. If the user says the vehicle has 2 or 3 wheels, the program will report that it is a
motorcycle, if it has 4 wheels the vehicle will be labeled a ???car or light truck,??? if it has 6, 8, 10, 12, 14,
16, or 18 wheels, it will be categorized as a truck.


Pages:
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
wyciągarki wróżby miłosne cukiernia-stykowscy.pl salon kosmetyczny kraków pompy ciepła