Java练习

1.

已知y与x的关系:,要求:从键盘上输入一个x的值,输出其对应的y的值。

public class w1 {
//已知y与x的关系:,要求:从键盘上输入一个x的值,输出其对应的y的值。
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入一个整数:");
int x= sc.nextInt();
int y=x*x+3*x+2;
System.out.println("y="+y);
}
}

结果:

请输入一个整数:
3
y=20

 

 

2.

输入一个圆半径(r),计算并输出圆的面积和周长。

public class w2 {
public static void main(String[] args) {
//输入一个圆半径(r),计算并输出圆的面积和周长。
final double PI=3.1416;
Scanner sc=new Scanner(System.in);
System.out.println("输入圆的半径:");
double r= sc.nextInt();
double s=PI*r*r;
double c=2*PI*r;
System.out.println("半径为2.0的圆的面积是"+s+",周长是"+c);
}
}

结果:

输入圆的半径:
2
半径为2.0的圆的面积是12.5664,周长是12.5664

 

 3.

输入一个三位正整数n,输出其个位、十位和百位上的数字。

public class w3 {
//输入一个三位正整数n,输出其个位、十位和百位上的数字。
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个三位数:");
int x = sc.nextInt();
int g = x % 10;
int s = x / 10 % 10;
int b = x / 100;
System.out.println("这个数的个位是" + g + ",十位是" + s + ",百位是" + b);

}


}
结果:

请输入一个三位数:
287
这个数的个位是7,十位是8,百位是2

 

 4.

根据性别和体重计算输血量。女性体重不超过50kg的输血量为200毫升,否则250毫升;男性不超过60kg的输血量为250毫升,否则300毫升。要求:输入性别和体重,输出输血量。

public class w4 {
public static void main(String[] args) {
//根据性别和体重计算输血量。女性体重不超过50kg的输血量为200毫升,否则250毫升;男性不超过60kg的输血量为250毫升,否则300毫升。要求:输入性别和体重,输出输血量。
Scanner sc = new Scanner(System.in);
System.out.println("请输入性别:");
String x = sc.nextLine();
System.out.println("请输入体重(kg):");
float weight = sc.nextFloat();
int result = 0;
if ("女".equals(x)) {
if(weight<=50){
result=200;
}else{
result=250;
}

}else if("男".equals(x)){
if(weight<=60){
result=250;
}else{
result=300;
}
}else{
System.out.println("请输入正确的性别!(男/女)");
}
System.out.println("输血量为:"+result+"ml");
}
}
结果:

请输入性别:

请输入体重(kg):
50
输血量为:200ml

 

 5.

当气温高于26℃时,需要开启制冷空调;气温低于10℃则开启制热空调;其余情况只需要开启送风模式即可。编写自动温控程序,输入温度,输出相应的提示字符串,比如“开启制冷”。

 

public class w5 {
public static void main(String[] args) {
//当气温高于26℃时,需要开启制冷空调;气温低于10℃则开启制热空调;其余情况只需要开启送风模式即可。编写自动温控程序,输入温度,输出相应的提示字符串,比如“开启制冷”。
Scanner sc=new Scanner(System.in);
System.out.println("请输入温度:");
int x= sc.nextInt();
if(x>26){
System.out.println("开启制冷");
}else if(x<10){
System.out.println("开启制热");
}else{
System.out.println("开启送风");
}
}
}
结果:

请输入温度:
5
开启制热

 

 6.

假设从A地到B地的火车票有硬座和硬卧,价格分别为100和190元。根据铁路部门规定,未成年人(18周岁以下)身高不足120cm免票,120(含)-150(不含)cm需半票,150及以上的需全票,未成年人卧铺只能减免硬座的半价。请设计一个购票程序,要求输入年龄和身高(未成人需要输入)以及票的类型,输出票的价。

public class w6 {
public static void main(String[] args) {
//假设从A地到B地的火车票有硬座和硬卧,价格分别为100和190元。根据铁路部门规定,未成年人(18周岁以下)身高不免足120cm票,120(含)-150(不含)cm需半票,150及以上的需全票,未成年人卧铺只能减免硬座的半价。请设计一个购票程序,要求输入年龄和身高(未成人需要输入)以及票的类型,输出票的价。
Scanner sc = new Scanner(System.in);
System.out.println("请输入年龄:");
int ege = sc.nextInt();
System.out.println("请输入选择的票型:");
String ticket = sc.next();
int p = 0;
if (ege > 18) {

if ("硬座".equals(ticket)) {
p = 100;
}
if ("硬卧".equals(ticket)) {
p = 190;
}
} else {
System.out.println("请输入身高:");
int high = sc.nextInt();
if ("硬座".equals(ticket) && high < 120) {
p = 0;
}
if ("硬座".equals(ticket) && high >= 120 && high < 150) {
p = 50;
}
if ("硬座".equals(ticket) && high > 150) {
p = 100;
}
if ("硬卧".equals(ticket)) {
p = 190 - 50;
}
System.out.println("价格是:"+p);
}
}
}

结果:

请输入年龄:
12
请输入选择的票型:
硬座
请输入身高:
145

价格是:50

 

 

 7.

居民生活用电按3个梯度收费:月用电量150千瓦时及以下部分,每千瓦时0.4463元,151—400千瓦时部分为0.4663元,401千瓦时以上部分为0.5663元,请编写程序,当输入用户的用电量时,计算出所需付的费用。

public class w7 {
//居民生活用电按3个梯度收费:月用电量150千瓦时及以下部分,每千瓦时0.4463元,151—400千瓦时部分为0.4663元,401千瓦时以上部分为0.5663元,请编写程序,当输入用户的用电量时,计算出所需付的费用。
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入用电量:");
int d= sc.nextInt();
double money=0;
if(d<=150){
money=d*0.4463;
}
if(d>=151&&d<=400){
money=150*0.4463+(d-150)*0.4663;
}
if(d>401){
money=150*0.4463+250*0.4663+(d-401)*0.5663;
}
System.out.println("用电费用为"+money);
}
}
结果:

请输入用电量:
395
用电费用为181.18849999999998

 

 8.

要求输入月份,判断该月所处的季节并输出季节(假设:12、1、2月为冬季,依次类推)(使用switch语句编写程序)

 

   public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int j= sc.nextInt();
switch(j){
case 1:
System.out.println("冬季");break;
case 2:
System.out.println("冬季");break;
case 3:
System.out.println("春季");break;
case 4:
System.out.println("春季");break;
case 5:
System.out.println("春季");break;
case 6:
System.out.println("夏季");break;
case 7:
System.out.println("夏季");break;
case 8:
System.out.println("夏季");break;
case 9:
System.out.println("秋季");break;
case 10:
System.out.println("秋季");break;
case 11:
System.out.println("秋季");break;
case 12:
System.out.println("冬季");
}
}
}
结果:

10
秋季

 

 9.

编写程序,把560分钟换算成用小时和分钟表示,然后输出。

public class w9 {
//编写程序,把560分钟换算成用小时和分钟表示,然后输出。
public static void main(String[] args) {
int x=560;
int h=560/60;
int f=560%60;
System.out.println("560分钟是"+h+"小时"+f+"分钟");

}
}

结果:

560分钟是9小时20分钟

 

 

 

 10.

输入三个整数A、B和C,将它们交换(即A的值给B,B的值给C,C的值给A)后输出A、B和C。

public class w10 {
//输入三个整数A、B和C,将它们交换(即A的值给B,B的值给C,C的值给A)后输出A、B和C。
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入三个整数:");
int A= sc.nextInt();
int B= sc.nextInt();
int C= sc.nextInt();
int x=0;
x=A;
A=B;
B=C;
C=x;
System.out.println("A:"+A);
System.out.println("B:"+B);
System.out.println("C:"+C);
}
}
结果:

请输入三个整数:
4 5 6
A:5
B:6
C:4

 

posted @   不想看月亮  阅读(97)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示