例3.1 求一元二次方程的根
1 import java.util.Scanner; 2 public class cjava { 3 public static void main(String[] args) { 4 float a,b,c,x1,x2; 5 Scanner s=new Scanner(System.in); 6 System.out.println("输入a,b,c的值:"); 7 a=s.nextInt(); 8 b=s.nextInt(); 9 c=s.nextInt(); 10 x1=(float)((-b+Math.sqrt(b*b-4*a*c))/(2*a)); 11 x2=(float)((-b-Math.sqrt(b*b-4*a*c))/(2*a)); 12 System.out.println("x1="+x1); 13 System.out.println("x2="+x2); 14 } 15 }
例3.2求三角形面积
1 import java.util.Scanner; 2 import java.lang.*; 3 public class cjava { 4 public static void main(String[] args) { 5 float a,b,c,x1,x2; 6 double s,area; 7 Scanner g=new Scanner(System.in); 8 System.out.println("输入a,b,c的值:"); 9 a=g.nextInt(); 10 b=g.nextInt(); 11 c=g.nextInt(); 12 if(a+b>c&&b+c>a&&a+c>b) 13 { 14 s=(a+b+c)/2; 15 area=Math.sqrt(s*(s-a)*(s-b)*(s-c)); 16 System.out.println("area="+area); 17 } 18 else 19 System.out.println("it is not a trilateral!"); 20 } 21 }
例3.3判断字符大小写,大写转换为小写,小写不转换
1 import java.util.Scanner; 2 public class cjava { 3 public static void main(String[] args) { 4 char ch; 5 Scanner g=new Scanner(System.in); 6 System.out.println("输入ch:"); 7 String s; 8 s=g.next(); 9 ch=s.charAt(0); 10 if(ch>='A'&&ch<='Z') 11 { 12 ch+=32; 13 System.out.println(ch); 14 } 15 else 16 System.out.println(ch); 17 } 18 }
2.今天不知道怎么用数学函数,百度查询到java.lang
3明天继续写例题