JAVA编程练习
1、已知y与x的关系:
,要求:从键盘上输入一个x的值,输出其对应的y的值
1 2 3 4 5 6 7 8 9 10 11 12 | package study; import java.util.Scanner; public class B6 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int x = scanner.nextInt(); int y =x*x+ 3 *x+ 2 ; System.out.println(y); } } |
2、 输入一个圆半径(r),计算并输出圆的面积和周长。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package study; import java.util.Scanner; public class B7 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int r = scanner.nextInt(); double pi= 3.14 ; double c = 2 *pi*r; double s = pi*r*r; System.out.println(c); System.out.println(s); } } |
3、输入一个三位正整数n,输出其个位、十位和百位上的数字。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package study; import java.util.Scanner; public class B8 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int a =scanner.nextInt(); int bai =a/ 100 ; int shi =a/ 10 % 10 ; int ge=a% 10 ; System.out.println( "百位数是" +bai); System.out.println( "十位数是" +shi); System.out.println( "个位数是" +ge); } |
4、根据性别和体重计算输血量。女性体重不超过50kg的输血量为200毫升,否则250毫升;男性不超过60kg的输血量为250毫升,否则300毫升。要求:输入性别和体重,输出输血量。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | package study; import java.util.Scanner; public class B9 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print( "请输入性别" ); String xb = scanner.nextLine(); System.out.println( "请输入体重" ); double tz = scanner.nextDouble(); int blood = 0 ; if (xb.equals( "男" )) { if (tz <= 60 ) { blood = 250 ; } else { blood = 300 ; } } else { if (tz <= 50 ) { blood = 200 ; } else { blood = 250 ; } } System.out.printf( "输血量为%d毫升" , blood); } } |
5、当气温高于26℃时,需要开启制冷空调;气温低于10℃则开启制热空调;其余情况只需要开启送风模式即可。编写自动温控程序,输入温度,输出相应的提示字符串,比如“开启制冷”。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import java.util.Scanner; public class B10 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int x =scanner.nextInt(); if (x< 10 ){ System.out.println( "制热空调" ); } else if (x > 26 ) { System.out.println( "制冷空调" ); } else { System.out.println( "送风模式" ); } } } |
6、假设从A地到B地的火车票有硬座和硬卧,价格分别为100和190元。根据铁路部门规定,未成年人(18周岁以下)身高不足120cm免票,120(含)-150(不含)cm需半票,150及以上的需全票,未成年人卧铺只能减免硬座的半价。请设计一个购票程序,要求输入年龄和身高(未成人需要输入)以及票的类型,输出票的价。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | package study; import java.util.Scanner; public class B11 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println( "请输入年龄" ); int age =scanner.nextInt(); System.out.println( "请输入身高" ); int sg =scanner.nextInt(); System.out.println( "请输入票价类型" ); String x =scanner.next(); if (age< 18 ){ if (sg< 120 ){ System.out.println( "票价免费" ); } else if ( 120 <=sg&&sg< 150 &&x.equals( "硬座" )){ System.out.println( 100 / 2 ); } else if ( 120 <=sg&&sg< 150 &&x.equals( "卧铺" )){ int qian = 190 - 100 / 2 ; System.out.println(qian); } else if (sg>= 150 &&x.equals( "卧铺" )){ System.out.println( 190 ); } else if (sg>= 150 &&x.equals( "硬座" )){ System.out.println( 100 ); } } else { System.out.println( "卧铺" + 190 + "硬座" + 100 ); } } } |
7、居民生活用电按3个梯度收费:月用电量150千瓦时及以下部分,每千瓦时0.4463元,151—400千瓦时部分为0.4663元,401千瓦时以上部分为0.5663元,请编写程序,当输入用户的用电量时,计算出所需付的费用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | package study; import java.util.Scanner; public class B12 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int x = scanner.nextInt(); if (x <= 150 ) { double z = x * 0.4463 ; System.out.println(z); } else if (x > 150 && x <= 400 ) { double z = x * 0.4663 ; System.out.println(z); } else { double z = x * 0.5663 ; System.out.println(z); } } } |
8、要求输入月份,判断该月所处的季节并输出季节(假设:12、1、2月为冬季,依次类推)(使用switch语句编写程序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | package study; import java.util.Scanner; public class B13 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int x =scanner.nextInt(); switch (x){ case 1 : case 12 : case 2 : System.out.println( "冬季" ); break ; case 3 : case 4 : case 5 : System.out.println( "春节" ); break ; case 6 : case 7 : case 8 : System.out.println( "夏" ); break ; case 9 : case 10 : case 11 : System.out.println( "秋" ); } } } |
9、编写程序,把560分钟换算成用小时和分钟表示,然后输出。
1 2 3 4 5 6 7 8 9 10 | package study; public class B14 { public static void main(String[] args) { int x = 560 ; int s = 560 / 60 ; int m= 560 -s* 60 ; System.out.println(s+ "小时" +m+ "分钟" ); } } |
10、输入三个整数A、B和C,将它们交换(即A的值给B,B的值给C,C的值给A)后输出A、B和C。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | package study; import java.util.Scanner; public class B15 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int a =scanner.nextInt(); int b =scanner.nextInt(); int c =scanner.nextInt(); int z =a; a =c; c=b; b=z; System.out.println(a); System.out.println(b); System.out.println(c); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· 单线程的Redis速度为什么快?
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码