java学习第一部分题目合集(pta)

这些Java编程题目涵盖了基础输入输出、数值计算、数组操作、字符串处理、线性方程组、枚举类型、大整数运算以及素数判断等多个方面,旨在帮助初学者巩固Java编程基础。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

7-1 jmu-Java-01入门-取数字浮点数

本题目要求读入若干以回车结束的字符串表示的整数或者浮点数,然后将每个数中的所有数字全部加总求和。

输入格式:

每行一个整数或者浮点数。保证在浮点数范围内。

输出格式:

整数或者浮点数中的数字之和。题目保证和在整型范围内。

输入样例:

-123.01
234

输出样例:

7
9
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        while(true){
            String a=sc.nextLine();
            char []b=a.toCharArray();
            int sum=0;
            for(int i=0;i<b.length;i++){
                if(b[i]!='.'&&b[i]!='-'){
                    int t=Integer.valueOf(b[i])-48;
                    sum=sum+t;
                }
            }
            System.out.println(sum);
        }
                           }
}

7-4 jmu-java-m01-Scanner入门

输入一个整数,然后输入一个浮点数(带小数点的数)。

对两个数求和,并输出。然后对和进行开根号并输出。

再将输出后的值转化为字符串(可使用String.valueOf()函数),截取前6个字符(包含小数点)。

输入格式:

整数x 浮点数y

输出格式:

整数与浮点数的和

和的开根号

和的开根号的前6个字符

输入样例:

2
3.141592654

输出样例:

5.141592654
2.2675080273286796
2.2675
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int a=0;
        double b=0;
        double c=0;
        a=sc.nextInt();
        b=sc.nextDouble();
        double sum=a+b;
        c=Math.sqrt(sum);
        String d=String.valueOf(c);
        System.out.println(sum);
        System.out.println(c);
        System.out.println(d.substring(0,6));
    }
}

7-5 jmu-java-m02-使用二维数组存储多元线性方程组

可以使用二维数组存储来存储线性方程组的系数与常数。比如,对于如下3元线性方程组

3x+y+z=1

6x+2y+z=-1

-2x+2y+z=7

可以使用二位数组存储

2 1 1 1

6 2 1 -1

-2 2 1 7

编写一个程序可以存储n元线性方程组

输入格式:

整数n,代表n元

n行、每行n+1列线性方程组的系数与常数。系数与常数为double型。

输出格式:

格式化输出二维数组。注意:使用Arrays.deepToString进行格式化输出。

依次输出n行线性方程组的系数与常数。系数以 , 分隔,系数与常数之间以 = 分隔,= 之间有两个空格。

输入样例:

3
2 1 1 1
6 2 1 -1
-2 2 1 7

输出样例:

[[2.0, 1.0, 1.0, 1.0], [6.0, 2.0, 1.0, -1.0], [-2.0, 2.0, 1.0, 7.0]]
2.0, 1.0, 1.0 = 1.0
6.0, 2.0, 1.0 = -1.0
-2.0, 2.0, 1.0 = 7.0
import java.util.Scanner;
import java.util.Arrays;
public class Main{
    public static void main(String[] args){
        int n;
        Scanner sc=new Scanner(System.in);
        n=sc.nextInt();
        double b[][]=new double[n][n+1];
        int i,j;
        for(i=0;i<n;i++){
            for(j=0;j<n+1;j++){
                b[i][j]=sc.nextDouble();
            }
        }
        System.out.println(Arrays.deepToString(b));
        for(i=0;i<n;i++){
            StringBuilder str=new StringBuilder();
            for(j=0;j<n;j++){
                str.append(b[i][j]);
                str.append(", ");
            }
            str.delete(str.length()-2,str.length());
            str.append(" = ");
            str.append(b[i][n]);
            System.out.println(str);
        }
    }
}

7-6 jmu-Java-01入门-第一个PTA上Java程序

本题目要求读入若干对整数a和b,然后输出它们的和。

输入格式:

在一行中给出一对整数a和b。

以下输入样例只有两对,实际测试数据可能有多对值。

输出格式:

对每一组输入,如果a的绝对值>1000,输出|a|>1000,否则输出a+b的值。

输入样例:

18 -299
1001 -9
-1001 8

输出样例:

-281
|a|>1000
|a|>1000
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        int a,b;
        Scanner sc=new Scanner(System.in);
        while(sc.hasNextInt()){
            a=sc.nextInt();
            b=sc.nextInt();
            if(Math.abs(a)>1000){
                System.out.println("|a|>1000");
            }
            else{
                System.out.println(a+b);
            }
        }
    }
}

7-7 jmu-Java-01入门-开根号

使用逐步逼近法对给定数值x求开根号。

逐步逼近法说明:从0开始逐步累加步长值。

步长=0.0001,epsilon(误差)=0.0001

循环继续的条件:

平方值<x 且 |x-平方值| > epsilon

###说明与参考

  1. 数值输出保留6位小数,使用System.out.printf("%.6f\n")

  1. 求平方,参考Math.pow函数。

  1. 输入值<0时,返回Double.NaN

输入格式:

任意数值

输出格式:

对每一组输入,在一行中输出其开根号。保留6位小数

输入样例:

-1
0
0.5
0.36
1
6
100
131

输出样例:

NaN
0.000000
0.707100
0.600000
1.000000
2.449500
10.000000
11.445600
import java.util.Scanner;
import java.lang.Math;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        double a;
        double b;
        while(sc.hasNext()){
            a=sc.nextFloat();
            b=0;
            if(a<0)
                System.out.println("NaN");
            else{
                while(b*b<a&&Math.abs(a-b*b)>0.0001){
                    b+=0.0001;
                }
                System.out.printf("%.6f\n",b);
            }
        }
    }
}

7-8 jmu-Java-02基本语法-03-身份证排序

  1. 输入n,然后连续输入n个身份证号。

  1. 然后根据输入的是sort1还是sort2,执行不同的功能。输入的不是sort1或sort2,则输出exit并退出。
    输入sort1,将每个身份证的年月日抽取出来,按年-月-日格式组装,然后对组装后的年-月-日升序输出。
    输入sort2,将所有身份证按照里面的年月日升序输出。

注意:处理输入的时候,全部使用Scanner的nextLine()方法,以免出错。

输入样例:

6
410425198309308225
320203197206115011
431227196108033146
330226196605054190
34080019810819327X
320111197112301539
sort1
sort2
e

输出样例:

1961-08-03
1966-05-05
1971-12-30
1972-06-11
1981-08-19
1983-09-30
431227196108033146
330226196605054190
320111197112301539
320203197206115011
34080019810819327X
410425198309308225
exit
import java.util.Scanner;
import java.util.Arrays;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
            int n;
            String b;
            n=sc.nextInt();
            sc.nextLine();
            String s[]=new String[n];
            String birth[]=new String[n];
          String str;
        int i,j;
        for(i=0;i<n;i++){
            s[i]=sc.nextLine();
        }
        while(sc.hasNext()){
            str=sc.nextLine();
            if(str.equals("e")){
                System.out.println("exit");
                break;
            }
            else if(str.equals("sort1")){
                for(i=0;i<n;i++){
                    birth[i]=s[i].substring(6,10)+"-"+s[i].substring(10,12)+"-"+s[i].substring(12,14);
                }
                Arrays.sort(birth);
                for(i=0;i<n;i++){
                    System.out.println(birth[i]);
                }
            }
            else if(str.equals("sort2")){
                for(i=0;i<n;i++){
                    birth[i]=s[i].substring(6,10)+s[i].substring(10,12)+s[i].substring(12,14);
                }
                Arrays.sort(birth);
                for(i=0;i<n;i++){
                   for(j=0;j<n;j++){
                       if(s[j].contains(birth[i])){
                           System.out.println(s[j]);
                       }
                   }
                }
            }
        }
   }
}

7-9 jmu-Java-02基本语法-04-动态数组

根据输入的n,打印n行乘法口诀表。

需要使用二维字符串数组存储乘法口诀表的每一项,比如存放1*1=1.

为了保证程序中使用了二维数组,需在打印完乘法口诀表后使用Arrays.deepToString打印二维数组中的内容。

提醒:格式化输出可使用String.format或者System.out.printf。

输出格式说明

  1. 每行末尾无空格。

  1. 每一项表达式之间(从第1个表达式的第1个字符算起到下一个表达式的首字符之间),共有包含7个字符。如2*1=2 2*2=4从第1个2开始到第二项``2*2=4`首字母之间,总共有7个字符(包含空格,此例中包含2个空格)。

输入样例:

2
5

输出样例:

1*1=1
2*1=2  2*2=4
[[1*1=1], [2*1=2, 2*2=4]]
1*1=1
2*1=2  2*2=4
3*1=3  3*2=6  3*3=9
4*1=4  4*2=8  4*3=12 4*4=16
5*1=5  5*2=10 5*3=15 5*4=20 5*5=25
[[1*1=1], [2*1=2, 2*2=4], [3*1=3, 3*2=6, 3*3=9], [4*1=4, 4*2=8, 4*3=12, 4*4=16], [5*1=5, 5*2=10, 5*3=15, 5*4=20, 5*5=25]]
import java.util.Scanner;
import java.util.Arrays;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int n;
        while(sc.hasNext()){
            n=sc.nextInt();
            String a[][]=new String[n][n];
            for(int i=0;i<n;i++){
                for(int j=0;j<=i;j++){
                    String str=String.format("%d*%d=%d",i+1,j+1,(j+1)*(i+1));
                    System.out.print(str);
                    a[i][j]=str;
                    if(j<i){
                        for(int k=0;k<7-str.length();k++)
                        System.out.print(" ");
                    }
                }
                System.out.printf("\n");
            }
            String s=new String(Arrays.deepToString(a));
            System.out.println(s.replace(", null",""));
        }
    }
}

7-10 jmu-Java-02基本语法-06-枚举

定义一个枚举类型Grade来表示考试的4个等级,值包含A,B,C,D,E

编写一个函数Grade getGrade(int score)将传递进来的score转化为枚举类型

>=90 and <=100返回A,

>=80 and <90 返回B,

>=70 and <80 返回C,

>=60 and <70返回D,

其他的返回E

#main方法

输入分数后,调用getGrade函数返回相应的Grade,使用switch根据Grade,根据分数打印不同的评价:

Excellent Good Average Fair Poor

并且每输出一个评语后,要调用如下函数

public static void printGradeInfo(Grade grade){
       System.out.println("class name="+Grade.class);
       System.out.println("grade value="+grade);
}

输入样例:

90
80
70
60

输出样例:

Excellent
class name=class Grade
grade value=A
Good
class name=class Grade
grade value=B
Average
class name=class Grade
grade value=C
Fair
class name=class Grade
grade value=D
import java.util.Scanner;
enum Grade{
    A,B,C,D,E;
}
public class Main{
    public static Grade getGrade(int a){
        if(a>=90&&a<=100)
            return Grade.A;
        else if(a>=80&&a<90)
            return Grade.B;
        else if(a>=70&&a<80)
            return Grade.C;
        else if(a>=60&&a<70)
            return Grade.D;
        else 
            return Grade.E;
    }
    public static void printGradeInfo(Grade grade){
        System.out.println("class name="+Grade.class);
        System.out.println("grade value="+grade);
    }
    public static void main(String[] args){
        int a=0;
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
            a=sc.nextInt();
        switch(getGrade(a)){
            case A:System.out.println("Excellent");printGradeInfo(getGrade(a));break;
            case B:System.out.println("Good");printGradeInfo(getGrade(a));break;
            case C:System.out.println("Average");printGradeInfo(getGrade(a));break;
            case D:System.out.println("Fair");printGradeInfo(getGrade(a));break;
            case E:System.out.println("Poor");printGradeInfo(getGrade(a));break;
        }
        }
    }
}

7-11 jmu-Java-02基本语法-07-大整数相加

有若干大整数,需要对其进行求和操作。

输入格式

每行输入一个字符串代表一个大整数,连续输入若干行,当某行字符为eE时退出。

输入样例:

42846280183517070527831839425882145521227251250327
55121603546981200581762165212827652751691296897789
e

输出样例:

97967883730498271109594004638709798272918548148116
import java.util.Scanner;
import java.math.BigInteger;
public class Main{
    public static void main(String[] args){
        BigInteger a=new BigInteger("0");
        Scanner sc=new Scanner(System.in);
        String str=new String();
        while(sc.hasNext()){
            str=sc.next();
            if(str.equals("e")||str.equals("E"))
                break;
           BigInteger b=new BigInteger(str);
            a=a.add(b);
        }
        System.out.println(a);
    }
}

7-12 jmu-Java-01入门-格式化输入输出与字符串

###本题主要考察

  • 使用Scanner处理输入

  • 使用System.out.printf进行格式化输出

  • String常用方法与字符串常用操作

main

###输入说明:

  • 输入double,然后输入3个浮点数。输出:从左到右依次输出3个double(均保留2位小数输出,宽度为5),格式依次为:右侧填充空格,左侧填充空格,直接输出

  • 输入int,然后输入3个整数(以1个或多个空格分隔)。输出:将3个整数相加后输出。

  • 输入str,然后输入3个字符串。输出:去除空格,然后倒序输出3个字符。

  • 输入line,然后输入一行字符串。输出:转换成大写后输出。

  • 如果输入不是上面几个关键词,输出:输出other

###输出说明

choice=你输入选项

该选项对应的输出内容

###提示

  1. 可使用line.split("\\s+");将以1个或多个空格分隔开的字符串分割并放入字符串数组。

  1. Scanner.nextLine与Scanner的其他next函数混用有可能出错。

输入样例:

double
1.578 3.0 3.14259
line
aaaaaaaaaa
int
1      2    3
str
321 654 987
line
dddddddddd
end

输出样例:

choice=double
1.58 , 3.00,3.14
choice=line
AAAAAAAAAA
choice=int
6
choice=str
987654321
choice=line
DDDDDDDDDD
choice=end
other
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        String str=new String();
        while(sc.hasNext()){
            str=sc.nextLine();
            if(str.equals("double")){
                double a1,a2,a3;
                a1=sc.nextDouble();
                a2=sc.nextDouble();
                a3=sc.nextDouble();
                System.out.println("choice="+str);
                System.out.printf("%-5.2f,%5.2f,%.2f\n",a1,a2,a3);
                sc.nextLine();
            }
            else if(str.equals("int")){
                int b1,b2,b3;
                b1=sc.nextInt();
                b2=sc.nextInt();
                b3=sc.nextInt();
                int b4=b1+b2+b3;
                System.out.println("choice="+str);
                System.out.println(b4);
                sc.nextLine();
            }
            else if(str.equals("str")){
                String c1,c2,c3;
                c1=sc.next();
                c2=sc.next();
                c3=sc.next();
                System.out.println("choice="+str);
                System.out.println(c3+c2+c1);
                sc.nextLine();
            }
            else if(str.equals("line")){
                String s=sc.nextLine();
                System.out.println("choice="+str);
                System.out.println(s.toUpperCase());
            }
            else{
                System.out.println("choice="+str);
                System.out.println("other");
            }
        }
    }
}

7-13 jmu-java-随机数-使用蒙特卡罗法计算圆周率的值

尝试使用蒙特卡罗法计算圆周率(π)的值。原理如下:

以原点(0, 0)作为圆心,半径为1画一个圆。该圆的外切正方形,边长为2。

现往该正方形内随机投点,数量足够多的情况下,落入圆内的点与落入整个

外切正方形的点的数量比值大概为: 4∗r2πr2,然后就可以得到π的值。

注意

  1. 请使用jdk库中的Random对象来生成随机数。

  1. 使用Math类中的sqrt与pow函数来计算开根号与平方值。

  1. 让点(x,y)投在整个矩形中,x与y的取值范围为(-1≤x<1, -1≤y<1)。

输入格式:

随机数种子seed 投点个数n

注意:seed为long型,n为int型

输出格式:

计算出的值圆周率的值

输入样例:

2 100000

输出样例:

3.14684
import java.util.Scanner;
import java.util.Random;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        long seed=sc.nextLong();
        double n=sc.nextInt();
        Random ra=new Random(seed);
        double inside=0;//落入圆内点数
        for(int i=0;i<n;i++){
            double x=ra.nextDouble()*2-1;
            double y=ra.nextDouble()*2-1;
            if(Math.pow(x,2)+Math.pow(y,2)<1){
                inside++;
            }
        }
        double k=inside/n;
        System.out.println(4*k);
    }
}

7-14 jmu-python-组合数

本题要求编写程序,根据公式

算出从n个不同元素中取出m个元素(m≤n)的组合数。

输入格式:

输入在一行中给出两个正整数m和n,以空格分隔。

  • 如果输入数据负数,能输出提示不能负数

  • 如果输入数据出发非法数据,能输出提示请输入数值 (数值异常处理)

输出格式:

按照格式result = 组合数计算结果输出。

输入样例:

2 7

输出样例:

result=21.00
import java.util.Scanner;
public class Main{
    public static int liu(int n){
        int i;
        int sum=1;
        for(i=1;i<=n;i++){
            sum=sum*i;
        }
        return sum;
    }
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        String str=sc.nextLine();
        char a[]=str.toCharArray();
        int i,j;
        int a1,a2;
        a1=0;a2=0;
        j=0;
        int k=0;
        int m=0;
        int n=0;
        int p=0;
        for(i=0;i<str.length();i++){
            if(a[i]<'0'||a[i]>'9'){
                if(a[i]!=' '&&a[i]!='-'){
                    System.out.println("请输入数值");
                    j=1;
                    break;
                }
            }
        }
        if(j==0){
        for(i=0;i<str.length();i++){
          if(a[i]==' '){
              k=1;
          }
            else if(a[i]>='0'&&a[i]<='9'){
             if(k==0){
                  a1=a1*10+Integer.valueOf(a[i])-'0';//m
                 p=1;
              }
             else{
                 a2=a2*10+Integer.valueOf(a[i])-'0';//n
                 n=1;
             }
            }
            else if(a[i]=='-'){
                m=1;
            }
        }
            if(m==1&&n==1&&p==1){
                System.out.println("不能负数");
            }
            else if(m==1&&n==0||m==1&&p==0){
                System.out.println("请输入数值");
            }
            if(m!=1){
                double sum=liu(a2)/(liu(a1)*liu(a2-a1));
                if(n==1&&p==1)
                System.out.printf("result=%.2f\n",sum);
            }
        }
    }
}

7-15 jmu-python-素数

输入一个数,判断其是否素数。素数是大于1的自然数中,除了1和它本身以外不再有其他因数。

输入格式:

输入一个整数

输出格式:

输出是否素数。

输入样例:

12

输出样例:

12 is not prime

输入样例:

7

输出样例:

7 is prime
import java.util.Scanner;
import java.lang.Math;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int j=0;
        for(int i=2;i<=Math.sqrt(n);i++){
            if(n%i==0){
                System.out.println(n+" is not prime");
                j=1;
            }
        }
        if(j==0&&n!=1&&n!=2)
        System.out.println(n+" is prime");
        if(n==1)
            System.out.println("1 is not prime");
        else if (n==2)
            System.out.println("2 is prime");
    }
}

以上13道基础题希望可以令大家有所提升!!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值