标题:java 中对象数组的问题
只看楼主
qwe885167759
Rank: 4
等 级:业余侠客
威 望:5
帖 子:148
专家分:259
注 册:2013-3-12
结帖率:72.22%
已结贴  问题点数:10 回复次数:6 
java 中对象数组的问题
package StudentDemo;

import java.util.*;

public class StudentDemo
{
    static Graduate G[]=null;
    static Student S[]=null;
    static Undergraduate Ug[]=null;

    public static void main(String args[])
    {
        
        String name,sex;
        int sno,mathGreade,EnglishGreade,chinaGreade;
        Scanner x =new Scanner(System.in);
        System.out.println("请输入人数");
        int k=x.nextInt();
        Undergraduate Ug[] = new Undergraduate[k];
        Graduate G[] = new Graduate[k];
        Student S[] = new Student[k];
        for(int i=0;i<k;i++)
        {
            Ug[i] = new Undergraduate();
            G[i]= new Graduate();
            S[i]= new Student();
            S[i].set1(name=x.nextLine());
             S[i].set2(sex=x.nextLine());
            S[i].set3(sno=x.nextInt());
            S[i].set4(chinaGreade=x.nextInt());
             S[i].set5(mathGreade=x.nextInt());
             S[i].set6(EnglishGreade=x.nextInt());
            
               
        }
        for(int i=0;i<k;i++)
        {
            G[i].judge1(S[i].get4(),S[i].get5(),S[i].get6());
            Ug[i].judge(S[i].get4(),S[i].get5(),S[i].get6());
        }
    }
}
class Student
{
    private String name;
    private String sex;
    private int sno;
    private int chinaGreade;
    private int mathGreade;
    private int EnglishGreade;
    public void set1 (String name)
    {
        this.name = name;
    }
    public void set2(String sex)
    {
        this.sex = sex;
    }
    public void set3(int sno)
    {
        this.sno = sno;
    }
    public void set4(int chinaGreade)
    {
        this.chinaGreade = chinaGreade;
    }
    public void set5(int mathGreade)
    {
        this.mathGreade = mathGreade;
    }
    public void set6(int EnglishGreade)
    {
        this.EnglishGreade = EnglishGreade;
    }
    public String get1()
    {
        return name;
    }
    public String get2()
    {
        return sex;
    }
    public int get3()
    {
        return sno;
    }
    public int get4()
    {
        return chinaGreade;
    }
    public int get5()
    {
        return mathGreade;
    }
    public int get6()
    {
        return EnglishGreade;
    }
   
}

class Undergraduate extends Student
{   
    public void judge(int a,int b,int c)
    {
    if((a+b+c)/3>60)
        System.out.println("jege");

     }
}
class Graduate extends Student
{
    public void judge1(int a,int b,int c)
    {
    if((a+b+c)/3>60)
        System.out.println("Graduge_jege");

     }
}


出现的异常:
请输入人数

1
g
b
Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at StudentDemo.StudentDemo.main(StudentDemo.java:32)


求指点
搜索更多相关主题的帖子: package public import null java 
2014-04-30 12:55
日知己所无
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:38
帖 子:427
专家分:2071
注 册:2014-3-22
得分:4 
程序代码:
package studentdemo; // 按照Java习惯,包名一般用纯小写字母

import java.util.*;

public class StudentDemo {

    public static void main(final String[] args) {
        try { // 因为涉及到IO操作,最好考虑发生异常的情况
            Scanner scanner = new Scanner(System.in);
            System.out.print("请输入人数:"); // 为了使提示更易理解,不用println
            int totalNumber = Integer.valueOf(scanner.nextLine()); // 关键修改

            if (totalNumber > 0) { // 防御编程
                Student[] StudentArray
                        = new Student[totalNumber];
                Graduate[] GraduateArray
                        = new Graduate[totalNumber];
                Undergraduate[] UndergraduateArray
                        = new Undergraduate[totalNumber];

                for (int index = 0; index < totalNumber; index++) {
                    StudentArray[index] = new Student();
                    GraduateArray[index] = new Graduate();
                    UndergraduateArray[index] = new Undergraduate();

                    System.out.print("第" + (index + 1) + "位同学姓名:");
                    String name = scanner.nextLine();

                    System.out.print("第" + (index + 1) + "位同学性别:");
                    String sex = scanner.nextLine();

                    System.out.print("第" + (index + 1) + "位同学学号:");
                    int studentNo = Integer.valueOf(scanner.nextLine());

                    System.out.print("第" + (index + 1) + "位同学语文成绩:");
                    int chineseScore = Integer.valueOf(scanner.nextLine());

                    System.out.print("第" + (index + 1) + "位同学数学成绩:");
                    int mathScore = Integer.valueOf(scanner.nextLine());

                    System.out.print("第" + (index + 1) + "位同学外语成绩:");
                    int englishScore = Integer.valueOf(scanner.nextLine());

                    StudentArray[index].setName(name);
                    StudentArray[index].setSex(sex);
                    StudentArray[index].setStudentNo(studentNo);
                    StudentArray[index].setChineseScore(chineseScore);
                    StudentArray[index].setMathScore(mathScore);
                    StudentArray[index].setEnglishScore(englishScore);
                }

                for (int index = 0; index < totalNumber; index++) {
                    GraduateArray[index].judge(
                            StudentArray[index].getChineseScore(),
                            StudentArray[index].getMathScore(),
                            StudentArray[index].getEnglishScore());
                    UndergraduateArray[index].judge(
                            StudentArray[index].getChineseScore(),
                            StudentArray[index].getMathScore(),
                            StudentArray[index].getEnglishScore());
                }
            }
        } catch (Exception e) { // 实际编程时,可以考虑仅处理有可能发生的异常
            System.out.println(e);
        } finally {
        }
    }
}

class Student {

    public final String getName() { // getter和setter全是用IDE自动生成的
        return m_name;
    }

    public final void setName(String name) {
        this.m_name = name; // 类成员(member)的变量最好有别于方法的参数名
    }

    public final String getSex() {
        return m_sex;
    }

    public final void setSex(String sex) {
        this.m_sex = sex;
    }

    public final int getStudentNo() {
        return m_studentNo;
    }

    public final void setStudentNo(int studentNo) {
        this.m_studentNo = studentNo;
    }

    public final int getChineseScore() {
        return m_chineseScore;
    }

    public final void setChineseScore(int chineseScore) {
        this.m_chineseScore = chineseScore;
    }

    public final int getMathScore() {
        return m_mathScore;
    }

    public final void setMathScore(int mathScore) {
        this.m_mathScore = mathScore;
    }

    public final int getEnglishScore() {
        return m_englishScore;
    }

    public final void setEnglishScore(int englishScore) {
        this.m_englishScore = englishScore;
    }

    private String m_name; // 根据习惯,一般把方法名放在前面,内部数据放在后面
    private String m_sex;
    private int m_studentNo;
    private int m_chineseScore;
    private int m_mathScore;
    private int m_englishScore;
}

class Graduate extends Student {

    public void judge(int chineseScore, int mathScore, int englishScore) {
        if ((chineseScore + mathScore + englishScore) / 3.0 >= 60.0) {//实数运算
            System.out.println("Graduge_jege");
        }
    }
}

class Undergraduate extends Student {

    public void judge(int chineseScore, int mathScore, int englishScore) {
        if ((chineseScore + mathScore + englishScore) / 3.0 >= 60.0) {
            System.out.println("jege");
        }
    }
}


修改好了,注释部分是重点内容,望参考

输入输出例:
程序代码:
请输入人数:2
第1位同学姓名:张三
第1位同学性别:男
第1位同学学号:1
第1位同学语文成绩:55
第1位同学数学成绩:35
第1位同学外语成绩:40
第2位同学姓名:李四
第2位同学性别:女
第2位同学学号:2
第2位同学语文成绩:95
第2位同学数学成绩:100
第2位同学外语成绩:90
Graduge_jege
jege
2014-04-30 15:05
日知己所无
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:38
帖 子:427
专家分:2071
注 册:2014-3-22
得分:0 
至于原来的程序为神马会出现问题,可以参考下面的帖子的1楼和4楼:
http://bbs.
2014-04-30 15:12
日知己所无
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:38
帖 子:427
专家分:2071
注 册:2014-3-22
得分:4 
InputTestOK_r0.java
程序代码:
// JAVA 2核心技术 卷I:基础知识
// Example 3-2. InputTest.java
// 先输入姓名,后输入年龄,结果是正确的示例

import java.util.*;

public class InputTestOK_r0 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        System.out.print("What is your name? ");
        String name = in.nextLine();

        System.out.print("How old are you? ");
        int age = in.nextInt();

        System.out.println("Hello, " + name + ". Next year, you'll be " + (age + 1));
    }
}


输入输出:
What is your name? xiaoxin
How old are you? 5
Hello, xiaoxin. Next year, you'll be 6

InputTestNG.java
程序代码:
import java.util.*;
// 先输入年龄,后输入姓名,结果是错误的示例

public class InputTestNG {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        System.out.print("How old are you? ");
        int age = in.nextInt();

        System.out.print("What is your name? ");
        String name = in.nextLine();

        System.out.println("Hello, " + name + ". Next year, you'll be " + (age + 1));
    }
}


输入输出:
How old are you? 5
What is your name? Hello, . Next year, you'll be 6

没有机会输入姓名,程序就执行完了

InputTestOK_r1.java
程序代码:
// 先输入年龄,后输入姓名,修改后,结果是正确的示例,但不太完美

import java.util.*;

public class InputTestOK_r1 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        System.out.print("How old are you? ");
        int age = in.nextInt();
        in.nextLine(); // 关键修改:额外追加一个读入回车符的操作

        System.out.print("What is your name? ");
        String name = in.nextLine();

        System.out.println("Hello, " + name + ". Next year, you'll be " + (age + 1));
    }
}


输入输出:
How old are you? 5
What is your name? xiaoxin
Hello, xiaoxin. Next year, you'll be 6

InputTestOK_r2.java
程序代码:
// 先输入年龄,后输入姓名,修改后,结果是正确的示例,应该是比较完美的解决办法

import java.util.*;

public class InputTestOK_r2 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        System.out.print("How old are you? ");
        int age = Integer.valueOf(in.nextLine()); // 关键修改:统一使用nextLine

        System.out.print("What is your name? ");
        String name = in.nextLine();

        System.out.println("Hello, " + name + ". Next year, you'll be " + (age + 1));
    }
}


输入输出:
How old are you? 5
What is your name? xiaoxin
Hello, xiaoxin. Next year, you'll be 6

至于为什么非要加try...catch,在上面任意一个例子中,让输入年龄时,输入姓名就知道了
2014-04-30 15:26
szgg520
Rank: 5Rank: 5
等 级:职业侠客
威 望:3
帖 子:79
专家分:307
注 册:2011-6-13
得分:2 
异常处理是必须的....

[url=http://www.]深圳复印机出租[/url]
2014-05-01 16:18
qwe885167759
Rank: 4
等 级:业余侠客
威 望:5
帖 子:148
专家分:259
注 册:2013-3-12
得分:0 
回复 2 楼 日知己所无
谢谢,很有帮助

我本楚狂人,凤歌笑孔丘
2014-05-03 09:27
qwe885167759
Rank: 4
等 级:业余侠客
威 望:5
帖 子:148
专家分:259
注 册:2013-3-12
得分:0 
回复 4 楼 日知己所无
懂了,非常感谢。

我本楚狂人,凤歌笑孔丘
2014-05-03 09:39



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-430853-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029597 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved