2005-10-30

Lab Max-Min

/*用當初學習C語言的邏輯觀念來寫此程式,首次在Java中使用陣列 剛開始遇到一些障礙,因為Java的使用陣列語法似乎跟C語言不太一樣 後來翻閱了一下書籍,參閱了上面的寫法,更改程式如下*/
package maxmin;
import javax.swing.JOptionPane;
public class MaxMin
{
public static void main (String[] args)
{
int i,j,n[],count,max,min;
//如果直接打上數字,ex:n[6],無法正常執行
n= new int[6];
//沒有打這一行,就無法正常執行
for(i=0;i<=5;i++)
{
String number=JOptionPane.showInputDialog("Please enter the "+(i+1)+" of 6 nunnegative numbers.");
n[i]= Integer.parseInt(number);
}
for(i=0;i<=5;i++)
{
count=0; max=n[i]; for(j=0;j<=5;j++)
{
if (max>=n[j])
count=count+1;
}
if (count==6)
System.out.println("Max="+max+"");
//當某數大於等於其他六個數時,則為Max
count=0;
min=n[i];
for(j=0;j<=5;j++)
{
if (min <= n[j]) count = count + 1;
}
if (count==6) System.out.println("Min="+min+"");
//當某數小於等於其他六個數時,則為Min
}
}
}

0 Comments:

張貼留言

<< Home