几道给师弟解决的学生代码作业

昨晚给一个在学java的师弟编写了几道代码题,皆为原创,不足之处望博友斧正。

题目:

     

代码:

//第一题

import java.util.*;

public class TestEle {
	public static void main(String args[]) {
		double ele, price;
		System.out.println("Plese input the electricity that you used this month:");
		
		while(true) {
			try {
				Scanner sc = new Scanner(System.in);
				ele = sc.nextDouble();         //从控制台输入用户该月得到的电费	
			}catch(Exception e) {
				System.out.println("Error input! Please input an integer or a decimal:");
				continue;  //如果用户输入的不是整数或小数,循环从头开始,提示重新输入
			}
			if(Integer.class.isInstance(ele) || Double.class.isInstance(ele)) break;   //当检测到输入正确时退出循环,运行下面的代码
		}
		
		if (ele <= 50) 
			price = ele * 0.538;
		else if (ele <= 200)
			price = ele * 0.568;
		else 
			price = ele * 0.638;   //电费计算
		
		System.out.println("The electricity charge you need to pay is:");
		System.out.println(String.format("%.2f" + "元", price));   //取小数点后两位
	}
}
//第二题

import java.util.*;

public class TestSort {
	public static void main(String args[]) {
		int test,len;
		
		System.out.println("Please input an integer(length > 2):");
		
		while(true) {
			Scanner sc = new Scanner(System.in);
			try {
				test = sc.nextInt();        //从控制台标准输入一个整数给变量test		
			}catch(Exception e) { 
				System.out.println("Input Error! Please input an integer:");
				continue;
			}
			
			//如果输入位数小于三位,报错重输
			if(test < 100) {
				System.out.println("Input Error!");
				System.out.println("Please input an integer whose length longer than 2:");
			}  
			
			if(test >= 100) break;
		}
		
			
		String str = Integer.toString(test);
		len = str.length();   //得到输入整数的长度
		
		int[] a = new int[len];
		int n = len - 1;
		
		//将输入的整数中各位位数拆成单个数存入数组a
		while(n >= 0) {
			a[n] = test % 10;
			test = test/10;
			n--;	
		}           
		
		//冒泡排序法(逆序)
		int temp;
		for(int i = 0; i < len; i++)
			for(int j = 1; j < len - i; j++) {
				if(a[j - 1] < a[j]) {
					temp = a[j];
					a[j] = a[j - 1];
					a[j - 1] = temp;
				}
			}
		
		System.out.println("The converse sorted result is:");
		for(int i = 0; i < len; i++) {
			System.out.print(a[i]);
		}
		
	}
}
//第三题

public class TestPrime {
	public static void main(String args[]) {
		int time = 0;
		int sum = 0;
		
		for(int num = 100; num < 501; num++) {
			for(int i = 2; i <= num; i++) {
				
				//循环到该数不是素数的情况,跳出小循环,从新的数开始
				if(num % i == 0 && i != num) {break;} 
				
				//该数是素数,输出,并计数+1,求和。
				else if(i == num){
					System.out.print(num + " ");
					time++;
					sum = sum + num;
				}
			}
		}
		
		System.out.println();
		System.out.println("The number of prime number in between 100 to 500 is: " + time);
		System.out.println("The sum of all the prime numbers is: " + sum);
	}
}
//第四题

import java.lang.Math.*;

public class TestArray {
	public static void main(String args[]) {
		int n = 2;
		double sum = 0;
		double[] a = new double[15];
		
		//构建出该数列的前15项
		for(int i = 0; i < 15; i++) {
			a[i] = (i+2)/Math.pow(2,i+1);
		}                      
		
		//求和
		for(int i = 0; i < 15; i++) {
			sum = sum + a[i]
		}
		
		System.out.println("The sum of the array a is: " + sum);
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值