close

(Chapter 3) 設計程式,給使用者一次又一次輸入整數,直到使用者輸入0代表輸入結束。此時,列出之前所輸入的最大整數、最小整數、所有輸入整數的加總及平均值。

 

阿聰很有義氣的睡死了,我只好拿命來換!

有別於使用一個大陣列來實現沒有腦殘會真的去輸入1萬個整數的方式

我選擇嘗試使用串列來實現無限個整數..給大家參考用!

 

 

 

import java.util.*;
import java.io.*;

class no
{
  int num;
  no link;
 
  no()
  {
    link = null;
  }

  no(int num)
  {
    this();
    this.num = num;
  }
}

public class Sequence
{
  public static void main(String[] args)
  {
    Scanner sc = new Scanner(System.in);
    int max, tmp;
    double averg;
    double count = 0.00;
    double sum = 0.00;

    no top = new no();
    no p = new no();
    no n;
    
    do
    {
      System.out.printf("plz input a integer(or enter 0 to quit!) : ");
      int x = sc.nextInt();
      
      n = new no(x);
      n.link = top.link;
      top.link = n;

      count++;     
    }while(n.num != 0);

    count = count - 1;

    n = top.link;
    p = top.link;
    while(p != null)
    {
      
      while(n.link != null)
      {   
           if(n.num > n.link.num)
           {
             tmp = n.num;
             n.num = n.link.num;
             n.link.num = tmp;
           }
        n = n.link;
      }
          
      p = p.link;
      n = top.link;
    }
   
    n = top.link;
    System.out.printf("MIN number is : %d\n",n.link.num);
    
    while(n != null)
    {
      if(n.link == null)  System.out.printf("MAX number is : %d\n",n.num);
      n = n.link;
    }
 
    n = top.link;
    while(n != null)
    {
      sum = sum + n.num;
      n = n.link;
    }
    System.out.printf("sum of the sequence is : %f\n",sum);
    averg = sum / count;
    System.out.printf("averg of the sequence is : %.2f\n",averg);
  }
}

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 Icen Zhong 的頭像
    Icen Zhong

    無止盡的Coding地獄

    Icen Zhong 發表在 痞客邦 留言(0) 人氣()