编程语言
首页 > 编程语言> > Java语言基础训练

Java语言基础训练

作者:互联网

写一个程序,在main方法里面定义一个数组,输出排序前的数组,将数组中的元素从小到大进行排序,输出排序后的数组。

package ZUOYE;
import java.util.Scanner;
public class A1 {
	 public static void QuickSort(int num[],int n){
		 int i,j,temp;
		 for(i = 0;i < n;i ++){
			 for(j = i + 1;j < n;j ++)
				 if(num[i] > num[j]){
					 temp = num[i];
					 num[i] = num[j];
					 num[j] = temp;
				 }
		 }
	 }
	 public static void QuickSort(double num[],int n){
		 int i,j;
		 double temp;
		 for(i = 0;i < n;i ++){
			 for(j = i + 1;j < n;j ++)
				 if(num[i] > num[j]){
					 temp = num[i];
					 num[i] = num[j];
					 num[j] = temp;
				 }
		 }
	 }
	 public static void main (String args[ ]) {
		 int n,i,m;
		 Scanner reader = new Scanner(System.in);
		 System.out.println("要输入的元素数量为(数量少于10):");
		 n = reader.nextInt();
		 System.out.println("要输入的元素是否有小数(有则为1,无则为2):");
		 m = reader.nextInt();
		 if(m == 1){
			 double[] num;
			 num = new double[10];
			 System.out.printf("请输入%d个元素:\n",n);
			 for(i = 0;i < n; i ++)
				 num[i] =  reader.nextDouble();
			 System.out.println("排序前的元素数组为:");
			 for(i = 0;i < n; i ++)
				 System.out.printf(num[i] + " ");
			 QuickSort(num,n);
			 System.out.println("\n排序后的元素数组为:");
			 for(i = 0;i < n;i ++)
				 System.out.printf(num[i] + " ");
		 }
		 else if(m == 2){
			 int[] num;
			 num = new int[10];
			 System.out.printf("请输入%d个元素:\n",n);
			 for(i = 0;i < n; i ++)
				 num[i] =  reader.nextInt();
			 System.out.println("排序前的元素数组为:");
			 for(i = 0;i < n; i ++)
				 System.out.printf(num[i] + " ");
			 QuickSort(num,n);
			 System.out.println("\n排序后的元素数组为:");
			 for(i = 0;i < n;i ++)
				 System.out.printf(num[i] + " ");
		 } 
	 }
}

写一个方法,用来计算三角形的面积(参数为三个边长,实型,由键盘输入,面积计算用海伦公式:,p为三角形的半周长,使用方法Math.sqrt(),请查询JDK帮助文档,java.lang.Math类里面定义的方法),在main方法里面调用该方法,输出面积计算的结果。

package ZUOYE;
import java.lang.Math;
import java.util.Scanner;
public class A1 {
	 public static double Triang(double length,double width,double height){
		 double p = (length + width + height)/2;
		 return	 Math.sqrt(p * (p - length) * (p - width) * (p - height));
	 }
	 public static void main (String args[ ]) {
		 Scanner reader = new Scanner(System.in);
		 double length;
		 double width;
		 double height;
		 System.out.println("请分别输入三角形的长,宽,高:");
		 length = reader.nextDouble();
		 width = reader.nextDouble();
		 height = reader.nextDouble();
		 System.out.println("三角形的面积为:" + Triang(length,width,height));
	 }
}

使用一个二维数组存放如下图形的字符

*

package ZUOYE;
import java.lang.Math;
import java.util.Scanner;
public class A1 {
	 public static void main (String args[ ]) {
		 int row = 0;
		 int line = 0;
		 int m = 0;
		 char[][] s = {{'*'},{'*','*','*'},{'*','*','*','*','*'},{'*','*','*','*','#','*','*'}};
         for(int i = 0;i < s.length;i ++)
        	 for(int j = 0;j < s[i].length;j ++)
        	 if(s[i][j] == '#'){
        		 row = i;
        		 line = j;
        		 m = 1;
        	 }
         if(m == 1)
        	 System.out .println ("#的位置下标为" + "[" + row + "]" + "["+ line + "]" );
         else
        	 System.out .println ("#不在该图形里!");
	 }
}

* * *

* * * * *

* * * * # * *

找到字符#,并打印出它在二维数组中的下标。

标签:Java,语言,int,double,System,++,基础训练,num,out
来源: https://blog.csdn.net/ywd171/article/details/121526668