會員登入 新使用者?立即註冊

此資料只供保存、查詢,不在更新......

關閉

計算輸入之總和、平均最大最小值

分類:Java作業
2005/12/10 01:38

/*設計程式,輸入數字,一直到輸入的數字為0才結束,然後顯示輸入數字的總和、平均值、最大值和最小值 */

import java.io.*;

public class ccc {
 public static void main(String[] args) throws IOException {
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

  String str;
  int x; // 輸入的數
  int sum = 0; // 總和
  int y = 0; // 輸入數的個數
  int max = 0; // 最大值
  int min = Integer.MAX_VALUE; // 最小值
  do {
   System.out.print("請輸入一個數字:");
   str = br.readLine();
   x = Integer.parseInt(str);
   System.out.println("您輸入的數字是:" + x);
   if (max < x)
    max = x;
   if (min > x && x!=0)
    min = x;
   y++;
   sum = sum + x;
  } while (x != 0);
  System.out.println("您輸入的總和是:" + sum);
  System.out.println("您輸入的最大值:" + max);
  System.out.println("您輸入的最小值:" + min);
  System.out.println("您輸入的總和平均:" + (sum / (y - 1)));

 }
}

輸出結果:

請輸入一個數字:10
您輸入的數字是:10
請輸入一個數字:20
您輸入的數字是:20
請輸入一個數字:30
您輸入的數字是:30
請輸入一個數字:40
您輸入的數字是:40
請輸入一個數字:50
您輸入的數字是:50
請輸入一個數字:60
您輸入的數字是:60
請輸入一個數字:70
您輸入的數字是:70
請輸入一個數字:80
您輸入的數字是:80
請輸入一個數字:90
您輸入的數字是:90
請輸入一個數字:100
您輸入的數字是:100
請輸入一個數字:0
您輸入的數字是:0
您輸入的總和是:550
您輸入的最大值:100
您輸入的最小值:10
您輸入的總和平均:55

 

附註: 差別在 min 的初始值..如果一開就給0 ,就己經是最小值了,再跟誰比還是最小,所以永遠是0

這裡我把 min 給了Integer的最大值給它,較為適合 , 另外在 if 的判別過程只要 x輸入不為0 ,if迴圈才會執行,如果為0,if迴圈不執行...

12:00要交...我1:30才看到..不知還來的及嗎....

上一篇 下一篇
回應(7)
意 • 可達2011/03/10 00:00 回應

此則為私密回應

意 • 可達2011/03/09 23:54 回應

呃   不好意思
我問錯題了= =

課堂上老師只有教我們寫要如何印出 hello world
回家作業就要求...
從使用者鍵入五個數字
分別求出最大值、最小值和平均

我有看著書寫
可以鍵入數字也有平均
但是最大、最小值
我不知道java的參數要邏輯要怎麼寫...


意 • 可達2011/03/09 23:46 回應

不好意思,我想請你幫我解題目。
我這學期才剛開始上java課程...

出題老師叫我們用

int calcWeekDay(int year, int month, int day){ //計算星期幾

 int TempYear, TempMonth, i, j;
  if (month >= 3)
  {
   TempMonth = month - 2;
   TempYear = year;
  } else
  {
   TempMonth = month + 10;
   TempYear = year - 1;
  }

  j = TempYear + (int)(TempYear / 4) - (int)(TempYear / 100) +
   (int)(TempYear / 400) + (int)(2.6 * TempMonth - 0.2) + day;

  i = j - 7*(int)(j / 7) + 1;
 
  return i;
}

這個函式算出2011年3月1日是星期二

要由主程式去呼叫

並且從星期二開始往下印日期(就是日歷格式)


我有大概寫出來...

#include
#include
using namespace std;
int calcWeekDay(int year, int month, int day){ //計算星期幾

 int TempYear, TempMonth, i, j;
  if (month >= 3)
  {
   TempMonth = month - 2;
   TempYear = year;
  } else
  {
   TempMonth = month + 10;
   TempYear = year - 1;
  }

  j = TempYear + (int)(TempYear / 4) - (int)(TempYear / 100) +
   (int)(TempYear / 400) + (int)(2.6 * TempMonth - 0.2) + day;

  i = j - 7*(int)(j / 7) + 1;
 
  return i;
}

int main()
{
    int a=2011,b=3,c=1,d,e;
    cout<<"西元 2011 年 3 月\n";
    cout<<"\n日 一 二 三 四 五 六\n";
    d=calcWeekDay(a,b,c);
    for(e=1;e<=31;e++)
    {
       if( e
        cout<<"   ";
       else
         printf("%3d",c++);
       if(e%7==0)
         cout<<"\n";          
    }
       

    system("pause");
}


可以執行,但答案是錯的    

如果要延伸印出一整年的話,要如何改?

請你幫我看一下  萬分感激,謝謝

2006/12/21 12:45 回應

你好:

看到你的程式真的好厲害唷~~我想請你幫我解答這兩題~因為是剛學的..很多都不懂~正在學習中!!

1.要求使用者輸入3個整數~請印出其中的最大跟最小值!

2.要求使用者輸入3個整數~請將該3數由小到大順序印出!(是不同題目)


public class nnn{
public static void main( String args[] ){
int x;
x=Integer.paseInt(in.readLine());
if(x>=90 && x<=100){
System.out.printlin("a")}
if(x>=80 && x<=89){

System.out.println("b")}

}
}
}
}

阿宗2006/10/09 03:48 回應

剛想出來怎麼解了,雖然是笨方法,但還是算的出來啦~還是謝謝啦

      if(num1<num2)
      {
         if(num1<num3)
          System.out.println(num1+"是最小值");
          else
          {
             if(num3<num2)
             System.out.println(num3+"是最小值");
          }
      }
      else
      {
       if(num2<num3)
          System.out.println(num2+"是最小值");
       else
       System.out.println(num3+"是最小值");
      }
            if(num1>num2)
      {
         if(num1>num3)
          System.out.println(num1+"是最大值");
          else
          {
             if(num3>num2)
             System.out.println(num3+"是最大值");
          }
      }
      else
      {
       if(num2>num3)
          System.out.println(num2+"是最大值");
       else
       System.out.println(num3+"是最大值");
      }  

阿宗2006/10/09 02:18 回應

Hi 迷途的羔羊你好

看了你的文章,了解您是一個java高手

以下有一個java程式,想請你幫我修改一下,我已經可以算出總和、平均值、乘積

但不會算最大值及最小值,麻煩可以幫我修改一下,最好再幫我註解,好讓我了解,3Q

---------------------------------------------------------

// app3_15,輸入三個整數,顯示數字的總和、平均值、乘積,最小值與最大值
import java.io.*;
public class app3_15
{  
   public static void main(String args[]) throws IOException
   {
      int num1,num2,num3;
      String str1,str2,str3;
      BufferedReader buf;
     
      buf=new BufferedReader(new InputStreamReader(System.in));
   
      System.out.print("請輸入第一個整數: ");
      str1=buf.readLine();        // 將輸入的文字指定給字串變數str1
      num1=Integer.parseInt(str1); // 將str1轉成int型態後給num1存放
     
      System.out.print("請輸入第二個整數: ");
      str2=buf.readLine();        // 將輸入的文字指定給字串變數str2
      num2=Integer.parseInt(str2); // 將str2轉成int型態後給num2存放
     
      System.out.print("請輸入第三個整數: ");
      str3=buf.readLine();        // 將輸入的文字指定給字串變數str3
      num3=Integer.parseInt(str3); // 將str3轉成int型態後給num3存放
     
      System.out.println(num1+","+num2+","+num3+"的總和="+(num1+num2+num3));
      System.out.println(num1+","+num2+","+num3+"的平均值="+(((float)num1+num2+num3)/3));
      System.out.println(num1+","+num2+","+num3+"的乘積="+(num1*num2*num3));
      }
}

小若2005/12/10 16:50 回應

哈!真的很謝謝你~

雖然來不及,可是你讓我了解了!

真的很謝謝你>w<*

原來是一開始設錯了!

真的很謝謝你唷~你的程式真的很厲害呢!

引用(0)
雅虎資訊 版權所有 © 2013 Yahoo! Taiwan All Rights Reserved
「本服務設有管理員」 服務條款 隱私權政策