1. 循环练习for语句 // 2020年1月24日
题目:
从1循环到150 并在每一行后面打印一个值,另外在每个3的倍数行上打印出“foo",在每个5的倍数行上打印"biz",在每个7的倍数行上打印输出"baz"
题目分析心得与讲解:
我们发现这是一个循环语句,只不过在循环内部还有判断if语句来实现题目的要求,我们运用%取余号来判断是否是倍数。
代码如下:
public class Diyiti20200124 {
/**
MADE BY 贪玩巴斯
20200124 23:49
*/
public static void main(String[] args) {
// TODO 自动生成的方法存根
for(int i = 1; i <= 150; i++) {
System.out.print("i = " + i +" ");
if ( i % 3 == 0)
{
System.out.print(" foo ");
}
if ( i % 5 == 0)
{
System.out.print(" biz ");
}
if ( i % 7 == 0)
{
System.out.print(" baz ");
}
System.out.println("");
}
}
}
2. 用JAVA实现九九乘法表 //2021年1月25日
题目:
/*
九九乘法表
1 * 1 = 1
2 * 1 = 2 2 * 2 = 4
。。。
9 * 1 = 9 。。。9 * 9 = 81
*/
题目分析心得与讲解:
我们首先要看出来这是一个循环语句实现的程序,再者我们可以发现它是(外行内列)由外层循环控制的有九行的程序,每一行的列数(也就是它的内层循环)是和它的行数有关系的。
代码如下:
/**
Made by 贪玩巴斯;
2021年1月25日
*/
public class Dierti20210126 {
public static void main(String[] args) {
// TODO 自动生成的方法存根
for(int i = 1; i <= 9; i++ ) {
for(int j = 1; j <= i; j++) {
System.out.print( i + " + " + j + " = " + (i+j) + " " );
}
System.out.println("");
}
}
}
程序实现截图如下:
3. 用JAVA打印*号组成的菱形图案 //2021年1月26日
题目:
运用 * 打印这样一个菱形。
题目分析心得与讲解:
我们可以看出,这是一个需要运用循环结构的程序,并且具有内层循环和外层循环,然后根据我们的口诀——外行内列,再去发现它的每一行与这一行的列数(就是 * 的个数)之间具有的联系。
我们还需要注意必须把它分成上下两部分来写,这样才能构建行数与列数的练习。否则无法构建联系
代码如下:
public class Disanti20210127 {
/**
*
* @Description
* @author AchesonD 贪玩巴斯
* @date 2021年1月27日下午10:10:41
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成的方法存根
for(int i = 1; i <= 5; i++) {
for(int k = 1; k <= 5 - i ; k++) {
System.out.print(" ");
}
for(int j = 1; j <= i; j++) {
System.out.print("*" + " ");
}
System.out.println("");
}
for(int i = 1; i <= 4; i++) {
for(int k = 1; k <= i ; k++) {
System.out.print(" ");
}
for(int j = 1; j <= 5 - i; j++) {
System.out.print("*" + " ");
}
System.out.println("");
}
}
}
代码截图:
4.一维数组的使用:从键盘输入学生成绩与个数,找出最高分,并且判断等级// 2021年1月27日
题目:
题目分析与心得理解:
建立int型的数组来存储数据。
代码如下:
import java.util.Scanner;
public class Disiti20210127 {
/**
*
* @Description
* @author AchesonD 贪玩巴斯
* @date 2021年1月27日下午23:03:12
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成的方法存根
Scanner scan = new Scanner(System.in);
System.out.println("请输入本次考试学生人数:");
int number = scan.nextInt();
System.out.println("请输入" + number + "个学生的成绩:(以回车结束)");
int[] scores = new int[number];
int maxScore = 0;
for(int i = 0; i < scores.length; i++){
scores[i] = scan.nextInt();
if(maxScore <= scores[i]) {
maxScore = scores[i];
}
}
System.out.println("最高分是:" + maxScore);
char level;
for(int i = 0; i< scores.length; i++) {
if(maxScore - scores[i] <= 10) {
level = 'A';
}else if(maxScore - scores[i] <= 20) {
level = 'B';
}else if(maxScore - scores[i] <= 30) {
level = 'C';
}else {
level = 'D';
}
System.out.println(" student " + i + " score is " + scores[i] + " grade is " + level);
}
}
}
代码截图:
5.一道简单的二维数组的题目,求数组中所有元素的和//2021年1月28日
题目:
获取图表中arr数组中所有元素的和。
题目分析与心得理解:
很简单使用for循环即可,但是需要注意在运用二维数组的时候,我们要选择动态的定义方法。因为内层数组的长度不同。
代码如下:
public class Diwuti20200128 {
/**
*
* @Description
* @author AchesonD 贪玩巴斯
* @date 2021年1月28日下午11:19:17
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成的方法存根
int[][] arr = new int[][] {{3,5,8},{12,9},{7,0,6,4}};
int add = 0 ;
for(int i = 0; i < arr.length; i++) {
for(int j = 0; j < arr[i].length; j++) {
add += arr[i][j];
}
}
System.out.println("则arr数组中所有元素之和=" + add);
}
}
结果截图:
6.JAVA的多维数组实现杨辉三角 //2021年1月29日
题目:
使用二维数组打印一个10行的杨辉三角。
题目分析与心得理解:
代码如下:
public class Diliuti20200129 {
/**
*
* @Description
* @author AchesonD 贪玩巴斯
* @date 2021年1月29日下午9:59:33
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成的方法存根
//声明并初始化二维数组
int[][] yanghui = new int[10][];
//给数组的元素赋值
for(int i = 0; i < yanghui.length; i++) {
yanghui[i] = new int[i+1];
//一行中 首末元素相同,都为1
yanghui[i][0] = yanghui[i][i] = 1;
//给一行中除了首末元素的其他元素赋值
if (i>1) {
for(int j = 1; j< yanghui[i].length - 1; j++ ) {
yanghui[i][j] = yanghui[i-1][j-1] + yanghui[i-1][j];
}
}
}
for(int i = 0; i < yanghui.length; i++){
for(int j = 0; j < yanghui[i].length; j++) {
System.out.print(yanghui[i][j] + " ");
}
System.out.println();
}
}
}
代码截图:
7.JAVA的类、方法的第一次初级练习 //2021年2月9日
题目:
题目分析与心得理解:
我在本题中第一次做了一个类的练习,在类中定义了属性和方法,并且创建了两个对象来test;
在写程序的过程中,我发现我有的时候会忘掉();在方法后面或者是在 定义类的对象时候的最后 会忘记括号。
通过这个简单的入门的初级的练习,我们可以对类有一个更好的掌握!
我在一个包package下写了两个class类,因为一般情况下就是一个class里面就最好只有一个源代码。
代码如下:
创建一个包 com.diqiti.java
创建第一个class Person
package com.diqiti.java;
/*
* 要求:
* (1)创建Person类的对象,设置该对象的name、age和sex属性,调用study方法,
* 输出字符串“studying”,调用showAge()方法显示age值,
* 调用addAge()方法给对象的age属性值增加2岁。
* (2)创建第二个对象,执行上述操作,体会同一个类的不同对象之间的关系。
*
*
*/
/**
*
* @Description
* @author AchesonD 贪玩巴斯
* @version
* @date 2021年2月9日下午10:38:21
*/
public class Person {
//创建类,并设计类的成员
String name;
int age;
/**
* sex = 1;表明是男性
* sex = 0;表明是女性
*/
int sex;
//构造方法,方法那里要写空格
public void study(){
System.out.println("studying");
}
public void showAge(){
System.out.println("此人的年龄为" + age);
}
public int addAge(int i) { //注意
age += i;
return age; //注意
}
}
创建第二个class PersonTest
package com.diqiti.java;
public class PersonTest {
public static void main(String args[]){
Person p1 = new Person(); //不要忘记最后的()
//定义属性、调用方法
p1.name = "吴彦祖";
p1.age = 18;
p1.sex = 1 ;
p1.study();
p1.showAge();
p1.addAge(2);
p1.showAge();
int newage = p1.addAge(2);
System.out.println(p1.name + "的新年龄为:" + newage);
Person p2 = new Person();
p2.name = "刘德华";
p2.age = 19;
p2.sex = 1 ;
p2.study();
p2.showAge();
int newage2 = p2.addAge(2);
System.out.println(p2.name + "的新年龄为:" + newage2);
}
}
代码结果截图:
8.用JAVA创建类的方法求一个圆的面积 //2021年2月10日
题目要求:
利用面向对象的编程方法,设计类Circle计算圆的面积。 圆的半径为 15
题目分析与心得理解:
这道题是运用构建类的方法来求一个半径为15的圆的面积。
在编写的过程中,我们的测试类一定要是public的主类这样,并且名字要和程序名一致。程序才能够运行。
代码如下:
package pers.AchesonD.circle;
/*
* 题目:
* 利用面向对象的编程方法,设计类Circle计算圆的面积。 圆的半径为 15
*/
/**
*
* @Description
* @author AchesonD 贪玩巴斯
* @version
* @date 2021年2月10日下午6:02:25
*/
public class FCircle {
public static void main(String[] args) {
Circle c1 = new Circle();
c1.radius = 15;
c1.circle();
System.out.println("半径为 " + c1.radius + " 的圆的面积为 " + c1.circle());
}
}
class Circle {
double radius;
public double circle() {
double area;
area = Math.PI * radius * radius ;
return area;
}
}
代码截图:
9.用JAVA构建方法来打印一个10*8的矩形以及打印和求m*n的矩形的面积 //2021年2月10日
题目:
3.1 编写程序,声明一个method方法,在方法中打印一个10*8 的*型矩形,在main方法中调用该方法。
3.2 修改上一个程序,在method方法中,除打印一个10*8的*型矩形外,再计算该矩形的面积,
并将其作为方法返回值。在main方法中调用该方法,接收返回的面积值并打印。
3.3 修改上一个程序,在method方法提供m和n两个参数,方法中打印一个m*n的*型矩形,
并计算该矩形的面积, 将其作为方法返回值。在main方法中调用该方法,接收返回的面积值并打印。
题目分析与心得理解:
在调用方法
//test.method2();
System.out.println("这个矩形的面积是" + test.method2());
的时候,如果第一排的test.method2() 没有去除的话会有两次调用方法。会重复调用
//3.1测试
// test.method();
//3.2测试
//方式一:
// int area = test.method();
// System.out.println("面积为:" + area);
//方式二:
// System.out.println(test.method());
//3.3测试
int area = test.method(12, 10);
System.out.println("面积为:" + area);
有两种方法调用,要自己体会他们的不同。
代码如下:
package pers.AchesonD.dijiuti;
/*
* 3.1 编写程序,声明一个method方法,在方法中打印一个10*8 的*型矩形,在main方法中调用该方法。
* 3.2 修改上一个程序,在method方法中,除打印一个10*8的*型矩形外,再计算该矩形的面积,
* 并将其作为方法返回值。在main方法中调用该方法,接收返回的面积值并打印。
*
* 3.3 修改上一个程序,在method方法提供m和n两个参数,方法中打印一个m*n的*型矩形,
* 并计算该矩形的面积, 将其作为方法返回值。在main方法中调用该方法,接收返回的面积值并打印。
*/
/**
*
* @Description
* @author AchesonD 贪玩巴斯
* @version
* @date 2021年2月10日下午8:08:24
*/
public class Dijiuti {
public static void main(String[] args) {
Dijiuti test = new Dijiuti();
System.out.println("下面是3.1的测试\n");
test.method();
System.out.println("\n下面是3.2的测试\n"); // \n加在""引号里面是换行
//test.method2();
System.out.println("这个矩形的面积是" + test.method2());
System.out.println("\n下面是3.3的测试\n");
//test.method3(5,6);
System.out.println("这个矩形的面积是" + test.method3(5,6));
}
//3.1 编写程序,声明一个method方法,在方法中打印一个10*8 的*型矩形,在main方法中调用该方法。
public void method() {
for(int i = 1; i <= 10; i++) {
for(int j = 1; j<= 8; j++) {
System.out.print("* ");
}
System.out.println("");
}
}
//3.2 修改上一个程序,在method2方法中,除打印一个10*8的*型矩形外,再计算该矩形的面积,
//并将其作为方法返回值。在main方法中调用该方法,接收返回的面积值并打印。
public int method2() {
for(int i = 1; i <= 10; i++) {
for(int j = 1; j<= 8; j++) {
System.out.print("* ");
}
System.out.println("");
}
return 10 * 8;
}
//3.3 修改上一个程序,在method3方法提供m和n两个参数,方法中打印一个m*n的*型矩形,
//并计算该矩形的面积, 将其作为方法返回值。在main方法中调用该方法,接收返回的面积值并打印。
public int method3(int m, int n) {
for(int i = 1; i <= m; i++) {
for(int j = 1; j<= n; j++) {
System.out.print("* ");
}
System.out.println("");
}
return m * n;
}
}
代码截图:
10.用JAVA来实现学生学号,年级,成绩随机值,并输出指定内容,以及冒泡法排序的程序 //2021年2月10日
题目:
4. 对象数组题目:
定义类Student,包含三个属性:学号number(int),年级state(int),成绩score(int)。
创建20个学生对象,学号为1到20,年级和成绩都由随机数确定。
问题一:打印出3年级(state值为3)的学生信息。
问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息
提示:
1) 生成随机数:Math.random(),返回值类型double;
2) 四舍五入取整:Math.round(double d),返回值类型long。
公式:
* [10,99] b-a + 1 + a random得到的是double型的
* 公式:(int)(Math.random() * (99 - 10 + 1) + 10)
题目分析与心得理解:
写累了。睡了
/**
*
* @Description
* @author AchesonD 贪玩巴斯
* @version
* @date
* 2021年2月10日下午9:19:24 到 11:34:31
*/
找一个错误找了半天!
代码如下:
package pers.AchesonD.dishiti;
/*
* 4. 对象数组题目:
定义类Student,包含三个属性:学号number(int),年级state(int),成绩score(int)。
创建20个学生对象,学号为1到20,年级和成绩都由随机数确定。
问题一:打印出3年级(state值为3)的学生信息。
问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息
提示:
1) 生成随机数:Math.random(),返回值类型double;
2) 四舍五入取整:Math.round(double d),返回值类型long。
公式:
* [10,99] b-a + 1 + a random得到的是double型的
* 公式:(int)(Math.random() * (99 - 10 + 1) + 10)
*/
/**
*
* @Description
* @author AchesonD 贪玩巴斯
* @version
* @date
* 2021年2月10日下午9:19:24 到 11:34:31
*/
public class Student {
public static void main(String[] args) {
//声明student类型的数组
//创建20个学生对象,学号为1到20,年级和成绩都由随机数确定。
studentinformation[] stus = new studentinformation[20]; //这里表示的是构造一个studentinformation类的数组
for(int i = 0; i < stus.length; i++) {
stus[i] = new studentinformation(); //这一步是关键
//学号
stus[i].number = (i + 1);
//年级1-6和成绩0-100都由随机数确定
//提示:
//1) 生成随机数:Math.random(),返回值类型double;
//2) 四舍五入取整:Math.round(double d),返回值类型long。
//年级
stus[i].state = (int)(Math.random() * (6 - 1 + 1) + 1);
//成绩
stus[i].score = (int)(Math.random() * (100 - 0 + 1) + 0);
}
//问题一:打印出3年级(state值为3)的学生信息。
//问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息
//构建一个测试对象
Student test = new Student();
//遍历学生数组
test.print(stus);
System.out.println("********************");
//问题一:打印出3年级(state值为3)的学生信息。
test.searchState(stus, 3);
System.out.println("********************");
//问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息
test.sort(stus);
//遍历学生数组
test.print(stus);
}
//以下代码是将数组操作代码封装到方法中
/**
*
* @Description
* @author AchesonD 贪玩巴斯
* @date 2021年2月10日下午10:28:55
* @param stus
* @param i
*/
public void print(studentinformation[] stus) {
for(int i = 0; i < stus.length; i++){
System.out.println(stus[i].info());
}
}
//查找:打印出3年级(state值为3)的学生信息。的方法
/**
*
* @Description 打印出3年级(state值为3)的学生信息。的方法
* @author AchesonD 贪玩巴斯
* @date 2021年2月10日下午10:37:26
* @param stus 要遍历查找的数组
* @param state 要求打印的年级
*/
public void searchState(studentinformation[] stus, int state) {
for(int i = 0 ; i < stus.length; i++) {
if(state == stus[i].state) {
System.out.println(stus[i].info());
}
}
}
//使用冒泡排序按学生成绩排序,并遍历所有学生信息的方法
/**
*
* @Description 使用冒泡排序按学生成绩排序,并遍历所有学生信息的方法
* @author AchesonD 贪玩巴斯
* @date 2021年2月10日下午10:47:49
* @param stus 遍历的数组
*/
public void sort(studentinformation[] stus) {
for(int i = 0; i < stus.length - 1 ; i++) {
for(int j = 0 ; j < stus.length - 1 - i; j++) {
if(stus[j].score > stus[j + 1].score) {
studentinformation temp = stus[j];
stus[j] = stus[j+1];
stus[j+1] = temp;
}
}
}
}
}
class studentinformation{
//定义类Student,包含三个属性:学号number(int),年级state(int),成绩score(int)。
int number; //学号number(int)
int state; //年级state(int)
int score; //成绩score(int)
public String info() {
return "这位同学的学号是:" + number + ",年级是:" + state + ",成绩是:" + score;
}
}
代码截图如下: