【问题标题】:why i am not getting stable output?为什么我没有得到稳定的输出?
【发布时间】:2015-07-13 02:32:04
【问题描述】:

我正在尝试自学 java,下面的代码是我的尝试。我没有按预期得到输出。我已经在下面发布了我的编码和输出。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
abstract class q2Student
{
String name;
int roll_no;
int sub1,sub2,sub3,sub4,sub5;
int total;
float grade;
float per;
public abstract void getdata() throws IOException;
//public abstract void show();
public void show()
{
    System.out.println ("Roll No. = "+roll_no);
    System.out.println ("Name = "+name);
    System.out.println ("Marks of 1st Subject = "+sub1);
    System.out.println ("Marks of 2nd Subject = "+sub2);
    System.out.println ("Marks of 3rd Subject = "+sub3);
    System.out.println ("Marks of 4th Subject = "+sub4);
    System.out.println ("Marks of 5th Subject = "+sub5);
    System.out.println ("Total Marks = "+total);
    System.out.println ("Percentage = "+per+"%");
    System.out.println("Grade="+grade);
}


}
class StudDetails extends q2Student{
public void getdata() throws IOException 
{
    BufferedReader br = new BufferedReader(new  InputStreamReader(System.in));
    System.out.println ("Enter Name of Student");
    name = br.readLine();
    System.out.println ("Enter Roll No. of Student");
    roll_no = Integer.parseInt(br.readLine());
    System.out.println ("Enter marks out of 100 of 1st subject");
    sub1 = Integer.parseInt(br.readLine());
    while(sub1>100){
        System.out.println ("you have entered invalid mark, please check  again and enter a mark out of 100");
        sub1 = Integer.parseInt(br.readLine());
    }
    System.out.println ("Enter marks out of 100 of 2nd subject");
    sub2 = Integer.parseInt(br.readLine());
    while(sub2>100){
        System.out.println ("you have entered invalid mark, please check again and enter a mark out of 100");
    sub2 = Integer.parseInt(br.readLine());}
    System.out.println ("Enter marks out of 100 of 2nd subject");
    sub3 = Integer.parseInt(br.readLine());
    while(sub3>100){
        System.out.println ("you have entered invalid mark, please check again and enter a mark out of 100");
    sub3 = Integer.parseInt(br.readLine());}
    System.out.println ("Enter marks out of 100 of 2nd subject");
    sub4 = Integer.parseInt(br.readLine());
    while(sub4>100){
        System.out.println ("you have entered invalid mark, please check again and enter a mark out of 100");
    sub4 = Integer.parseInt(br.readLine());}
    System.out.println ("Enter marks out of 100 of 2nd subject");
    sub5 = Integer.parseInt(br.readLine());
    while(sub5>100){
        System.out.println ("you have entered invalid mark, please check again and enter a mark out of 100");
    sub5 = Integer.parseInt(br.readLine());
    }

}
public void calculate(){
    total=sub1+sub2+sub3+sub4+sub5;
    per=(total*100)/500;
    grade=per/10;
    }

    public void result(){

    if(grade>=9){
        System.out.println("Execellent!!passed with distinction");
    }
    else if((grade>=7.5) &&( grade<=8.9)){
        System.out.println("first class!!passed with distinction");
        }
    else if((grade>=6)&&(grade<=7.4)){
        System.out.println("passed!! first class");
    }
    else{
        System.out.println("sorry!! you are failed");
    }
    }
}
public class Student extends StudDetails{
static int n;
public static void main(String args[]) throws IOException
{
    Student s=new Student();

    System.out.println ("enter the total no of students");
    Scanner in=new Scanner(System.in);
    n=in.nextInt();
    System.out.println ("the total no of students"+n);
    Student id[]=new Student[n];// array object
    for(int i=0;i<id.length;i++){
        s.getdata();
        s.show();
        s.calculate();
        s.result();
    }

        //in.close();
}
}

我的输出如下我给对象数组输入为 2 并且计算的输出与我预期的不一样

enter the total no of students
2
the total no of students2
Enter Name of Student
ram
Enter Roll No. of Student
1
Enter marks out of 100 of 1st subject
89
Enter marks out of 100 of 2nd subject
98
Enter marks out of 100 of 2nd subject
78
Enter marks out of 100 of 2nd subject
76
Enter marks out of 100 of 2nd subject
87
Roll No. = 1
Name = ram
Marks of 1st Subject = 89
Marks of 2nd Subject = 98
Marks of 3rd Subject = 78
Marks of 4th Subject = 76    
Marks of 5th Subject = 87
Total Marks = 0           //total im getting 0
Percentage = 0.0%
Grade=0.0           // grade 0 but result states passed with first class
passed!! first class
Enter Name of Student
sur
Enter Roll No. of Student
2
Enter marks out of 100 of 1st subject
65
Enter marks out of 100 of 2nd subject
67
Enter marks out of 100 of 2nd subject
89
Enter marks out of 100 of 2nd subject
86
Enter marks out of 100 of 2nd subject
87
Roll No. = 2
Name = sur
Marks of 1st Subject = 65
Marks of 2nd Subject = 67
Marks of 3rd Subject = 89
Marks of 4th Subject = 86
Marks of 5th Subject = 87
Total Marks = 350        // same method called in second index calculating properly
Percentage = 70.0%
Grade=7.0
passed!! first class

【问题讨论】:

  • 代码太多了。更准确地确定问题并返回
  • 定义“不符合预期”。
  • 检查您正在调用的函数的顺序 s.getdata();----s.show();-----&gt;s.calculate();.total 未在 getData() 中初始化,并且您在 show() 中打印 total,为您提供 int 0 的默认值。在第二次尝试中它起作用了,因为 total 已为第一次尝试初始化 n calculate()
  • 我在 for 循环中调用计算方法。在第一次迭代中,我得到 total=0,grade=0 但结果通过了,在第二次迭代中,总计算为 total=350,grade=7.0 结果为第一类。我想知道你在 main 方法的 for 循环的第一次迭代中得到 0
  • 我也尝试调用像 id[i].getdata--id[i].result 这样的方法,在这种情况下,我在 main 方法中得到空指针异常。因为我创建了单独的对象来调用方法。我的订单是正确的。 show mwthod jus 显示输入的数据

标签: java arrays oop object for-loop


【解决方案1】:
for(int i=0;i<id.length;i++){
    s.getdata();
    s.calculate();
    s.show();
    s.result();
}

您需要在展示之前进行计算。不过我不确定,我没有仔细阅读您的所有代码。您应该看到How do I ask a good question?How to create a Minimal, Complete, and Verifiable example. 的帮助中心

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-05
    • 1970-01-01
    • 1970-01-01
    • 2012-01-08
    相关资源
    最近更新 更多