Java程序设计基础练习50题(中)


Java程序设计基础练习50题(上)
Java程序设计基础练习50题(下)

3 程序调试

E201_03 _01评委评分

面的程序存在错误,请调试并排除错误

public static void main(String[] args) {
System.out.println(“请依次输入5个评委的评分在(0-100):”);
Scanner scan = new Scanner(System.in);
int score1 = scan.nextInt();
int score2 = scan.nextInt();
int score3 = scan.nextInt();
int score4 = scan.nextInt();
int score5 = scan.nextInt();
int min = 100;
if(score1 < min){
min = score1;
}
if(score2 < min){
min = score1;
}
if(score3 < min){
min = score1;
}
if(score4 < min){
min = score1;
}
if(score5 < min){
min = score1;
}
int max = 0;
if(score1 > max){
max = score1;
}
if(score2 > max){
max = score1;
}
if(score3 > max){
max = score1;
}
if(score4 > max){
max = score1;
}
if(score5 > max){
max = score1;
}
int total = score1+score2+score3+score4+score5;
float ave = (total-max-min)/5;
System.out.println("");
}

package e201_03_01;
import java.util.Scanner;

public class MamberGrade {
	public static void main(String[] args) {
	    System.out.println("请依次输入5个评委的评分在(0-100):");
	    Scanner scan = new Scanner(System.in);
	    int score1 = scan.nextInt();
	    int score2 = scan.nextInt();
	    int score3 = scan.nextInt();
	    int score4 = scan.nextInt();
	    int score5 = scan.nextInt();

	    int min = 100;
	    if(score1 < min){
	        min = score1;
	    }
	    if(score2 < min){
	        min = score2;
	    }
	    if(score3 < min){
	        min = score3;
	    }
	    if(score4 < min){
	        min = score4;
	    }
	    if(score5 < min){
	        min = score5;
	    }
	    int max = 0;
	    if(score1 > max){
	        max = score1;
	    }
	    if(score2 > max){
	        max = score2;
	    }
	    if(score3 > max){
	        max = score3;
	    }
	    if(score4 > max){
	        max = score4;
	    }
	    if(score5 > max){
	        max = score5;
	    }
	    int total = score1+score2+score3+score4+score5;
	    float ave = (float) ((total-max-min)/3.0);

	    //System.out.println("");
	    System.out.println(ave);
	}
}

E201_03 _02判断水仙花数

题目要求:下面的程序存在错误,请调试并排除错误

public static void main(String[] args) {
System.out.println(“请输入一个三位数:”);
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
if(num<100 && num>999){
System.out.println(num+“不是一个三位数”);
return;
}
int b100 = num/100;
int b10 = num%100/10;
int b1 = num%10;
boolean result = b100b100b100+b10b10b10+b1b1b1 == num;
if(result = true){
System.out.println(num+“是水仙花数”);
}else{
System.out.println(num+“不是水仙花数”);
}
}

package e201_03_02;

import java.util.Scanner;

public class shuixianhau {

	public static void main(String[] args) {
	    System.out.println("请输入一个三位数:");
	    Scanner scan = new Scanner(System.in);
	    int num = scan.nextInt();

	    if(num<100 && num>999){
	        System.out.println(num+"不是一个三位数");
	        return;
	    }

	    int b100 = num/100;
	    int b10 = num/10%10;
	    int b1 = num%10;

	    boolean result = b100*b100*b100+b10*b10*b10+b1*b1*b1 == num;
	    if(result == true){
	        System.out.println(num+"是水仙花数");
	    }else{
	        System.out.println(num+"不是水仙花数");
	    }
	}

}

4 循环

E201_04_01存钱翻倍

假定银行一年期存款利率为3%,到期后自动转存,问一万元需要连续存多少年才能超过2万元

package e201_04_01;

public class moneydouble {

	public static void main(String[] args) {
		double money=10000;
		int n = 0;
		while(money<20000) {
			money=money*1.03;
			n=n+1;
		}
		System.out.println("Need to keep it for "+n+" years");
	}

}

E201_04_02统计法计算圆周率

随机数法求解π,向正方形中扔点,如果落在圆内在计数,重复这个过程,然后用该计数除以总次数就是π的四分之一。

package e201_04_02;

public class MoncatelloPI {
	public static void main(String[] args) {
		int n=100000;
		double x=0.0;
		double y=0.0;
		double pi1=0.0;
		double in=0;
		for (int i=0;i<n;i++) {
			x=Math.random();
			y=Math.random();
			if(Math.sqrt(x*x+y*y)<1.0) {
				in++;
			}
		}
		pi1=4*in/n;
		System.out.println("The pi is "+pi1);
	}
}

E201_04_03_计算数列之和

编程计算数列的前20项之和:1/2,2/3,3/5,5/8……

package e201_04_03;

public class seriesSummation {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		double n = 0;
		double x = 1.0;
		double y = 2.0;
		double sum = 0;
		while(n<20.0) {
			sum=sum+x/y;
			double t = y;
			y = x+y;
			x = t;
			n=n+1;
		}
		System.out.println("The sum of the series is "+sum);

	}

}

E201_04_04_输出所有水仙花数

package e201_04_04;

public class narcissusNumber {
	public static void main(String[] args) {
		for (int i = 100; i <1000 ; i++) {
			int firstNum = i/100;
			int secondNum = i/10%10;
			int thirdNum = i%10;
			if(Math.pow(firstNum, 3) + Math.pow(secondNum, 3)+Math.pow(thirdNum, 3) == i){
				System.out.println("The narcissus number is:" + i);
			}
		}
	}
}

E201_04_05验证角谷定理

任意一个自然数,若为偶数则除以2,若为奇数则乘以3加1,得到新的自然数,经过若干次这样演算,最终结果必然为1。要求显示验证的步骤。

package e201_04_05;

import java.util.Scanner;

public class ValleyAngleTheorem {
	static int i = 0;
	@SuppressWarnings("resource")
	public static void main(String[] args) {
		System.out.println("please input a number:");
		Scanner sca = new Scanner(System.in);
		int n = sca.nextInt();
		int i = count(n);
		System.out.println("step=" +i);
	}

	public static int count(int n) {
		if (n == 1) {
			System.out.println(n);
			i++;
		}else if (n % 2 == 0) {
			System.out.println(n);
			count(n / 2);
			i++;
		}else {
			System.out.println(n);
			count(n * 3 + 1);
			i++;
		}
		return i;
	}

}

E201_04_06 输出100以内的素数

package e201_04_06;

public class PrimeNumber {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int n=100;
		int i=0;
		for(i=2;i<n;i++) {
			boolean flag=true;
			for (int j = 2; j < i/2+1; j++) {
				if (i%j==0) {
					flag=false;
				}
			}
			if (flag==true) {
				System.out.println(i+" is prime");
			}
		}
	}
}


E201_04_07九九乘法表

package e201_04_07;

public class multiplicationTable {
	public static void main(String[] args){
		for(int i=1;i<=9;i++){
			for(int j=1;j<=i;j++){
				System.out.print(j+"*"+i+"="+(i*j)+"  ");
			}
			System.out.println();
		}
	}
}

E201_04_08_输出1000以内的完数

package e201_04_08;

public class perfectNumber {
	public static void main(String[] args) {
		
		for(int i=2;i<1000;i++) {
			int sum=0;
			for(int j=1;j<i;j++) {
				if(i%j==0) {
					sum+=j;
				}
			}
			if(sum==i) {
				System.out.print(sum + " ");
			}
		}
		
	}
}

5 数组

E201_05_01_成绩统计

根据学生的考试成绩,计算最高分、最低分、平均分和标准差。

package e201_05_01;

import java.util.Arrays;
import java.util.Scanner;

public class grade {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		@SuppressWarnings("resource")
		Scanner sca = new Scanner(System.in);
		System.out.println("please input numbers of student:");
		int n= sca.nextInt();double[] score = new double[n];
		double sum=0;double total=0;
		for (int i=0;i<score.length;i++) {
			System.out.print("please input grade of sutdent");
			score[i]=sca.nextDouble();sum+=score[i];
		}
		Arrays.sort(score);
		double avg=sum/n;double max=score[n-1];double min=score[0];
		for(int i=0;i<score.length;i++){
		    total += (score[i]-avg)*(score[i]-avg);
		}
		double sta = Math.sqrt(total/score.length);
		System.out.println("Top score is "+max+" ,lowest mark is "+min+" ,average score is "+avg+" , standard deviation is "+sta);
	}

}

E201_05_02_数组交集

package e201_05_02;

import java.util.ArrayList;

public class arrayOverlap {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String[] arr1 = { "1", "2", "3" };
		String[] arr2 = { "4", "5", "3" };
        System.out.println("The intersection of the array is:"+retain(arr1,arr2)); 
        
	}

	private static ArrayList<Object> retain(String[] a, String[] b){
		ArrayList<Object> a1 = new ArrayList<>();
        ArrayList<Object> b1 = new ArrayList<>();
		for(String str : a) {
			a1.add(str);
		}
		for(String str : b) {
			b1.add(str);
		}
		b1.retainAll(a1);//求交集
		return b1;	
	}
}

E201_05_03_数组并集

package e201_05_03;
import java.util.HashSet;
import java.util.Set;
 
public class arrayUnion {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String[] arr1 = { "1", "2", "3" };
        String[] arr2 = { "4", "5", "6" };
        System.out.println("The union of arrays is:"+union(arr1, arr2));
	}
	private static Set<Object> union(String[] arr1, String[] arr2) {
		Set<Object> m=new HashSet<>();
		for (String str : arr1) {
            m.add(str);
        }
        for (String str : arr2) {
            m.add(str);
        }
        return m;
    }

}

E201_05_04_超大数相加

长整型数据为32位,能表示的最大值为232-1,但是在很多情况下是不够用的,需要程序设计者自行解决这个问题。例如可以通过数组存储超长整数的每一位数值,对应位相加,如果超过9则产生进位。这里假设运算数据和结果都不超过30位。

package e201_05_04;

import java.math.BigDecimal;
import java.util.Scanner;

public class largeNumber {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		@SuppressWarnings("resource")
		Scanner sca = new Scanner(System.in);
        String s1 = sca.nextLine();
        String s2 = sca.nextLine();
        BigDecimal b1 = new BigDecimal(s1);
        BigDecimal b2 = new BigDecimal(s2);
        System.out.println("The sum of the large numbers is:"+b1.add(b2));
    }

}

E201_05_06_矩阵转置

输出用户输入的矩阵,输出其对应的转置矩阵。

package e201_05_06;

import java.util.Arrays;
import java.util.Scanner;

public class matrixTranspose {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		@SuppressWarnings("resource")
		Scanner sca = new Scanner(System.in);
		System.out.println("The rank of the matrix is:");
		int n = sca.nextInt();
		int a[][] = new int[n][n];
		System.out.println("Please input a matrix:");
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				a[i][j] = sca.nextInt();
			}
		}
		int b[][] = new int[n][n];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				b[i][j] = a[j][i];
			}
		}
		System.out.println("This matrix is:");
		for(int i=0; i<a.length; i++){
			System.out.println(Arrays.toString(a[i]));
		}
		System.out.println("Inverse matrix is:");
		for(int i=0; i<b.length; i++){
			System.out.println(Arrays.toString(b[i]));
		}
	}

}

E201_05_07_杨辉三角

package e201_05_07;

import java.util.Arrays;
import java.util.Scanner;
public class yanghui {
	public static void main(String[] args) {
		@SuppressWarnings("resource")
		Scanner sca = new Scanner(System.in);
		System.out.println("please input line number:");
		int n = sca.nextInt();
		int[][] arr = new int[n][];
		arr[0]=new int[1];
		arr[0][0]=1;
		for( int i=0;i<arr.length;i++) {
			arr[i]=new int[i+1];arr[i][0]=arr[i][i]=1;
			for( int j=1;j<i;j++) {
				arr[i][j]=arr[i-1][j-1]+arr[i-1][j];
			}
		}
		for(int i=0;i<arr.length;i++) {
			System.out.println(Arrays.toString(arr[i]));
		}
		
	}

}

  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孤影墨客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值