文章目录
- 1 基础知识
- E201_01_01温度转换
- E201_01_02计算圆形面积
- E201_01_03计算球的体积
- E201_01_04计算三角形面积
- E201_01_05鬼谷算题
- E201_01_06 话费计费器
- 2 选择结构
- E201_02_01的士计价器
- E201_02_02判断闰年
- E201_02_03分段函数
- E201_02_04按性别计算输血量
- E201_02_05购买火车票
- E201_02_06阶梯电价
- E201_02_07个税计算器
- E201_02_08判断体型
- E201_02_09奖金发放
- E201_02_10判断季节
- E201_02_11四则运算器
Java程序设计基础练习50题(中)Java程序设计基础练习50题(下)
1 基础知识
E201_01_01温度转换
输入华氏温度值,转换为摄氏温度输出,华氏温度转换为摄氏温度 。
package e201_01_01;
import java.util.Scanner;
public class temperature {
public static void main(String[] args) {
int fahrenheit;
float centigrade;
System.out.println("Please input fahrenheit:");
Scanner scn=new Scanner(System.in);
fahrenheit=scn.nextInt();
centigrade=(float)((f-32)*5/9.0);
System.out.println("The centigrade temperature is:"+centigrade);
}
}
E201_01_02计算圆形面积
输入圆形的半径(假设为整型),计算圆形的面积,面积要求定义成单精度浮点型。
package e201_01_02;
import java.util.Scanner;
public class CircularArea {
public static void main(String[] args) {
int r;
float c;
double p=3.14;
System.out.println("Please input the radius of the circle:");
Scanner scn=new Scanner(System.in);
r=scn.nextInt();
c=(float)(r*r*p);
System.out.println("The area of the circle is:"+c);
}
}
E201_01_03计算球的体积
输入球的半径,计算球的体积,体积公式:
package e201_01_03;
import java.util.Scanner;
public class VolumeSphere {
public static void main(String[] args) {
int r;
double s;
double p=3.14;
System.out.println("Please input the radius of the sphere:");
Scanner scn=new Scanner(System.in);
r=scn.nextInt();
s=(3*Math.pow(r,3)/4);
System.out.println("The volume of a sphere is:"+s);
}
}
E201_01_04计算三角形面积
输入三角形的三个边长(假设为整型),根据海伦公式计算三角形的面积,海伦公式如下:
面积=,其中
package e201_01_04;
import java.util.Scanner;
public class TriangularArea {
public static void main(String[] args) {
int r1,r2,r3;
float a,s;
System.out.println("Please input lengths:");
Scanner scn=new Scanner(System.in);
r1=scn.nextInt();
r2=scn.nextInt();
r3=scn.nextInt();
s =(r1+r2+r3)/2f;
a= (float) Math.sqrt(s*(s-r1)*(s-r2)*(s-r3));
if (r1+r2>r3 && r1+r3>r2 && r2+r3>r1){
System.out.println("The area of the triangular is:"+a);
}else{
System.out.println("The triangles don't exist.");
}
}
}
E201_01_05鬼谷算题
在鬼谷算题中有这样一个著名的题目:“今有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二,问物几何?”这属于高等数学中的数论。我国宋代学者对这类题目钻研已颇为精深,总结出了“三人同行七十稀,五树梅花廿一枝,七子团圆正半月,去百零五便得知。” 也就是三的余数乘以70,五的余数乘以21,七的余数乘以15,然后对105求余即可。
package e201_01_05;
import java.util.Scanner;
public class GuiGu {
public static void main(String[] args) {
Scanner sca = new Scanner(System.in);
System.out.println("Please input three numbers:");
int a = sca.nextInt();
int b = sca.nextInt();
int c = sca.nextInt();
int a1=((a*70) +(b*21)+(c*15))%105;
System.out.println(a1);
}
}
E201_01_06 话费计费器
输入通话时长(秒),计算通话费用。费用按分钟计费,不足一分的按一分钟计算,资费标准0.2元/分钟。
package e201_01_06;
import java.util.Scanner;
public class TelephoneCharge {
public static void main(String[] args) {
Scanner sca = new Scanner(System.in);
System.out.println("Please input the time:");
int S = sca.nextInt();
float a = (float) ((float ) (Math.ceil(S/60.0))*0.2);
System.out.println("The price is "+a+"yuan");
}
}
2 选择结构
E201_02_01的士计价器
嘉兴市出租车,起步价(2公里以内)为7元,超过2公里的按照每公里3元计算。要求根据路程计算费用。
package e201_02_01;
import java.util.Scanner;
public class texipay {
public static void main(String[] args) {
Scanner sca = new Scanner(System.in);
System.out.println("Please enter your journey:");
double j = sca.nextDouble();
if (j<2.0 || j==2.0) {
System.out.println("Need to pay 7 yuan.");
}else {
double p=((j-2.0)*3.0)+7.0;
System.out.println("Need to pay " +p+" yuan.");
}
}
}
E201_02_02判断闰年
闰年的条件是能被4整除,但不能被100整除;或能被4整除,又能被100整除。
package e201_02_02;
import java.util.Scanner;
public class LeapYear {
public static void main(String[] args) {
Scanner sca = new Scanner(System.in);
System.out.println("Please input year:");
int year = sca.nextInt();
if(year<0){
System.out.println("Inpur error!");
}else if(year%4==0&&year%100!=0||year%400==0){
System.out.println(year+" is a leap year");
}else{
System.out.println(year+" isn't a leap year");
}
}
}
E201_02_03分段函数
输入X的值,求处对应的f(x)的值。
package e201_02_03;
import java.util.Scanner;
public class function {
public static void main(String[] args) {
Scanner sca = new Scanner(System.in);
System.out.println("Please input a number:");
double x = sca.nextDouble();
if (x<0) {
double y = x+6.0;
System.out.println("Result is "+y);
}else {
double y = x*x+4.0*x+6;
System.out.println("Result is "+y);
}
}
}
E201_02_04按性别计算输血量
根据性别和体重计算输血量。女性体重不超过50kg的输血量为200毫升,否则250毫升;男性不超过60kg的输血量为250毫升,否则300毫升。
package e201_02_04;
import java.util.Scanner;
public class BloodTransfusionVolume {
public static void main(String[] args) {
Scanner sca = new Scanner(System.in);
System.out.println("Please input your gender and weight:");
int x= sca.nextInt();
Double w =sca.nextDouble();
switch(x) {
case 0:
if (w<50) {
System.out.println("You can donate 200ml of blood.");
break;
}else {
System.out.println("You can donate 250ml of blood.");
break;
}
case 1:
if (w<60) {
System.out.println("You can donate 250ml of blood.");
break;
}else {
System.out.println("You can donate 300ml of blood.");
break;
}
default:
System.out.println("error");
break;
}
}
}
E201_02_05购买火车票
假设从A地到B地的火车票有硬座和硬卧,价格分别为100和190元。根据铁路部门规定,未成年人(18周岁以下)身高不足120cm免票,120(含)-150(不含)cm需半票,150及以上的需全票,未成年人卧铺只能减免硬座的半价。请设计一个购票程序,要求输入年龄和身高(未成人需要输入)以及票的类型,输出票的价格
package e201_02_05;
import java.util.Scanner;
public class PurchaseTrainTickets {
public static void main(String[] args) {
Scanner scn=new Scanner(System.in);
System.out.println("Please input your age:");
int a=scn.nextInt();
System.out.println("Please input ticket type(1.head seat 2.head sleeper):");
int r=scn.nextInt();
if (a<18) {
System.out.println("Please input your height(cm):");
double h=scn.nextDouble();
if (h<120) {
System.out.println("Free ticket");
}else if (h<150) {
switch(r) {
case 1:
System.out.println("The ticker is 50 yuan.");
break;
case 2:
System.out.println("The ticker is 140 yuan.");
break;
default:
System.out.println("Input error.");
}
}else {
ticker(r);
}
}else {
ticker(r);
}
}
public static void ticker(int r) {
switch(r) {
case 1:
System.out.println("The ticker is 100 yuan.");
break;
case 2:
System.out.println("The ticker is 190 yuan.");
break;
default:
System.out.println("Input error.");
}
}
}
E201_02_06阶梯电价
根据《福建省电网销售电价表》,居民生活用电按3个梯度收费:月用电量150千瓦时及以下部分,每千瓦时0.4463元,151—400千瓦时部分为0.4663元,401千瓦时以上部分为0.5663元,请编写程序,当输入用户的用电量时,计算出所需付的费用。
package e201_02_06;
import java.util.Scanner;
public class StepTariff {
public static void main(String[] args) {
double n;
double f;
System.out.println("Please input electricity consumption:");
Scanner scn=new Scanner(System.in);
n=scn.nextDouble();
if (n<150) {
f=n*0.4463;
}else if(n<400){
f=150*0.4463+(n-150)*0.4663;
}else {
f=150*0.4463+150*0.6663+(n-400)*0.5663;
}
System.out.println("The tariff is: "+f);
}
}
E201_02_07个税计算器
要求输入工资,计算应交个税额。
个税额 = 全月应纳税所得额*税率-速算扣除数
全月应纳税所得额 = (应发工资-四金)-3500
假设四金700元,3500元为起征点
应纳税所得额(含税) | 税率(%) | 速算扣除数 |
不超过1500元的 | 3 | 0 |
超过1500元至4,500元的部分 | 10 | 105 |
超过4,500元至9,000元的部分 | 20 | 555 |
超过9,000元至35,000元的部分 | 25 | 1,005 |
超过35,000元至55,000元的部分 | 30 | 2,755 |
超过55,000元至80,000元的部分 | 35 | 5,505 |
超过80,000元的部分 | 45 | 13,505 |
package e201_02_07;
import java.util.Scanner;
public class IndividualIncomeTax {
public static void main(String[] args) {
System.out.println("please input salary:");
Scanner sca = new Scanner(System.in);
double n = sca.nextDouble();
double a=0;
double m = 700+3500;
if (n<m) {
System.out.println("There's no tax");
}else if ((n-m)<1500){
a =(n-m)*0.03-0;
}if (1500<n-m && n-m<4500) {
a =(n-m)*0.10-105;
}if (4500<n-m && n-m<9000) {
a =(n-m)*0.20-555;
}if (9000<n-m && n-m<35000) {
a =(n-m)*0.25-1005;
}if (35000<n-m && n-m<55000) {
a =(n-m)*0.30-2775;
}if (55000<n-m && n-m<80000) {
a =(n-m)*0.35-5505;
}if (80000<n-m) {
a =(n-m)*0.45-13505;
}
System.out.println("The tax to be paid is "+a);
}}
E201_02_08判断体型
BMI(Body Mass Index)指数,即身体质量指数,简称体质指数,是用体重公斤数除以身高米数平方得出的数字,是目前国际上常用的衡量人体胖瘦程度以及是否健康的一个标准。输入体重和身高,输出体型描述。
BMI | 体型描述 |
<18.5 | 体重过低 |
[18.5,25) | 正常 |
[25,30) | 肥胖前期 |
[30,35) | I度肥胖 |
[35,40) | II度肥胖 |
≥40 | Ⅲ度肥胖 |
package e201_02_08;
import java.util.Scanner;
public class BMI {
public static void main(String[] args) {
Scanner sca = new Scanner(System.in);
System.out.println("please input height(m) and weight(kg)");
double h = sca.nextDouble();
double w = sca.nextDouble();
double bmi = w/(Math.pow(h, 2));
if (bmi<18.5) {
System.out.println("体重过低");
}else if (18.5<=bmi && bmi<25.0){
System.out.println("正常");
}if (25.0<=bmi && bmi<30.0) {
System.out.println("肥胖前期");
}if (30.0<=bmi && bmi<35.0) {
System.out.println("1度肥胖");
}if (35.0<=bmi && bmi<40.0) {
System.out.println("2度肥胖");
}if (bmi>=40.0) {
System.out.println("3度肥胖");
}
}
}
E201_02_09奖金发放
企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数?
package e201_02_09;
import java.util.Scanner;
public class bonus {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sca = new Scanner(System.in);
System.out.println("please input profits");
double i = sca.nextDouble();
double p = 0;
if (i<100000) {
p = i*0.1;
}if (i>=100000 && i<200000) {
p=10000+(i-100000)*0.075;
}if (i>=200000 && i<400000) {
p=17500+(i-200000)*0.05;
}if (i>=400000 && i<600000) {
p=27500+(i-400000)*0.03;
}if (i>=600000 && i<1000000) {
p=33500+(i-600000)*0.015;
}if (i>=100000) {
p=39500+(i-1000000)*0.01;
}
System.out.println("The bonus is "+p);
}
}
E201_02_10判断季节
要求输入月份,判断该月所处的季节并输出季节(假设:12、1、2月为冬季,依次类推)
package e201_02_10;
import java.util.Scanner;
public class season {
public static void main(String[] args) {
Scanner sca = new Scanner(System.in);
System.out.println("please input month");
int m = sca.nextInt();
if (m>2 &&m<=5) {
System.out.println("spring");
}if (m>5 &&m<=8) {
System.out.println("summer");
}if (m>8 &&m<=11) {
System.out.println("autumn");
}if (m==12 || m==1 || m==2) {
System.out.println("winter");
}
}
}
E201_02_11四则运算器
要求操作符和两个操作数,执行相应的四则运算。
package e201_02_11;
import java.util.Scanner;
public class arithmetic {
public static void main(String[] args) {
double x,y,s = 0;String t;
@SuppressWarnings("resource")
Scanner sca =new Scanner(System.in);
System.out.println("please input operator and two numbers:");
t=sca.nextLine();
x=sca.nextDouble();y=sca.nextDouble();
switch(t){
case "+":s=x+y;break;
case "-":s=x-y;break;
case "*":s=x*y;break;
case "/":s=x/y;break;
}
System.out.println(x+t+y+"="+s);
}
}