Java,

2016-04-05 6 views
0

dizisinden kullanıcı girişi alamıyor Burada yapmaya çalıştığım şey, temel olarak, sonuç göstergesini gösteren bir Java programıdır.Java,

Bazen programın çıktıyı düzgün gösteremediği garip bir hata alıyorum.

import java.util.*; 
import java.text.DecimalFormat; 

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

    //variable declaration 

    int totalsubjects; 
    int totalcredithour = 0; 

    double totalpointaccumulate=0; 

    String name; 
    String id; 
    String program; 

    //Decimal Formatting 
    DecimalFormat df = new DecimalFormat("#,###,##0.00"); 

    //prompt for student information 
    System.out.println("Please enter student's name: "); 
    name = sc.nextLine(); 
    System.out.println("Please enter student's ID: "); 
    id = sc.nextLine(); 
    System.out.println("Please enter student's program: "); 
    program = sc.nextLine(); 
    System.out.println("Please enter how many subjects: "); 
    totalsubjects = sc.nextInt(); 

    //arrays declaration 
    String[] code = new String[totalsubjects]; 
    int[] credithour = new int[totalsubjects]; 
    double[] courseworkMark = new double[totalsubjects]; 
    double[] finalexamMark = new double[totalsubjects]; 
    double[] gpa = new double[totalsubjects]; 
    double[] totalMarks = new double[totalsubjects]; 
    char[] grade = new char[totalsubjects]; 
    double[] point = new double[totalsubjects]; 
    double[] totalpoint = new double[totalsubjects]; 

    //loop for each subject informations 
    for(int i=0;i <code.length;i++) 
    { 
     System.out.println("Please enter subject code no "+(i+1)+":"); 
     code[i] = sc.next(); 
     System.out.println("Please enter credithour for subject no"+(i+1)+":"); 
     credithour[i]=sc.nextInt(); 
     System.out.println("Please enter the coursework mark for subject no"+(i+1)+":"); 
     courseworkMark[i] = sc.nextDouble(); 
     System.out.println("Please enter the final exam mark for subject no"+(i+1)+":"); 
     finalexamMark[i] = sc.nextDouble(); 
     totalMarks[i]=(courseworkMark[i]*0.6)+(finalexamMark[i]*0.4); 
     totalcredithour = credithour[i] + totalcredithour; 


     //Grade placement for each subject 


     if (totalMarks[i]<= 100 && totalMarks[i]>=80) 
     { 
      grade[i] = 'A'; 
      point[i] = 4.0; 
     } 

     else if (totalMarks[i]>=60 && totalMarks[i] <=79) 
     { 
      grade[i] = 'B'; 
      point[i] = 3.0; 
     } 
     else if (totalMarks[i]>=59 && totalMarks[i] <=49) 
     { 
      grade[i] = 'C'; 
      point[i] = 2.0; 
     } 
     else if (totalMarks[i]>=0 && totalMarks[i] <=40) 
     { 
      grade[i] = 'F'; 
      point[i] = 0.0; 
     } 
     totalpoint[i] = point[i] * credithour[i]; 
     totalpointaccumulate = totalpoint[i] + totalpointaccumulate; 

    } 

    System.out.println("\t -x-"); 
    System.out.println(" Result Slip Semester January 2016"); 
    System.out.println("____________________________________________"); 
    System.out.println("Name : "+name); 
    System.out.println("ID  : "+id); 
    System.out.println("Program: "+program); 
    System.out.println("____________________________________________"); 
    System.out.println("    CrHr Grade Pt TotalPt"); 
    System.out.println("--------------------------------------------"); 


    for(int k=0;k<code.length;k++) 
    { 
     System.out.println((k+1)+(".")+code[k] +("\t")+credithour[k]+("\t")+grade[k]+("\t")+point[k]+"\t"+totalpoint[k]); 

    } 

    System.out.println("--------------------------------------------"); 
    System.out.println(" Total"+"  "+totalcredithour+"      " + totalpointaccumulate); 

    System.out.println("          GPA: "+(df.format(totalpointaccumulate/totalcredithour))); 


} 

}

Bu hata üretir Örneğim girişidir. Burada

Please enter student's name: 
KZK KKY 
Please enter student's ID: 
32423432 
Please enter student's program: 
BSE 
Please enter how many subjects: 
3 
Please enter subject code no 1: 
IGB4040 
Please enter credithour for subject no1: 
3 
Please enter the coursework mark for subject no1: 
70 
Please enter the final exam mark for subject no1: 
90 
Please enter subject code no 2: 
IGB3030 
Please enter credithour for subject no2: 
3 
Please enter the coursework mark for subject no2: 
50 
Please enter the final exam mark for subject no2: 
70 
Please enter subject code no 3: 
IGB2020 
Please enter credithour for subject no3: 
4 
Please enter the coursework mark for subject no3: 
60 
Please enter the final exam mark for subject no3: 
70 

The error output produced from sample input above. Sorum birinci ve üçüncü konu gayet güzel çalışırken sonuç fişinde ikinci konu boş neden? Kodumda herhangi bir mantık hatası var mı?

Şimdiden teşekkürler.

+3

THis satırı bozuk: 'else (totalMarks [i]> = 59 && totalMarks [i] <= 49)' – mjohnsonengr

+0

Bunu nasıl anladım? Konu no2 için girdinin ders notu "50" olduğunu gördüm, böylece sonucun ne olduğunu görmek için kodu takip ettim ve bu çizgiyi gördüm. – mjohnsonengr

+0

Teşekkür ederim !!!!!!!! Şimdi kodum iyi çalışıyor. Ben totalMarks [i]> = 49 && totalMarks [i] <= 59. Tersine düzgün görüntüleyemiyorum. Tekrar teşekkürler. – hanx20

cevap

1

Yorumlarda ele alındığı gibi, kodu inceledikten sonra, else if (totalMarks[i]>=59 && totalMarks[i] <=49) hattını görüyorum - bu asla doğru olmayacaktır; çevreleyen durumlar> = 60 ve < = 40 olduğundan, bu satırın else if (totalMarks[i]>=41 && totalMarks[i] <=59)

olabileceğini varsayalım, bu düzeltmeyi, bazı hata ayıklama becerilerinde size yardımcı olacak bir zaman yatırımıyla takip ederim Bu gibi küçük mantık hatalarını tanımlayın :)