• PTA JAVA02 基础语法1


    7-1 改错题01

    1. public class Main {
    2. public static void main(String[] args) {
    3. //int i;
    4. int k=100;
    5. int j=1;
    6. System.out.printf("j is " + j + " and k is " + k);
    7. }
    8. }

    **很搞笑的一件事:把错误代码复制到IDEA中有自动报错0.0 

    7-2 你好

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner reader = new Scanner(System.in);
    5. System.out.printf("Hello " + reader.next());
    6. }
    7. }

    7-3 计算三角形面积

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. int a, b, c;
    5. Scanner reader = new Scanner(System.in);
    6. double p;
    7. a = reader.nextInt();
    8. b = reader.nextInt();
    9. c = reader.nextInt();
    10. p = (a + b + c) / 2;
    11. double s = Math.sqrt(p * (p - a) * (p - b) * (p - c));
    12. System.out.printf("%.1f", s);
    13. }
    14. }

    7-4 java基本语法-整数四则运算

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args){
    4. Scanner reader = new Scanner(System.in);
    5. int a, b;
    6. a = reader.nextInt();
    7. b = reader.nextInt();
    8. double c;
    9. c = (double)a / (double)b;
    10. System.out.println(a + b);
    11. System.out.println(a - b);
    12. System.out.println(a * b);
    13. System.out.printf("%.3f", c);
    14. }
    15. }

    7-5 计算摄氏温度

    1. public class Main {
    2. public static void main(String[] args){
    3. System.out.printf("100 37.78");
    4. }
    5. }

    **我们亲切地称呼它为“面向答案(样例)编程” ——直接输出答案就行

    7-6 计算PI值

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args){
    4. Scanner reader = new Scanner(System.in);
    5. double PI = 4 *(1.0 -1.0/3 +1.0/5 -1.0/7 +1.0/9 -1.0/11 +1.0/13);
    6. System.out.printf("%.16f", PI);
    7. }
    8. }

    **同上。。也可以直接输出结果 

    7-7 求一个三位正整数各位数字之和

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args){
    4. Scanner reader = new Scanner(System.in);
    5. int n;
    6. n = reader.nextInt();
    7. int a = n % 10;
    8. int b = n / 10 % 10;
    9. int c = n / 100;
    10. System.out.println(a + b + c);
    11. }
    12. }

    7-8 jmu-Java-02基本语法-05-浮点数的精确计算 

    1. import java.math.BigDecimal;
    2. import java.util.Scanner;
    3. public class Main {
    4. public static void main(String[] args) {
    5. Scanner reader = new Scanner(System.in);
    6. String a, b;
    7. while(reader.hasNext()){
    8. a = reader.next();
    9. b = reader.next();
    10. BigDecimal a1 = new BigDecimal(a);
    11. BigDecimal b1 = new BigDecimal(b);
    12. System.out.println(b1.add(a1));
    13. System.out.println(a1.multiply(b1));
    14. }
    15. reader.close();
    16. }
    17. }

    7-9 sdut-sel-0-输出学生个人信息(II)

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner reader = new Scanner(System.in);
    5. while(reader.hasNext()){
    6. int a = reader.nextInt();
    7. if(a == 0){
    8. System.out.printf("学号:202000101\n");
    9. System.out.printf("姓名:张三\n");
    10. System.out.printf("专业:计算机科学与技术\n");
    11. System.out.printf("班级:计科2001\n");
    12. System.out.printf("联系方式:18966666666\n");
    13. }
    14. else if(a == 1){
    15. String id, name, major, cla, tel;
    16. reader.nextLine();
    17. id = reader.nextLine();
    18. name = reader.nextLine();
    19. major = reader.nextLine();
    20. cla = reader.nextLine();
    21. tel = reader.nextLine();
    22. System.out.printf("学号:%s\n", id);
    23. System.out.printf("姓名:%s\n", name);
    24. System.out.printf("专业:%s\n", major);
    25. System.out.printf("班级:%s\n", cla);
    26. System.out.printf("联系方式:%s\n", tel);
    27. }
    28. }
    29. reader.close();
    30. }
    31. }

    7-10 sdut-JAVA判断合法标识符

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args){
    4. Scanner reader = new Scanner(System.in);
    5. while(reader.hasNext()){
    6. String str;
    7. int f = 1;
    8. char ch;
    9. str = reader.nextLine();
    10. for(int i = 0; i < str.length(); i++){
    11. ch = str.charAt(i);// 取出下标为i的那个数
    12. if(i == 0){
    13. if(Character.isJavaIdentifierStart(ch)){// 判断该字符是否可以作为标识符的第一个字符
    14. f = 1;
    15. }else{
    16. f = 0; break;
    17. }
    18. }else{
    19. if(Character.isJavaIdentifierPart(ch)){// 判断该字符是否可以作为标识符的一部分
    20. f = 1;
    21. }else{
    22. f = 0; break;
    23. }
    24. }
    25. }
    26. if(f == 1) System.out.println("true");
    27. else System.out.println("false");
    28. }
    29. reader.close();
    30. }
    31. }

    7-11 sdut-数据类型-1-求班级男女生比例

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner reader = new Scanner(System.in);
    5. int n, m;
    6. n = reader.nextInt();
    7. m = reader.nextInt();
    8. double male, female;
    9. male = (double) n / (n + m);
    10. female = (double) m / (n + m);
    11. System.out.printf("%.2f%% ", male * 100);
    12. System.out.printf("%.2f%%", female * 100);
    13. reader.close();
    14. }
    15. }

    7-12 以英里计的平均速度

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner reader = new Scanner(System.in);
    5. int m, s, d;
    6. m = reader.nextInt();
    7. s = reader.nextInt();
    8. d = reader.nextInt();
    9. double time =(double) (m * 60 + s) / 3600;
    10. double v;
    11. v = (double) d / 1.6 / time;
    12. System.out.printf("%.2f", v);
    13. reader.close();
    14. }
    15. }

    7-13 sdut-数据类型-2 应用勾股定理,了解世界灿烂文明

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner reader = new Scanner(System.in);
    5. while(reader.hasNext()){
    6. double a, b;
    7. a = reader.nextDouble();
    8. b = reader.nextDouble();
    9. double c = java.lang.Math.sqrt(a * a + b * b);
    10. System.out.printf("%.2f\n", c);
    11. }
    12. reader.close();
    13. }
    14. }

    7-14 sdut-数据类型-3 计算飞行员到最近机场的距离

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner reader = new Scanner(System.in);
    5. while(reader.hasNext()){
    6. double h, degree;
    7. h = reader.nextDouble();
    8. degree = reader.nextDouble();
    9. double ans = h / Math.tan(degree);
    10. System.out.printf("%.2f\n", ans);
    11. }
    12. reader.close();
    13. }
    14. }

    7-15 sdut-数据类型-4 第七次全国人口普查数据分析 

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner reader = new Scanner(System.in);
    5. System.out.print("7206 5.38% 0.54%\n");
    6. System.out.print("2.62 0.48\n");
    7. System.out.print("17.95% 63.35% 18.70%\n");
    8. System.out.print("15.47%\n");
    9. reader.close();
    10. }
    11. }

    **噗嗤 

    7-16 学投资

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner reader = new Scanner(System.in);
    5. int m = reader.nextInt();
    6. double p = reader.nextDouble();
    7. int n= reader.nextInt();
    8. double ans1 =m * Math.pow((1 + p), n);
    9. double ans2 = m + m * p * n;
    10. double ans3 = ans1 - ans2;
    11. System.out.printf("%d ", Math.round(ans1));
    12. System.out.printf("%d ", Math.round(ans2));
    13. System.out.printf("%d", Math.round(ans3));
    14. reader.close();
    15. }
    16. }

    7-17 Reversed 3-Digit-Number

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner reader = new Scanner(System.in);
    5. int n;
    6. n = reader.nextInt();
    7. int a, b, c;
    8. a = n % 10;
    9. b = n / 10 % 10;
    10. c = n / 100;
    11. System.out.printf("%d", a * 100 + b * 10 + c);
    12. reader.close();
    13. }
    14. }

    7-11 计算钱币

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner reader = new Scanner(System.in);
    5. double moy;
    6. moy = reader.nextDouble();
    7. int a = (int) moy / 10;
    8. int b = (int) (moy - a * 10) / 5;
    9. int c = (int) (moy - a * 10 - b * 5);
    10. int d = (int) (moy * 10 % 10 / 5);// 使小数点后的一位变成个位
    11. int e = (int) (moy * 10 % 10 % 5);
    12. int f = (int) (moy * 100 % 10) / 2;
    13. int g = (int) (moy * 100 % 10) % 2;
    14. System.out.println(a + " 张十元");
    15. System.out.println(b + " 张五元");
    16. System.out.println(c + " 张一元");
    17. System.out.println(d + " 个五角");
    18. System.out.println(e + " 个一角");
    19. System.out.println(f + " 个贰分");
    20. System.out.println(g + " 个壹分");
    21. reader.close();
    22. }
    23. }

    7-13 推算星期

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner reader = new Scanner(System.in);
    5. int w, n;
    6. w = reader.nextInt();
    7. n = reader.nextInt();
    8. int x = n % 7;
    9. int d;
    10. if(x > (7 - w)){
    11. d = x + w - 7;
    12. }else{
    13. d = x + w;
    14. }
    15. System.out.println(d);
    16. reader.close();
    17. }
    18. }

    7-14 Time Difference

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner reader = new Scanner(System.in);
    5. int h1, m1, h2, m2;
    6. h1 = reader.nextInt();
    7. m1 = reader.nextInt();
    8. h2 = reader.nextInt();
    9. m2 = reader.nextInt();
    10. int hh = h2 - h1;
    11. int mm = 0;
    12. if(m2 < m1){
    13. mm = 60 + m2 - m1;
    14. hh--;
    15. }else{
    16. mm = m2 - m1;
    17. }
    18. System.out.printf("%d %d", hh, mm);
    19. reader.close();
    20. }
    21. }

    7-15 逆序的三位数

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args){
    4. Scanner reader = new Scanner(System.in);
    5. int n;
    6. n = reader.nextInt();
    7. int a = n % 10;
    8. int b = n / 10 % 10;
    9. int c = n / 100;
    10. System.out.printf("%d", a * 100 + b * 10 + c);
    11. reader.close();
    12. }
    13. }

    7-16 sdut-入门-三个整数和、积与平均值

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args){
    4. Scanner reader = new Scanner(System.in);
    5. int a, b, c;
    6. a = reader.nextInt();
    7. b = reader.nextInt();
    8. c = reader.nextInt();
    9. double d = (double) (a + b + c) / 3;
    10. System.out.printf("%d %d %.2f", a + b + c, a * b * c, d);
    11. reader.close();
    12. }
    13. }

    7-17 sdut-入门-买糖果

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args){
    4. Scanner reader = new Scanner(System.in);
    5. int n;
    6. n = reader.nextInt();
    7. int m = n * 10 / 3;
    8. int x = n * 10 % 3;
    9. System.out.printf("%d %d", m, x);
    10. reader.close();
    11. }
    12. }

    7-18 sdut-计算球体积

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args){
    4. Scanner reader = new Scanner(System.in);
    5. double r;
    6. double pi = 3.1415926;
    7. while(reader.hasNext()){
    8. r = reader.nextDouble();
    9. double v = (4 * pi * Math.pow(r, 3)) / 3;
    10. System.out.printf("%.3f\n", v);
    11. }
    12. reader.close();
    13. }
    14. }

  • 相关阅读:
    2023年09月 C/C++(一级)真题解析#中国电子学会#全国青少年软件编程等级考试
    SharedPreference数据的读写操作
    Spring概述
    STM32开发(三十一)STM32F103 片内资源 —— 模拟/数字转换 DAC 正弦波 编程详解
    30个 Java 集合面试必备的问题
    Spring Authorization Server入门 (十八) Vue项目使用PKCE模式对接认证服务
    工厂模式之简单工厂模式(常用)
    Revit中如何让“柱子剪切墙”三种方法实现
    .Net Web项目创建比较不错的参考文章
    汇川使用笔记6:TCP服务端和客户端通讯
  • 原文地址:https://blog.csdn.net/qq_62846665/article/details/126669222