1.
编写程序,声明一个method方法,在方法中打印一个10*8型矩形,计算该矩形的面积,并将其作为方法返回值,在main方法中调用该方法。
代码:
package zzx.com;
public class yyds {
public static void main(String[] args) {
yyds m = new yyds();
System.out.println(m.method());
}
public double method() {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 8; j++) {
System.out.print("*");
}
System.out.println();
}
return 10 * 8;
}
}
回顾:
①根据题意:需要声明一个方法时,在main方法中直接声明就可以,不需要再定义类,然后在类中再定义方法。
2.
在method方法中提供m和n两个参数,方法中打印一个m*n的*型矩形,计算该矩形的面积,将其作为方法返回值。在main方法中调用该方法,接受返回的面积值并打印。
错误代码:
package zzx.com;
import java.util.Scanner;
public class yyds {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
yyds y = new yyds();
System.out.println("矩形的长为:");
y.m = in.nextInt();
y.n = in.nextInt();
}
public int method() {
int m;
int n;
for(int i = 0;i < m;i++){
for(int j = 0;j < n;j++){
System.out.print("*");
}
System.out.println();
}
return m * n;
}
}
错误原因:
①类中的变量属于:局部变量,只能在类中使用
代码:
package zzx.com;
import java.util.Scanner;
public class yyds {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
yyds y = new yyds();
System.out.println("面积为:" + y.method(3,6));
}
public int method(int m ,int n) {
for(int i = 0;i < m;i++){
for(int j = 0;j < n;j++){
System.out.print("*");
}
System.out.println();
}
return m * n;
}
}
3.
定义类Student,包含三个属性:学号number(int),年级state(int),成绩score(int)。创建20个学生对象,学号为1~20,年级(1~6)和成绩([0,100])都由随机数确定。
问题一:遍历所有学生信息。
代码一:
package zzx.com;
public class yyds {
public static void main(String[] args) {
Student[] s = new Student[20];
for(int i = 0;i < s.length; i++){
//给数组元素开辟所指向的空间(即给数组元素赋值)
s[i] = new Student();
s[i].number = i + 1;
s[i].state = (int)(Math.random() * 6 + 1);
s[i].score = (int)(Math.random() * (100 - 0 + 1));
}
for(int j = 0;j < s.length;j++){
System.out.println("学号:" + s[j].number + "年级:" + s[j].state + "成绩:" + s[j].score);
}
}
}
class Student{
int number;//学号
int state;//年级
int score;//成绩
}
代码二:
package zzx.com;
public class yyds {
public static void main(String[] args) {
Student[] s = new Student[20];
for(int i = 0;i < s.length; i++){
//给数组元素开辟所指向的空间(即给数组元素赋值)
s[i] = new Student();
s[i].number = i + 1;
s[i].state = (int)(Math.random() * 6 + 1);
s[i].score = (int)(Math.random() * (100 - 0 + 1));
}
for(int j = 0;j < s.length;j++){
System.out.println(s[j].shuchu());
}
}
}
class Student{
int number;//学号
int state;//年级
int score;//成绩
//显示学生信息的方法
public String shuchu(){
return "学号:" + number + ",年级:" + state + ",成绩" + score;
}
}
4.
问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息。
代码:
package zzx.com;
public class yyds {
public static void main(String[] args) {
Student[] s = new Student[20];
for(int i = 0;i < s.length; i++){
//给数组元素开辟所指向的空间(即给数组元素赋值)
s[i] = new Student();
s[i].number = i + 1;
s[i].state = (int)(Math.random() * 6 + 1);
s[i].score = (int)(Math.random() * (100 - 0 + 1));
}
//使用冒泡排序按学生成绩排,并遍历所有学生的信息
for(int i = 0;i < s.length - 1;i++){
for(int j = 0;j < s.length - 1 - i;j++){
if(s[j].score > s[j + 1].score){
//交换的是数组的元素,Student的对象
Student temp = s[j];
s[j] = s[j + 1];
s[j + 1] = temp;
}
}
}
for(int i = 0;i < s.length;i++){
System.out.println(s[i].shuchu());
}
}
}
class Student{
int number;//学号
int state;//年级
int score;//成绩
//显示学生信息的方法
public String shuchu(){
return "学号:" + number + ",年级:" + state + ",成绩" + score;
}
}