开始大学的java学习

116 阅读5分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

这篇文章属于是老文章了,是因为发现这次的可能新人创作礼要来不及了,所以用之前的文章继续来补一下


20210913 现在是上课的第二周,是因为上学期学过C语言还有python了嘛? 老师现在上的巨快,第三节课,居然已经上到类和对象了 欸 现在的进度完全超过了我的想象,暑假的东西已经不够用啦!!!!哭 现在自己现在的作业保存一下,下次慢慢梳理现在的知识点,看看有哪些还不太清楚的地方 —————————————————————————————— **

7-1 Your First Java Program (10 分) ** It is your first Java program. The string, 'This is my first Java program ’ , will be displayed on screen. 输入格式: No input

输出格式: This is my first Java program

输入样例: 在这里给出一组输入。例如:

输出样例 This is my first Java program

public class Main{
    public static void main(String[] args){
           //其他代码
        System.out.println("This is my first Java program");
    }
}

**

7-2 第一个JAVA程序 (10 分)

**
这是你的第一个Java程序,你需要在屏幕上显示“你好,Java。”

输入格式:

输出格式:
你好,Java。

输入样例:
在这里给出一组输入。例如:

输出样例:
在这里给出相应的输出。例如:

你好,Java。

public class Main{
    public static void main(String[] args){
           //其他代码`在这里插入代码片`
        System.out.println("你好,Java。");
    }
}

函数题

**

6-1 冒泡算法 (10 分)

**
冒泡排序是最简单的排序之一,通过与相邻元素的比较和交换来把小的数交换到最前面。请完成实现swap方法,来完成冒泡算法。

函数接口定义:
请参见代码内的相关引用

裁判测试程序样例:

public class Main{

    public static void main(String[] args) {

     int a[] = {17,15,9,10,1};
     BubbleSort.bubbleSort(a);
     for(int i=0; i<a.length; i++)
     {
         System.out.print(a[i]+" ");
    }

}
}
class BubbleSort {

    public static void bubbleSort(int[] arr) {
        if(arr == null || arr.length == 0)
            return ;
        for(int i=0;i<arr.length-1;i++) {
            for(int j=arr.length-1; j>i; j--) {
                if(arr[j]<arr[j-1]) {
                    swap(arr, j-1, j);
                }
            }
        }
    }

 /* 请在这里填写答案 */  
}


输入样例:
在这里给出一组输入。例如:

输出样例:
在这里给出相应的输出。例如:

1 9 10 15 17

     public static void swap(int[]arr,int i,int j) {
    	 int x=arr[i];
    	 arr[i]=arr[j];
    	 arr[j]=x;
     }


**

编程题 ** 7-1 计算π的值 (10 分) 在数学中,我们可以通过π=4╳(1-1/3+1/5-1/7+1/9-…1/(2╳n+1)) 来计算,n越大,计算值越靠近。输入正整数n来计算π。

输入样例: 在这里给出一组输入。例如:

100 结尾无空行 输出样例: 在这里给出相应的输出。例如:

3.1315929035585537 结尾无空行

import java .util.Scanner;
public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner reader=new Scanner(System.in);
		int n=reader.nextInt();
		int i;
		double x=1,y;
		for(i=1;i<n;i++)
		{
			if(i%2==1)
			{
				x=x-1.0/(1+2.0*i);
			}
			else
			{
				x=x+1.0/(1+2.0*i);
			}
		}
		y=x*4;
		System.out.print(y);
	}

}


7-2 求圆柱的面积和体积 (10 分) ** 输入圆柱的底面半径r和高l(double类型),输出圆柱的底面积和体积(double类型)。注意:计算中的π来自于Math。

输入样例: 在这里给出一组输入。例如:

5.01 4.02 结尾无空行 输出样例: 在这里给出相应的输出。例如:

The area is 78.85428976436916 The volume is 316.994244852764 结尾无空行

import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input=new Scanner(System.in);
		double x=input.nextDouble();
		double y=input.nextDouble();
		double s,v;
		s=Math.PI*x*x;
		v=s*y;
		System.out.println("The area is "+s);
		System.out.println("The volume is "+v);
	}

}


**

7-3 判断奇偶 (10 分) ** 输入一个整数,判断其奇偶性

输入格式: x(整数)

输出格式: x is even. (偶数) x is odd. (奇数)

输入样例: 在这里给出一组输入。例如:

4 结尾无空行 输出样例: 在这里给出相应的输出。例如:

4 is even. 结尾无空行

import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		int x=input.nextInt();
		if(x%2==0)
		{
			System.out.println(x +" is even.");
		}
		else {
			System.out.println(x +" is odd.");
		}
	}

}


** 7-6 中国生肖年 (5 分) ** 输入一个年份,输出该年的生肖属相。注:十二生肖为鼠、牛、虎、兔、龙、蛇、马、羊、猴、鸡、狗、猪。

输入样例: 在这里给出一组输入。例如:

2020 结尾无空行 输出样例: 在这里给出相应的输出。例如:

鼠 结尾无空行

啊啊啊啊啊啊啊啊 这题太丢脸了,懒得想,随便敲了一个最简单的代码,大家随便看看吧

import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		int x=input.nextInt();
		if((x-4)%12==0)
		{
			System.out.println("鼠");
		}
		if((x-4)%12==1) {
			System.out.println("牛");
		}
		if((x-4)%12==2) {
			System.out.println("虎");
		}
		if((x-4)%12==3) {
			System.out.println("兔");
		}
		if((x-4)%12==4)
		{
			System.out.println("龙");
		}
		if((x-4)%12==5)
		{
			System.out.println("蛇");
		}
		if((x-4)%12==6)
		{
			System.out.println("马");
		}
		if((x-4)%12==7) {
			System.out.println("羊");
		}
		if((x-4)%12==8) {
			System.out.println("猴");
		}
		if((x-4)%12==9) {
			System.out.println("鸡");
		}
		if((x-4)%12==10) {
			System.out.println("狗");
		}
		else if((x-4)%12==11){
			System.out.println("猪");
		}
	}

}

—————————————————————————————— **

7-7 打印乘法表 (5 分) ** 输出10以内的乘法表,输入为1-20的整数,输出请参照输出样例。打印的时候,一个数占4位。

输入样例: 在这里给出一组输入。例如:

5 结尾无空行 输出样例: 在这里给出相应的输出。例如:

1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 5 10 15 20 25 结尾无空行

import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		int x=input.nextInt(),i,j,s;
		for(i=1;i<=x;i++) {
			for(j=1;j<=x;j++) {
				s=i*j;
				System.out.printf("%4d",s);
			}
			System.out.println();
		}
	}

}