JAVA:模拟提交JAVA作业

发现几个问题:

  • 带参方法并非任何时候都好用
  • 使用this引用会导致错误
  • 利用了局部变量的一些好处(例如:index,index1)

运行效果图:

模拟提交JAVA作业运行截图
image-1350

参考代码1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package cn.puruidong.accp_15;
/**
 * @author PuRuidong
 *
 */
import java.util.Scanner;
public class Test5 {
   
    public String name ; //名字
    private String pre ; //密码
    private String newname ; //验证名字
    private String newpre ; //验证密码
    private String mail ; //邮箱
    private String file ; //java文件名
    private boolean loginzhi ;//登录验证
    private int index ; //indexOf方法定义
    private int index1 ; //lastIndexOf方法定义
    private Scanner sc = new Scanner (System.in);
   
   
    public String inname (){
        System.out.println("********注册系统*********");
        boolean namezhi = false ;//定义循环
        do {
            System.out.println("请输入用户名(长度不小于5位):");
            name = sc.next(); //录入用户名
            if (name.length()<5){
                System.out.println("用户名长度必须大于5位.");
                System.out.println("请重新录入!");
                namezhi = true ;
                continue ;
            } else{
                namezhi = false;
            }
        } while (namezhi!=false);
        return name ; //返回用户名
    }
   
    public String inpre (){
        boolean prezhi = false ;
        do {
            System.out.println("请输入密码不小于6位,并且必须包含小写字母:java):");
            pre = sc.next(); //录入密码
            index= pre.indexOf("java");
            if (pre.length()<6||index==-1){ //密码长度小于6,或者没有在密码中找到java字样则重复录入
                System.out.println("密码长度必须大于6位!或者密码中不包含java!");
                System.out.println("请重新录入!");
                System.out.println();
                prezhi = true ;
            } else {
                prezhi = false ;
            }
        } while (prezhi!=false);
        return pre ; //返回密码
    }
   
    public String inmail (){
        this.mail=mail;
        boolean mailzhi = false ;
        do {
            System.out.println("请输入邮箱:");
            mail = sc.next(); //录入邮箱
            index = mail.indexOf("@");
            index1 = mail.lastIndexOf(".");
            if (index1==-1||index==0||index==-1||index>index1||(index+1==index1)){ //判断.是否在@的后面
                System.out.println("对不起,邮箱格式输入错误!");
                System.out.println("请重新录入!");
                System.out.println();
                mailzhi = true ;
            } else {
                mailzhi = false ;
            }
           
        } while (mailzhi!=false);
        return mail ; //返回邮箱
    }
   
    public boolean login (boolean loginzhi){
        System.out.println("********登录系统*********");
        loginzhi = false ;
        for (int i=4;i>0;i--){
            System.out.println("请输入用户名(用户名与密码均忽略大小写):");
            newname = sc.next();//验证用户名
            System.out.println("请输入密码:");
            newpre = sc.next(); //验证密码
            if (newname.equalsIgnoreCase(name)&&newpre.equalsIgnoreCase(pre)){
                //验证用户名与原用户名是否相等,或者邮箱与原邮箱是否相等,验证密码
                System.out.println("恭喜,登录成功!");
                System.out.println("");
                loginzhi = true ; //登录成功标记
                break ;
            } else {
                System.out.println("抱歉,输入错误!");
                System.out.println("您还有"+(i-1)+"次输入机会!");
                loginzhi = false ;//登录失败标记
            }
        }
        if (loginzhi==false){  //一旦登录失败将退出程序,不再执行下方的部分
            System.out.println("登录失败,程序退出,谢谢使用!");
            System.exit(0); //退出程序
        }
        return loginzhi ;
    }
   
    public String infile (){
            for (int i=4;i>0;i--){
                System.out.println("请输入java文件名:");
                file = sc.next(); //录入文件名
                index = file.indexOf("."); //检查.的位置
                index1 = file.lastIndexOf("java"); //检查java所在的位置
                if (index==-1||index==0||index1==-1||index1<index){
                    System.out.println("抱歉文件名录入错误,请检查!");
                    continue ;
                } else {
                    System.out.println("恭喜,提交成功!");
                    break;
                }
            }
        return file ;
    }
   
    public void show (){
            System.out.println("您录入的信息是:");
            System.out.println("*******************************");
            System.out.println("用户名:"+name);
            System.out.println("密码:"+pre);
            System.out.println("邮箱"+mail);
            System.out.println("文件名:"+file);
            System.out.println();
            System.out.println("程序退出,谢谢使用!");
       
    }

    /**
     * @param args
     */
}

参考代码2(测试类):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package cn.puruidong.accp_15;

/**
 * @author PuRuidong
 *
 */
public class Ttest5 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Test5 center = new Test5();
        boolean loginzhi = true ;
        center.inname();
        center.inpre();
        center.inmail();
        center.login(loginzhi);
        center.infile();
        center.show();
    }

}

Java:比较随机数的大小

原题:* 随机产生10个1~1000之间的整数,放在一个数组中,求出其中的最大值和最小值,要求不能使用sort()方法
* 书本305页

参考代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package cn.puruidong.accp_14;
/**
 * @author PuRuidong
 *
 */
/*
 * 随机产生10个1~1000之间的整数,放在一个数组中,求出其中的最大值和最小值,要求不能使用sort()方法
 * 书本305页
 */
public class Num {
    private static int sum [] = new int [10];//定义数组
    private int max ;//定义最大值
    private int min ;//定义最小值
   
    public void setSum(int[] sum){//获取数组,并输出结果
        this.sum=sum; //引用当前变量
        for (int i=0;i<sum.length;i++){ //产生随机数,并将随机数存入数组
            int random1 = (int)(Math.random()*1000) ;//产生随机数
            sum[i]=random1;//将所产生的随机数存入数组
        }
        max = sum[0];//假设最大值是数组中下标为0的数字
        min = sum[9];//假设最小值是数组中下标为9的数字
        for (int j=0;j<sum.length;j++){ //判断最大值与最小值
            if (sum[j]>max){ //判断最大值
                max=sum[j];
            }else if (sum[j]<min){ //判断最小值
                min = sum[j];
            }
        }
        for (int c=0;c<sum.length;c++){//看下数组,别让数字乱了...
            System.out.print(sum[c]+" ");
        }
        System.out.println();
        System.out.println("***********************");
        System.out.println("最大值是:"+max);
        System.out.println("最小值是:"+min);
    }
   
    public int[] getSum (){
        return sum ;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Num center = new Num ();
        center.setSum(sum);
        center.getSum();

    }

}

Java:向整形数组插入元素

原题:编写程序。向整形数组的指定位置插入元素,并输出插入前后数组的值。

参考:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package cn.puruidong.accp_14;

/**
 * @author PuRuidong
 *
 */
/*
 * 编写程序。向整形数组的指定位置插入元素,并输出插入前后数组的值。
 * 书本:306页
 */
import java.util.Scanner;
public class Fivenum {

    /**
     * @param args
     */
    private int num [] = new int[] {10,20,30,55,99};//原数组
    private int no [] = new int [num.length+1];//新数组
    private static Scanner sc = new Scanner (System.in);
    private static int index ;//录入想插入的位置
    private static int value ;//录入想插入的值
   
    public void show (int index ,int value){
        ////////////输出插入前的数组
        System.out.println("插入前的数组:");
        for (int i=0;i<num.length;i++){//输出插入前的数组
            System.out.print(num[i]+" ");
        }
        /////赋值数组
        for (int c=0;c<num.length;c++){
            no[c] = num[c];
        }
        System.out.println();
            System.out.println("请输入想插入的位置(0~4):");
            index = sc.nextInt(); //录入想插入的位置
            System.out.println("请输入想插入的数值:");
            value = sc.nextInt(); //录入想插入的值
       
        for (int j=no.length-1;j>index;j--){ ////元素后移
            no[j] = no[j-1];
        }
        no[index] = value; //将 value的值放在index位置
        System.out.println("插入后的数组:");
        /////输出插入后的数组
        for (int k=0;k<no.length;k++){
            System.out.print(no[k]+" ");
        }
    }
   
    public static void main(String[] args) {
        // TODO Auto-generated method stub
    Fivenum center = new Fivenum ();
    center.show(index, value);

    }

}

Java:模拟账户查询,实现存取款

要求:
模拟银行账户业务,添加带参方法存款和取款,存款初始为0,取款时需要判断余额是否充足。

参考代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package cn.puruidong.accp_13;
import java.util.Scanner;
/*
 * 1.选择菜单
 * 2.定义3个菜单,0,1,2
 * 3.选择0时用System.exit(0);终止JVM实例
 * 4.选择1或2时录入数据,并存入cunkuan或qukuan方法中
 * 5.定义cunkuan,qukuan方法,存入刚才的数据,并执行输出
 * 6.书本第285页
 */
public class Account {
    private static double sum = 0.0;//定义余额
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int operation = 0; //定义菜单
        double money = 0.0;//定义输入的金额
        while(true){
            System.out.println("1:存款\t2.取款\t0.退出");
            System.out.print("请选择你需要办理的业务:");
            operation = sc.nextInt();
            switch(operation){
            case 0:
                System.out.println("谢谢使用!");
                System.exit(0);
                /*
                 * public static void exit(int status)终止当前正在运行的 Java 虚拟机。
                 * 参数用作状态码;
                 * 根据惯例,非 0 的状态码表示异常终止。
                 * 该方法调用 Runtime 类中的 exit 方法。该方法永远不会正常返回。
                 * 调用 System.exit(n) 实际上等效于调用:
                 * Runtime.getRuntime().exit(n)
                 */
                break;
            case 1:
                System.out.print("请输入存款金额:");
                money = sc.nextDouble();
                cunKuan(money);//将数字money赋值给cunkuan????
                break;
            case 2:
                System.out.print("请输入取款金额:");
                money = sc.nextDouble();
                quKuan(money);//将数字money赋值给qukuan????
                break;
            }
        }
    }
   
    //////////////写在类中,禁止写在主方法里!!!!!!

    private static void cunKuan(double money) { //输出结果
        /*
         * 静态的方法不能调用非静态的方法,所以在void前面要加上static。
         */
        sum += money;
        System.out.println("存款成功!");
        System.out.println("***余额为:" + sum + " ***");
        System.out.println();
    }

    private static void quKuan(double money) { //输出结果
       
        if(money>sum){
            System.out.println("抱歉,余额不足!");

        }else{
            sum -=money;
            System.out.println("取款成功!");
            System.out.println("***余额为:" + sum + " ***");
        }
        System.out.println();
    }
}

Java:带参方法查找客户

根据需要查找客户姓名,给出查找结果。

  1. 录入客户姓名;
  2. 输出刚才录入到数组中的姓名;
  3. 录入需要查找的姓名;
  4. 跟数组中的姓名一一比对;
  5. 输出结果。

参考代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package cn.puruidong.accp_13;

/**
 * @author PuRuidong
 *
 */
/*
 * 查找客户姓名
 * 书本:272页
 */
import java.util.Scanner ;
public class User {
    private String choose ;//定义选择是否继续查找
    private String seachname ;//定义在数组中查找名字
    Scanner sc = new Scanner (System.in);//定义录入
    String name1 [] = new String [5];//定义数组存储客户的姓名
   
    public void show (){//作用:循环录入姓名
        for (int i=0;i<name1.length;i++){ //循环录入
            System.out.println("请输入客户的姓名:");
            name1[i]=sc.next();
            System.out.println("继续输入吗(y/n):");
            choose = sc.next();
            if (!"y".equals(choose)){ //当输入的不等于y时,退出循环
                break;
            }
        }
       
    }
   
    public boolean newshow (String name){ //作用:循环输出原数组,并且在数组中查找名字
        boolean fild = false ; //定义返回值
        for (int i=0;i<name1.length;i++){ //输出原数组
            System.out.print(name1[i]+"\t");
        }
        System.out.println();
        System.out.println();
        System.out.println("请输入您要查找的姓名:");
        seachname = sc.next(); //录入需要查找的新名字
        System.out.println("*******查找结果*******");
        for (int i=0;i<name1.length;i++){ //输出查找结果
            if (seachname.equals(name1[i])){
                System.out.println("找到了!");
                System.exit(0);//当找到的时候,使用此方法结束循环.也可以使用break(需要添加一些代码,否则会多输出)来结束.
            }
        }
        System.out.println("不好意思,找不到这个人耶!");
        return fild ;
    }
   
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        User center = new User ();//创建对象
        String name=""; //定义名字的String
        center.show();
        System.out.println("****************************");
        System.out.println("客户姓名列表:");
        System.out.println("****************************");
        center.newshow(name);
    }
}