1. (简答题)
Sum the digits in an integer:
import java.util.Scanner;
public class Demo1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个数字:");
int a = sc.nextInt();
int ge = a%10;
int shi = a/10%10;
int bai = a/100%10;
int sum = ge + shi + bai;
System.out.println("sum="+sum);
}
}
2. (简答题)Area of a triangle:
import java.util.Scanner;
import static java.lang.Math.pow;
public class Demo2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("please enter the value of X1:");
double x1 = sc.nextDouble();
System.out.println("please enter the value of y1:");
double y1 = sc.nextDouble();
System.out.println("please enter the value of x2:");
double x2 = sc.nextDouble();
System.out.println("please enter the value of y1:");
double y2 = sc.nextDouble();
System.out.println("please enter the value of X3:");
double x3 = sc.nextDouble();
System.out.println("please enter the value of y3:");
double y3 = sc.nextDouble();
double a1 = x2 - x1;
double a2 = y2 - y1;
double a3 = pow(a1,2)+pow(a2,2);
double a4 = pow(a3,0.5);
double b1 = x3 - x2;
double b2 = y3 - y2;
double b3 = pow(b1,2)+pow(b2,2);
double b4 = pow(b3,0.5);
double c1 = x3 - x1;
double c2 = y3 - y1;
double c3 = pow(c1,2)+pow(c2,2);
double c4 = pow(c3,0.5);
double site1 = a4;
double site2 = b4;
double site3 = c4;
double s =(site1 + site2 + site3)/2;
double area1 = s*(s-site1)*(s-site2)*(s-site3);
double area = pow(area1,0.5);
System.out.println("The area of the triabgle :"+(int)(area*10)/10.0);
}
}
3. (简答题)Computing BMI:
import java.util.Scanner;
public class Demo3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("please enter the value of pounds:");
double pounds = sc.nextDouble();
System.out.println("please enter the value of inches:");
double inches =sc.nextDouble();
double kg = 0.45359237*pounds;
double meter = 0.0254*inches;
double bmi1 = kg/meter/meter;
int bmi2 = (int)(bmi1*100000);
if(bmi2%10<5){
bmi2 = bmi2;
}else{
bmi2 = bmi2 +10;
}
int bmi3 = bmi2/10;
double bmi = bmi3/10000.0;
System.out.println("该人的BMI为:"+bmi);
}
}
4. (简答题)Calculate future investment value:
import java.util.Scanner;
public class Demo4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("please enter the value of investment amount:");
double investmentAmount = sc.nextDouble();
System.out.println("please enter the value of annual interest rate in percentage:");
double rate = sc.nextDouble()/100;
System.out.println("please enter the number of year:");
int year = sc.nextInt();
double futureInvestmentValue = investmentAmount ;
for(int i= 1;i < year*12+1;i++){
futureInvestmentValue = futureInvestmentValue*(1+rate/12);
}
System.out.println("Accumulated Value:" + (int)(futureInvestmentValue*100)/100.0);
}
}
5. (简答题)Compute the volume of a cylinder:
import java.util.Scanner;
public class Demo5 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("please enter the value of radius");
double radius = sc.nextDouble();
System.out.println("please enter the value of length");
double length = sc.nextDouble();
double area = radius * radius * 3.1415926;
double volume1 = area * length;
System.out.println("The area is :"+(int)(area*10000)/10000.0);
int volume2 = (int)(volume1 *100);
if(volume2%10<5){
volume2 = volume2;
}else{
volume2 = volume2 +10;
}
int volume3 = volume2/10;
double volume = volume3/10.0;
System.out.println("The volume is:"+volume);
}
}