public class p3 { /* This program can find square root of any number that >=0 and will stop when you enter a value that < 0 阿版權是蔡文能的, 歡迎拷貝分享初學者! */ public static void main(String p[ ]) throws java.io.IOException { float onevar; /* 變數名稱隨便我取的啦 */ double ans; /* 用來放答案 */ printf("Find square root of X. Negative value will cease the program\n"); printf("X=? "); /* 還是問"X=?" 比較簡潔有力, 不用說onevar */ onevar = (float) readDouble( ); while(onevar >=0 ) { /*只要onevar大於或等於零我們就一直重複做{...} */ ans = Math.sqrt(onevar); /*其實sqrt的參數也應該為double 型別*/ ans = ((int)(ans*1000+0.5))/1000.0; printf(" SQRT(" + onevar + ")=" + ans+ "\n"); printf("X=? "); onevar = (float)readDouble( ); } /* while(onevar */ } // main static void printf(String s) { System.out.print(s); } static float readDouble( ) throws java.io.IOException { java.io.InputStreamReader isr = new java.io.InputStreamReader(System.in); java.io.BufferedReader br = new java.io.BufferedReader(isr); return (float)Double.parseDouble( br.readLine( ) ); } // readDouble }