Homework 6 Code

Homework 6 Code - file/X|/cs101/Homework6_0[6.5 from p204...

This preview shows pages 1–3. Sign up to view the full content.

file:///X|/cs101/Homework6_0.txt /* [6.5 from p204] Write a program that reads in ten numbers and displays distinct numbers (i.e., if a number appears multiple times, it is displayed only once). */ import java.util.Scanner; public class Homework6_0 { public static void main(String[] args) { // Scanner to get user input. Scanner input = new Scanner(System.in); // int array to store user input. int[] numbers = new int[10]; // actual number of ints stored. int actual = 0; for(int i=0; i<10; i++,actual++) { // prompt for an integer then add it to the array. System.out.print("Enter integer #"+(i+1)+": "); numbers[actual] = input.nextInt(); // if what we just entered exists already, decrement the actual counter. for(int j=0; j<actual; j++) if (numbers[j] == numbers[actual]) actual--; } // print out the unique numbers. System.out.println("\nThe following unique numbers were entered:"); for(int i=0; i<actual; i++) System.out.print(numbers[i]+" "); } } file:///X|/cs101/Homework6_0.txt [4/29/2008 12:10:50 PM]

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
file:///X|/cs101/Homework6_1.txt
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/29/2008 for the course CS 101L taught by Professor Vineyard during the Winter '07 term at Kettering.

Page1 / 4

Homework 6 Code - file/X|/cs101/Homework6_0[6.5 from p204...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online