标题:调试说是输入空字符串,请问怎样修改,感谢!!!
只看楼主
air5116
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2014-11-1
结帖率:40%
 问题点数:0 回复次数:2 
调试说是输入空字符串,请问怎样修改,感谢!!!
import java.util.Scanner;

public class Demo {
    Stu stu[] = new Stu[3];

    public void inputjavaGrade() {

        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < stu.length; i++) {
            System.out.println("请输入第" + (i + 1) + "学生的学号:");
            stu[i].setNumber(sc.nextLine());
            System.out.println("请输入第" + (i + 1) + "学生的姓名:");
            stu[i].setName(sc.nextLine());
            System.out.println("请输入第" + (i + 1) + "学生的java成绩:");
            stu[i].setJavaGrade(sc.nextFloat());
        }

    }

    public void vaGrade() {
        float vaGrade = 0;
        float allGrade = 0;
        for (int i = 0; i < stu.length; i++) {
            allGrade += stu[i].getJavaGrade();
            vaGrade = allGrade / stu.length;
            System.out.println("学生的Java总成绩为" + allGrade);
            System.out.println("第" + (i + 1) + "学生的平均成绩为" + vaGrade);
        }
    }

    public void maxGrade() {
        float max = 0;
        int i = 0;
        while (i < stu.length) {
            if (stu[i].getJavaGrade() > max) {
                max = stu[i].getJavaGrade();
            }
            i++;
        }
        System.out.println("最高成绩的同学学号为" + stu[i].getNumber() + "成绩是" + max);
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Demo demo = new Demo();
        demo.inputjavaGrade();
        demo.vaGrade();
        demo.maxGrade();
    }

}

class Stu {
    private String number;
    private String name;
    private float javaGrade;

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public float getJavaGrade() {
        return javaGrade;
    }

    public void setJavaGrade(float javaGrade) {
        this.javaGrade = javaGrade;
    }

}
搜索更多相关主题的帖子: public import 字符串 姓名 
2015-06-14 14:08
秦时的明月夜
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:13
帖 子:126
专家分:504
注 册:2013-3-12
得分:0 
那是没有初始化呀……你的stu[i]是个对象呀……要实例化啊so you should do this :stu[i]=new Stu();
我觉得读取用sc.next()吧,因为nextLine()是读取一行包括换行符,next()就是字符串。你这里要的就是字符串。
程序代码:
import java.util.Scanner;

public class Demo {
    Stu stu[] = new Stu[3];

    
    public void inputjavaGrade() {

        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < stu.length; i++) {
            stu[i]=new Stu();
            System.out.println("请输入第" + (i + 1) + "学生的学号:");
            stu[i].setNumber(sc.next());
            System.out.println("请输入第" + (i + 1) + "学生的姓名:");
            stu[i].setName(sc.next());
            System.out.println("请输入第" + (i + 1) + "学生的java成绩:");
            stu[i].setJavaGrade(sc.nextFloat());
        }

    }

    public void vaGrade() {
        float vaGrade = 0;
        float allGrade = 0;
        for (int i = 0; i < stu.length; i++) {
            allGrade += stu[i].getJavaGrade();
            vaGrade = allGrade / stu.length;
            System.out.println("学生的Java总成绩为" + allGrade);
            System.out.println("第" + (i + 1) + "学生的平均成绩为" + vaGrade);
        }
    }

    public void maxGrade() {
        float max = 0;
        int i = 0;
        while (i < stu.length) {
            if (stu[i].getJavaGrade() > max) {
                max = stu[i].getJavaGrade();
            }
            i++;
        }
        System.out.println("最高成绩的同学学号为" + stu[i].getNumber() + "成绩是" + max);
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Demo demo = new Demo();
        demo.inputjavaGrade();
        demo.vaGrade();
        demo.maxGrade();
    }

}

class Stu {
    private String number;
    private String name;
    private float javaGrade;

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public float getJavaGrade() {
        return javaGrade;
    }

    public void setJavaGrade(float javaGrade) {
        this.javaGrade = javaGrade;
    }

}


[ 本帖最后由 秦时的明月夜 于 2015-6-14 16:04 编辑 ]
2015-06-14 16:02
air5116
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2014-11-1
得分:0 
真的非常感谢,之前代码真的很多错误
2015-06-14 17:30



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




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

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