我叫王超然,是一名电脑爱好者,现在在新加坡留学上高一.我立志成为一名电脑人才,愿意在这里与大家一同分享我玩转电脑的心得.O-level华文考了A-One哈哈!
天气: 晴朗
心情: 高兴
/*Filename:P6Q05
* Name:Wang Chaoran
Description:5 (Printing distinct numbers)
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).
Hint: Read a number and store it to an array if it is new.
If the number is already in the array, discard it. After the input,
the array contains the distinct numbers.
*/
import java.util.Scanner;
class P6Q05{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
double[] input = new double[10];
int count = 1;
double number = 0;
System.out.println("Force you to enter 10 distinctive numbers: ");
System.out.println("Enter number "+1);
input[0] = scan.nextDouble();
while(count<=9){
System.out.println("Enter number "+(count+1));
number = scan.nextDouble();
boolean judge = true;
for(int i=0;i<=count;i++){
if(number==input[i]){
System.out.println("Your number is repeated! Discard!");
judge=false;
}
}
if(judge){
input[count]=number;
count++;
}
}
System.out.println("10 distinct numbers are:");
for(int i =0;i<input.length;i++)
System.out.print(input[i]+" ");
}
}
导入论坛查看(50)回复(0)引用(0)好评(0) 差评(0)
加入收藏
编辑
审核
TAG:
computing