实验四 采用两个一维数组输出等腰三角形的杨辉三角
package 杨辉三角;
import java.util.Scanner;
public class 杨辉三角0 {
public static void main(String[] args)
{
Scanner a = new Scanner(System.in);
System.out.print("输入杨辉三角的行数n:");
int n=a.nextInt();
int[] b=new int[100]; //创建整形数组,最大可存放100个整形
int[] c=new int[100]; //创将两个数组,一个用来存放上一层的数据,另一个用来计算并保存本层数据
int i,x,y;
for(i=0;i<100;i++) //将两个数组全部初始化,数据全为1
b[i]=c[i]=1;
for(i=0;i<n;i++)
{
for(x=1;x<i-1;x++)
{
c[x]=b[x-1]+b[x]; //计算杨辉三角的过程
}
for(y=0;y<=n-i;y++) //每行开头输出空格规范格式
System.out.print(" ");
for(x=0;x<=i-1;x++)
{b[x]=c[x]; //进入下一层之前把本层数据转为上一层数据,给接下来的计算使用
System.out.print(c[x]+" ");}
System.out.print("\n"); //每输出一行自动换行
}
}
}
总结心得:
(1)创建静态数组时,一定要定义数组的大小,我这里定义的大小为100
(2)计算杨辉三角时,要注意两个数组之间的关系,即c[x]=b[x-1]+b[x]
(3)嵌套的for循环作用:外层for循环是控制杨辉三角层数,内层for循环是具体的计算每个元素的数值
(4)输出格式,即开头的空格数,层数越多,开头的空格越少,用y<=n-i来控制次数
(5)这一步是最容易出错的,就是把计算完成的一行转成上一层并进行下一层的计算,一定不能直接用b=c来赋值,要用for循环一个一个元素的赋值
实验五 任意输入10个int类型数据,排序输出,再找出素数
实验心得:
1.素数输出时不理解简单算法,只能用一种比较笨的方法输出结果。
2.排序输出的时候,用Java的方法输出时,语句使用上有一点不熟悉。
实验六:类的封装
import java.util.Scanner;
public class Account {
public int id;
public String name;
public int password;
public int money;
public Account(int id, String name, int password, int money) {
this.id = id;
this.name = name;
this.password = password;
this.money = money;
}
public void Display(){
System.out.println("账户:" + id);
System.out.println("姓名:" + name);
System.out.println("余额:" + money);
}
public void takeMoney(){
while(true){
Scanner sc = new Scanner(System.in);
System.out.println("请输入密码进行验证!");
int pass = sc.nextInt();
if(pass == password){
System.out.println("请输入需要取款的金额:");
int withdrawals = sc.nextInt();
if(withdrawals <= money) {
money= money-withdrawals;
System.out.println("账户余额:" + money);
}else {
System.out.println("当前余额不足!");
}
break;
}else {
System.out.println("你输入的密码有误,请重新输入!");
}
sc.close();
}
}
public void saveMoney(int inmoney){
money = money + inmoney;
System.out.println("此次存款为:" + inmoney);
System.out.println("账户余额:" + money);
}
public static void main(String[] args) {
Account acc = new Account(10000,"小明",123456,100000);
Scanner sc = new Scanner(System.in);
while(true) {
System.out.println("---欢迎进入银行账户操作系统---");
System.out.println("---------1银行账户信息--------");
System.out.println("---------2取款操作------------");
System.out.println("---------3存款操作------------");
System.out.println("---------4退出系统------------");
System.out.println("------------------------------");
int choice = sc.nextInt();
switch(choice) {
case 1:
System.out.println("---银行账户信息---");
acc.Display();
break;
case 2:
System.out.println("---取款操作---");
acc.takeMoney();
break;
case 3:
System.out.println("---存款操作---");
acc.saveMoney(1000);
break;
case 4:
System.exit(0);
break;
default:
System.out.println("您的选择有误!");
break;
}
}
}
}
实验心得:
1、类使用成员变量存储表示对象属性和状态的数据,使用成员方法表示对数据的操作,成员变量和成员方法统称为类的成员。
2、【修饰符】 class 类 【父类】【implements 接口列表】
3、声明一个对象:类 对象
4、使用new运算符可以调用类的一个构造方法,创建该类的一个实例,为实例分配内存空间并初始化,再将该实例赋值给一个对象。
5、对象获得一个实例后,就可以使用”.”运算符,引用对象中的成员变量和调用成员方法了
实验七:
package 实验包;
import java.util.Scanner;
public class Student {
public int i=0;
public String xing,name,zy,nj,address;
public double date,score;
public String xuehao;
Scanner shuru=new Scanner(System.in);
public Student(){}
public Student(String xing,String name,String zy,String nj,String address,double date,double score)
{
this.xing=xing;
this.name =name;
this.zy=zy;
this.nj=nj;
this.address=address;
this.date=date;
this.score=score;
}
void name_chazhao(String temp) {
System.out.println(name+" "+zy+" "+nj+" "+address+" "+date+" "+score+" "+xuehao);
}
void date_chazhao(double temp) {
System.out.println(name +" "+zy+" "+nj+" "+address+" "+date+" "+score+" "+xuehao);
}
void address_chazhao(String temp) {
System.out.println(name+" "+zy+" "+nj+" "+address+" "+date+" "+score+" "+xuehao);
}
public static void main(String[] args) {
int i,j=1,t=1,w=0;
int length;
Student[] stu=new Student[5];//("李","李会通","网络工程","17级","海南万宁",1998,100);
stu[0]=new Student("李","李会通","网络工程","17级","海南万宁",1998,100);
stu[1]=new Student("钟","钟若文","网络工程","17级","广西南宁",1997,100);
stu[2]=new Student("马","马元斌","网络工程","17级","青海西宁",1999,100);
stu[3]=new Student("白","白云超","网络工程","17级","青海西宁",1999,100);
stu[4]=new Student("牛","牛明旺","生物工程","17级","河北武安",1998,92);
String temp=stu[0].zy;
int []fuzhu=new int[100];
String []zhuanye=new String[100];
zhuanye[0]=stu[0].zy;
length=stu.length;
for(i=0;i<stu.length;i++){
if(stu[i].zy!=temp){
fuzhu[j]=j;
zhuanye[j++]=stu[i].zy;
}
}
for(i=0;i<length;i++){
stu[i].xuehao="20";
char [] stringArr =stu[i].zy.toCharArray();
for(j=0;j<stringArr.length;j++){
if(stringArr[j]>=‘0‘&&stringArr[j]<=‘9‘){
stu[i].xuehao+=stringArr[j];
}
for(w=0;w<zhuanye.length;w++){
if(stu[i].zy==zhuanye[w]){
stu[i].xuehao+=fuzhu[w];
break;
}
}
}
stu[i].xuehao+=t++;
}
Scanner shuru=new Scanner(System.in);
int choice=1;
while(choice!=0){
System.out.println("0.退出系统\n 1.按姓名查找\n 2.按年份查找\n 3.按省市查找\n 4.统计成绩");
System.out.println("请输入你的选择:");
choice=shuru.nextInt();
switch(choice){
case 0:System.exit(0);
case 1:System.out.println("请输入查找的姓:");
temp=shuru.next();
for(i=0;i<stu.length;i++) {
if(stu[i].xing.equals(temp)){
stu[i].name_chazhao(temp);
break;
}
}//for循环的括号
break;
case 2:System.out.println("请输入查找的年份:");
double temp2=shuru.nextDouble();
for(i=0;i<stu.length;i++){
if(stu[i].date==temp2) {
stu[i].date_chazhao(temp2);
}
}
break;
case 3:System.out.println("请输入查找的省市:");
temp=shuru.next();
for(i=0;i<stu.length;i++){
if(stu[i].address.contains(temp)==true) {
stu[i].address_chazhao(temp);
}
}
break;
case 4:double []num=new double[300];
for(i=0;i<300;i++){num[i]=0;}
for(i=0;i<stu.length;i++){
for(j=0;j<zhuanye.length;j++){
if(stu[i].zy==zhuanye[j]){
num[fuzhu[j]]+=stu[i].score;
}
}
}
for(i=0;i<num.length;i++){
if(num[i]!=0){
System.out.print(zhuanye[i]);
System.out.println("班的总分是:"+num[i]);
}
}
break;
}//选择结构的括号
}
}
}
总结心得:在类里面要用this指针初始化
实验八:
package 计算圆锥体面积;
class abc extends yuanzhuiti implements Area,Volume {
final double PI=3.14;
public double volume(double r,double h) {
double v;
v=r*h/3;
return v;
}
public double area(double r,double l) {
double a;
a=PI*r*l+PI*r*r;
return a;
}
}
public class yuanzhuiti{
public static void main(String[] args) {
abc a=new abc();
abc b=new abc();
System.out.println("圆锥体A的面积为:"+a.area(2, 4));
System.out.println("圆锥体A的体积为:"+a.volume(3, 6));
System.out.println("圆锥体B的面积为:"+b.area(3, 6));
System.out.println("圆锥体B的体积为:"+b.volume(4, 8));
System.out.println("体积较大的是:"+Math.max(a.area(3, 6), b.volume(4, 8)));
}
}
package 计算圆锥体面积;
public interface Area{
public abstract double area(double r,double l);
}
package 计算圆锥体面积;
public interface Volume {
public abstract double volume(double r,double h);
}
实验心得:只要明白接口的性质,就可以完成实验
实验九:
源代码:
package 抛出异常;
public class 实验 {
public static void main(String[] args) {
point p=new point(1,3);
point p1=new point(1,2);
point p2=new point(1,1);
rectangle r=new rectangle(p,5,6);
triangle t=new triangle(p,p1,p2);
}
}
class point {
public int x,y;
public point() {}
public point(int x,int y)throws IllegalArgumentException
{
this.x=x;
this.y=y;
if(x<0||y<0)
throw new IllegalArgumentException("无效参数");
}
}
class rectangle extends point{
public int width,length;
//public point point1(3,6);
public rectangle(point point1,int length,int width)throws IllegalArgumentException
{
this.length=length;
this.width=width;
if(length<0||width<0)
throw new IllegalArgumentException("参数无效");
}
}
class triangle extends point{
public triangle(point point1,point point2,point point3)throws IllegalArgumentException
{
if(((point1.x-point2.y)-(point2.x-point1.y))+((point2.x-point3.y)-(point3.x-point2.y))+((point3.x-point1.y)-(point3.y-point1.x))==0)
throw new IllegalArgumentException("无效的参数");
}
}
实验心得:明白了异常抛出的结果
实验十
package jisuanqi;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
class counter1 extends JFrame
{
public counter1()
{
super("计算器");
this.setSize(400,100);
this.setLocation(300,240);
this.setLayout(new FlowLayout());
TextField text1=new TextField(4);
text1.setText("1");
this.add(text1);
String proList[] = { "+","-","x" ,"%"};
TextField text;
JComboBox comboBox;
Container conPane = getContentPane();
comboBox = new JComboBox(proList);
comboBox.setEditable(true);
conPane.add(comboBox);
TextField text2=new TextField(4);
text2.setText("1");
this.add(text2);
JButton button = new JButton("=");
this.add(button);
TextField text3=new TextField(4);
text3.setText("2");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
String s=comboBox.getEditor().getItem().toString();
double a= Integer.parseInt(text1.getText());
double b= Integer.parseInt(text2.getText());
if(s.equals("+")) {
double t=a+b;
String m=String.valueOf(t);
text3.setText(m);
}
else if(s.equals("-"))
{double t=a-b;
String m=String.valueOf(t);
text3.setText(m);}
else if(s.equals("x"))
{double t=a*b;
String m=String.valueOf(t);
text3.setText(m);}
else
{double t=a/b;
String m=String.valueOf(t);
text3.setText(m);}
}});
conPane.add(text3);
this.setVisible(true);
}
}
public class Counter {
public static void main(String[] args)
{
new counter1();
}
}
实验心得:了解到了计算器的制作代码和制作过程
实验十一:
package jisuanqi2;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Jisuanqi extends Frame implements ActionListener{
public Jisuanqi() {
super("计算器");
Frame ff=new Frame("framework test");
ff.setSize(400,100);
ff.setLocation(300,240);
ff.setLayout(new FlowLayout());
final TextField f1=new TextField("10",8);
ff.add(f1);
//this.add(new Label("+"));
Label l1=new Label("+");
ff.add(l1);
//this.add(new TextField("20",8));
TextField f2=new TextField("20",8);
ff.add(f2);
//this.add(new Button("="));
Button b1=new Button("=");
ff.add(b1);
//this.add(new TextField(10));
TextField f3=new TextField(10);
ff.add(f3);
ff.addWindowListener(new myclose());
ff.setVisible(true);
b1.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent e)
{double c;
String s1=f1.getText();
double a=Integer.parseInt(s1);
String s2=f2.getText();
double b=Integer.parseInt(s2);
c=a+b;
String m=String.valueOf(c);
f3.setText(m);
}
private double Integer(String s) {
return 0;
}
});
}
class myclose implements WindowListener{
public void windowActivated(WindowEvent arg0) {
}
public void windowClosed(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}
public void windowDeactivated(WindowEvent arg0) {
}
public void windowDeiconified(WindowEvent arg0) {
}
public void windowIconified(WindowEvent arg0) {
}
public void windowOpened(WindowEvent arg0) {
}
}
public static void main(String[] args) {
new Jisuanqi();
}
public void actionPerformed(ActionEvent arg0) {
}
}
实验心得:
此次实验在上一次实验的基础上添加新的功能,用以实现为控件加监测实现驱动。在编写过程中遇到很多问题,不过在同学的帮助下解决了。对图形界面知识点的掌握还是不够熟练,以后还需要加强训练。
实验十二:
package a;
import java.awt.FlowLayout;
import javax.swing.*;
import java.awt.Container;
public class A{
public A()
{
A jf1=new JFrame ("简历");
jf1.setLayout(new FlowLayout());
jf1.getContentPane().add(new JButton("姓名")) ;
jf1.getContentPane().add(new JTextField("LHN",43)) ;
jf1.getContentPane().add(new JButton("性别")) ;
jf1.getContentPane().add(new JTextField("男",43)) ;
jf1.getContentPane().add(new JButton("年龄")) ;
jf1.getContentPane().add(new JTextField("20",43)) ;
jf1.getContentPane().add(new JButton("民族")) ;
jf1.getContentPane().add(new JTextField("汉",43)) ;
jf1.getContentPane().add(new JButton("籍贯")) ;
jf1.getContentPane().add(new JTextField("辽宁辽阳",43)) ;
jf1.getContentPane().add(new JButton("学院")) ;
jf1.getContentPane().add(new JTextField("计算机学院",43)) ;
jf1.getContentPane().add(new JButton("专业")) ;
jf1.getContentPane().add(new JTextField("网络工程",43)) ;
JPanel p1=new JPanel();
jf1.getContentPane().add(p1);
jf1.setSize(420,310);
Container conPane = getContentPane();
jf1.add(conPane);
jf1.setVisible(true);
}
public static void main (String[] args) {
new A();
}
}
实验心得:我觉得这个实验非常有趣,让人对java学习产生了浓厚的兴趣,作为本学期最后一次实验课,使我真正体会到了java课程的魅力和对它的热爱,我又一次爱上了java程序设计这门课程。
原文:https://www.cnblogs.com/faust-coffee/p/11042630.html