PTA JAVA01 Java入门

这个系列的博客包含了多个简单的Java编程练习,旨在帮助初学者掌握Java的基础操作,如输入输出、数值计算、字符串处理等。练习涵盖Hello World、数字交换、字母转换、几何计算(球的表面积和体积)、温度转换以及一系列输入输出操作的练习题。
摘要由CSDN通过智能技术生成

**题都是大一上刚开始学的c语言的难度,这些题的目的只是为了让我们学习java的基本操作。

以下仅供参考!

7-1 模仿练习

public class Main {
    public static void main(String[] args) throws Exception{
        System.out.println("Programming is fun!");
        System.out.println("Fundamentals First");
        System.out. println("Problem Driven");
    }
}

7-2 Hello World!

public class Main {
    public static void main(String[] args) throws Exception{
        System.out.println("Hello World!");
    }
}

7-3 sdut-入门-交换两个整数的值

import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws Exception{
        Scanner scanner = new Scanner(System.in);
        int x = scanner.nextInt();
        int y = scanner.nextInt();
        System.out.print(y + " ");
        System.out.print(x);
    }
}

**print、printf、println的区别请看:Java中print、printf、println的区别_C2G_Rg的博客-CSDN博客_print和println的区别 

7-4 sdut-入门-转换字母

import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws Exception{
        String a;
        Scanner reader = new Scanner(System.in);
        a = reader.nextLine();
        System.out.print(a.toUpperCase());
        reader.close();
    }
}

7-5 球的表面积和体积

import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws Exception{
        double r; double pi = 3.14159265;
        Scanner reader = new Scanner(System.in);
        r = reader.nextDouble();
        double s = 4 * pi * r * r;
        double v = 4 * pi * r * r * r / 3;
        System.out.printf("%.2f", s);
        System.out.print(" ");
        System.out.printf("%.2f", v);
        reader.close();
    }
}

7-6 华氏温度转化为摄氏温度

import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws Exception{
        double r;
        Scanner reader = new Scanner(System.in);
        r = reader.nextDouble();
        double c = 5 * (r - 32) / 9;
        System.out.printf("c=%.2f", c);
        reader.close();
    }
}

7-7 sdut-入门-1 A+B for Input-Output Practice(I)

import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws Exception{
        int a, b, c;
        Scanner reader = new Scanner(System.in);
        while(reader.hasNext()){
            a = reader.nextInt();
            b = reader.nextInt();
            c = a + b;
            System.out.println(c);
        }
        reader.close();
    }
}

7-8 sdut-入门-2 A+B for Input-Output Practice (II)

import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws Exception{
        int a, b, c;
        int n;
        Scanner reader = new Scanner(System.in);
        n = reader.nextInt();
        while(n > 0){
            a = reader.nextInt();
            b = reader.nextInt();
            c = a + b;
            n--;
            System.out.println(c);
        }
        reader.close();
    }
}

7-9 sdut0-入门-3 A+B for Input-Output Practice (III)

import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws Exception{
        int a, b, c;
        Scanner reader = new Scanner(System.in);
        while(reader.hasNext()){
            a = reader.nextInt();
            b = reader.nextInt();
            if(a == 0 && b == 0){
                break;
            }
            c = a + b;
            System.out.println(c);
        }
        reader.close();
    }
}

7-10 sdut-入门-4 A+B for Input-Output Practice (IV)

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int n, x;
        Scanner reader = new Scanner(System.in);
        while(reader.hasNext()) {
            int sum = 0;
            n = reader.nextInt();
            if(n == 0) break;
            for (int i = 0; i < n; i++) {
                x = reader.nextInt();
                sum += x;
            }
            System.out.println(sum);
        }
    }
}

7-11 sdut-入门-5 A+B for Input-Output Practice (V)

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int n, x, t;
        Scanner reader = new Scanner(System.in);
        t = reader.nextInt();
        for(int j = 0; j < t; j++) {
            int sum = 0;
            n = reader.nextInt();
            for (int i = 0; i < n; i++) {
                x = reader.nextInt();
                sum += x;
            }
            System.out.println(sum);
        }
    }
}

7-12 sdut-入门-6 A+B for Input-Output Practice (VI)

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int n, x;
        Scanner reader = new Scanner(System.in);
        while(reader.hasNext()) {
            int sum = 0;
            n = reader.nextInt();
            for (int i = 0; i < n; i++) {
                x = reader.nextInt();
                sum += x;
            }
            System.out.println(sum);
        }
    }
}

7-13 sdut-入门-7 A+B for Input-Output Practice (VII)

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int a, b;
        Scanner reader = new Scanner(System.in);
        while(reader.hasNext()) {
            int sum = 0;
            a = reader.nextInt();
            b = reader.nextInt();
            sum = a + b;
            System.out.println(sum);
            System.out.printf("\n");
        }
    }
}

7-14 sdut-入门-8 A+B for Input-Output Practice(VIII)

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int n, x, t;
        Scanner reader = new Scanner(System.in);
        t = reader.nextInt();
        for(int j = 0; j < t; j++) {
            int sum = 0;
            n = reader.nextInt();
            for (int i = 0; i < n; i++) {
                x = reader.nextInt();
                sum += x;
            }
            System.out.println(sum);
            System.out.printf("\n");
        }
    }
}

7-15 打印字母B。

public class Main {
    public static void main(String[] args) {
        System.out.printf("*****\n");
        System.out.printf("*   *\n");
        System.out.printf("*****\n");
        System.out.printf("*   *\n");
        System.out.printf("*****\n");
    }
}

**直接输出就行o.o 还想着怎么用for循环。。。

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值