mercredi 22 novembre 2017

java if(s.nextLine().equals("")) result in being true even with input [duplicate]

This question already has an answer here:

a colleague at school has given me an average-grade-calculator that is written in Java, since it was his first program he wanted me to look at it and improve it. I was able to do that, but when I wanted to make it dynamic I failed with the "if" statement. (it's written in German with commented translation)

Here's my code:

package main;
import java.util.Scanner;
public class Program {
public static int anzahl;
public static void main(String[] args) {
    @SuppressWarnings("resource")
    Scanner s=new Scanner (System.in);
    int num[]=new int[50];
    int average=0;
    int i=0;
    int sum=0;
    System.out.println("Nichts eingeben zum beenden!"); //Type nothing to exit
    for (i=0;i<num.length;i++) 
    {
        System.out.print("Note: "); //Grade
        num[i]=s.nextInt();
        if(s.nextLine().equals(""))
         { 
            System.out.println("Terminated");
            break;
         }
         sum+=num[i];

    }
    average=sum/num.length;
    System.out.println("Durchschnitt="+average); //Average
    }
}

Aucun commentaire:

Enregistrer un commentaire