输入一个实数,输出它的平方根值,如果输入小于0,则输出“The number is error!" 编写程序:输入一个实数,如果小于0,显示"无意义",否则计算...
Java\u7f16\u7a0b\uff1a\u8f93\u5165\u4e00\u4e2a\u5b9e\u6570x,\u5982\u679cx\u5c0f\u4e8e0,\u8f93\u51fa"input error";\u5426\u5219\u8f93\u51fax\u7684\u5e73\u65b9\u6839(\u4fdd\u75593\u4f4d\u5c0f\u6570)\u4ee3\u7801\u5982\u4e0b\uff1a
import java.text.DecimalFormat;import java.util.Scanner;public class Test {public static void main(String [] args){Scanner sc = new Scanner(System.in);try{double x = sc.nextDouble();if(x<0){throw new Exception();}double result = Math.sqrt(x);System.out.println(result);DecimalFormat df = new DecimalFormat("#.##"); System.out.println(df.format(result));} catch (Exception e){e.printStackTrace();}sc.close();}}
\u6211\u6765\u4e2aC\u8bed\u8a00\u7684\uff1a
#include
main(){
float x;
scanf("%f",&x);
if (x<0) printf("invalid\n");
else printf("%f\n",sqrt(x));
}
\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a
16
4.000000
-5
invalid
QBASIC\u5440\uff0c\u65e9\u8bf4\u5440\uff1a
INPUT X
IF X<0 THEN
PRINT "\u65e0\u610f\u4e49"
ELSE
PRINT "\u7b97\u672f\u5e73\u65b9\u6839\u662f\uff1a",SQR(X)
END IF
#include <math.h>
int main()
{
double d;
scanf("%lf", &d);
if (d < 0)
{
printf("The number is error!");
}
else
{
printf("%lf", sqrt(d));
}
return 0;
}
绛旓細- 鐗涢】娉曪細渚濇嵁鐗涢】杩唬娉曪紝鍙氳繃浠ヤ笅杩唬鍏紡璁$畻骞虫柟鏍锛歺n+1 = (xn + x / xn) / 2 杩唬鐩村埌xn+1鍜寈n涔嬮棿鐨勫樊寮傚皬浜庢墍闇鐨勭簿搴︺- 浜屽垎娉曪細瀵逛簬涓涓缁欏畾鐨勯潪璐瀹炴暟x锛屾垜浠彲浠ラ氳繃浜屽垎娉曢艰繎鍏跺钩鏂规牴銆傞鍏堬紝鎴戜滑閫夋嫨涓涓乏杈圭晫L鍜屽彸杈圭晫R锛堝彲浠ユ槸浠绘剰姝f暟锛夛紝璁$畻涓棿鍊糓锛屽苟灏嗗叾骞虫柟涓巟...
绛旓細Private Sub Command1_Click()Dim x As Stringx = Abs(InputBox("", "", 3.2))Print Format(x ^ 2, "0.000"), Format(Sqr(x), "0.000"), Format(x ^ 3, "0.000"), Format(x ^ (1 / 3), "0.000")End Sub
绛旓細骞虫柟鏍瑰叕寮忔槸涓绉嶆暟瀛﹀叕寮忥紝鐢ㄤ簬璁$畻涓涓潪璐瀹炴暟鐨勫钩鏂规牴銆傚浜庝竴涓潪璐熷疄鏁 x锛屽畠鐨勫钩鏂规牴璁颁綔 鈭歺銆傚钩鏂规牴鍏紡鍙互琛ㄧず涓猴細鈭歺 = 卤 鈭(x)銆1. 绗﹀彿锛氬钩鏂规牴鐨勮绠椾細浜х敓涓や釜鍙兘鐨勭粨鏋锛屼竴涓鏄鍊硷紝鍙︿竴涓槸璐熷笺傝繖灏辨槸涓轰粈涔堝钩鏂规牴鍏紡涓鏈変竴涓 卤 绗﹀彿锛岃〃绀轰袱绉嶅彲鑳芥с傚鏋 x 鏄...
绛旓細閭d箞蹇呭畾鏈変袱涓紝瀹冧滑浜掍负鐩稿弽鏁般傛樉鐒讹紝濡傛灉鐭ラ亾浜嗚繖涓や釜骞虫柟鏍圭殑涓涓紝閭d箞灏卞彲浠ュ強鏃剁殑鏍规嵁鐩稿弽鏁扮殑姒傚康寰楀埌瀹冪殑鍙︿竴涓钩鏂规牴銆傝礋鏁板湪瀹炴暟绯诲唴涓嶈兘寮骞虫柟銆傚彧鏈夊湪澶嶆暟绯诲唴锛岃礋鏁版墠鍙互寮骞虫柟銆傝礋鏁鐨勫钩鏂规牴涓轰竴瀵瑰叡杞函铏氭暟銆備緥濡傦細-1鐨勫钩鏂规牴涓郝眎锛-9鐨勫钩鏂规牴涓郝3i锛屽叾涓璱涓鸿櫄鏁板崟浣嶃
绛旓細瀵逛簬闈炶礋鏁 x锛屽畠鐨勫钩鏂规牴鍙互鐢ㄤ互涓嬪叕寮忚〃绀猴細鈭歺 = x^(1/2)鍗筹紝灏 x 鐨勬寚鏁拌缃负 1/2锛屽緱鍒 x 鐨勫钩鏂规牴銆傚浜庤礋鏁帮紝浣跨敤澶嶆暟杩涜琛ㄧず锛屽钩鏂规牴鐨勮绠楀叕寮忓涓嬶細鈭(-x) = i 鈭歺 鍏朵腑锛宨 鏄櫄鏁板崟浣嶏紝鍗 i² = -1銆傞渶瑕佹敞鎰忕殑鏄紝瀵逛簬瀹炴暟闈炶礋鏁 x锛屽钩鏂规牴鏈変袱涓鍊硷紝涓涓姝f暟...
绛旓細鐢<math.h>涓殑鍑芥暟锛歞ouble sqrt( double num );include "stdio.h"void main(){ int input;printf("璇疯緭bai鍏ヤ竴涓鏁帮細du")锛泂canf("%d",&input);if(input>0) { printf("缁撴灉鏄細%d"锛宨nput*input)锛泒 else if(input<0) { printf("缁撴灉鏄細%d"锛-input)锛泒 else if(input==0) ...
绛旓細2. 濡傛灉瑕佸澶氫釜鏁板瓧杩涜骞虫柟鏍硅繍绠楋紝璇蜂娇鐢ㄧ被浼肩殑鏂规硶锛氬亣璁捐璁$畻A1鍒癆10鑼冨洿鍐呮墍鏈夋暟瀛楃殑骞虫柟鏍广傚湪B1鍗曞厓鏍间腑杈撳叆浠ヤ笅鍏紡锛= SQRT(A1:A10)鎸変笅鍥炶溅閿悗锛屽皢鍦˙1鍒癇10鑼冨洿鍐呯湅鍒扮浉搴鐨勫钩鏂规牴鍊銆傝娉ㄦ剰锛屽綋琚紑鏂圭殑鏁颁负璐熸暟鏃讹紝SQRT鍑芥暟浼氳繑鍥為敊璇硷紙#NUM!锛夛紝鍥犱负瀹炴暟鐨勫钩鏂规牴涓嶉傜敤浜庤礋鏁般
绛旓細Private Sub Command1_Click()Dim i!Do i = Val(InputBox("杈撳叆涓涓姝瀹炴暟"))Loop While i <= 0 Print "鏁板瓧"; i Print "骞虫柟"; Round(i ^ 2, 3)Print "骞虫柟鏍"; Round(Sqr(i), 3)Print "绔嬫柟"; Round(i ^ 3, 3)Print "绔嬫柟鏍"; Round(i ^ (1 / 3), 3)End Sub ...
绛旓細include<iostream> include<iomanip> using namespace std;int main(){ double a,x;double x0,x1;cout<<"Please input the value : ";cin>>a;if(a>0.0){ x0=a;do { x1=(x0+(a/x0))/2;if(x1>=x0)break;x0=x1;}while(1);x=x0;} else x=a;cout.setf(ios::fixed);cou...
绛旓細Private Sub Form_Click()Dim inputval As Double inputval = InputBox("璇杈撳叆涓涓鏁")Print "骞虫柟鏄:"; inputval * inputval & "骞虫柟鏍鏄: " & Sqr(inputval)End Sub