java - Why is my message printing twice when I break out of the inner if? -
i having little problem code. compiling , running works well, however, when attempt break out of inner loop,
system.out.println("type category want add to."); system.out.println("homework, classwork, labs, test, quizzes, midterm, final");
the code above printing twice terminal when want print once.
i have feeling simple mistake way brackets aligned having difficulty figuring out how it. appreciated!
import java.util.scanner; public class getgrade { public static void main(string[] args) { scanner input = new scanner(system.in); final int max = 15; int[] homework = new int[max]; int[] classwork = new int[max]; int[] lab = new int[max]; int[] test = new int[max]; int[] quizzes = new int[max]; int[] midterm = new int[max]; int[] fin = new int[max]; int hwcount, clcount, labcount, testcount, quizcount, midcount, fincount; double hwtotal, cltotal, labtotal, testtotal, quiztotal, midtotal, fintotal; double grade = 0; string selection = ""; system.out.println(); system.out.println("welcome getgrade!"); system.out.println(); while (true) { system.out.println("type category want add to."); system.out.println("homework, classwork, labs, test, quizzes, midterm, final"); selection = input.nextline(); if (selection.equals("homework")) { system.out.print("what percentange of grade homework? > "); double hwpercent = input.nextdouble(); system.out.println("now begin typing grades. when finished, type -1."); (int = 0; < homework.length; i++) { homework[i] = input.nextint(); hwtotal = homework[i] * hwpercent; grade += hwtotal; if (homework[i] == -1) break; } } } } }
it's trivial seems:
call input.nextint(); in inner loop not include newline. breaking of innerloop, receiving next line contains newline - character in input.nextline(); remaining input of "-1\n" line , proceed main loop again not match "homework".
Comments
Post a Comment