Homework 6 Code

Homework 6 Code - file:/X|/cs101/Homework6_0.txt /* [6.5...

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

View Full Document Right Arrow Icon
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]
Background image of page 1

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

View Full DocumentRight Arrow Icon
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

Homework 6 Code - file:/X|/cs101/Homework6_0.txt /* [6.5...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online