Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Jacob Dixon
- *October 10, 2012
- *INF 260-002
- *Program 3: For Loop, Do-While Loop, Array, Switch Statements
- *
- */
- import java.util.Scanner;
- public class prog3 {
- public static void main(String[] args) {
- long [] numbers = new long [20];
- Scanner input = new Scanner(System.in);
- System.out.println( "Please select an option:\n0 - initialize array\n1 - square array\n2 - reverse array\n3 - accumulate array\n4 - halve array\n5+ - to quit" );
- int cmd = input.nextInt();
- do{
- switch( cmd ){
- case 0: //Initialize array
- System.out.println( "Initially the array contains:" );
- for( int x = 0; x < numbers.length; x++ )
- {
- numbers[x] = x;
- System.out.println( numbers[x] );
- }
- break;
- case 1: //Squares array
- long square = 0;
- for( int x = 0; x < numbers.length; x++ )
- {
- square = numbers[x]*numbers[x];
- numbers[x] = square;
- System.out.println( "Now the array contains:\n" + numbers[x] );
- }
- break;
- case 2: //Reverse array
- for( int x = 0; x < numbers.length/2; x++ )
- {
- long temp = numbers[x];
- numbers[x] = numbers[numbers.length-x-1];
- numbers[numbers.length-x-1] = temp;
- System.out.println( "Now the array contains:\n" + numbers[x] );
- }
- break;
- case 3: //Accumulate array
- long sum = 0;
- for( int x = 0; x < numbers.length; x++ )
- {
- sum = sum+numbers[x];
- numbers[x] = sum;
- System.out.println( "Now the array contains:\n" + numbers[x] );
- }
- break;
- case 4: //Halve array
- long halve = 0;
- for( int x = 0; x < numbers.length; x++ )
- {
- halve = numbers[x]/2;
- numbers[x] = halve;
- System.out.println( "Now the array contains:\n" + numbers[x] );
- }
- break;
- default: //Quit the array
- System.out.println( "Quitting the array." );
- break;
- }
- }while( cmd < 5 && cmd > -1);
- }
- }
Add Comment
Please, Sign In to add comment