java - Labeled loop,continue keyord does not work -
import java.util.scanner; class details { int num; string names[][]; double marks[][]; double total[]; string grades[]; void getdata() { scanner ob = new scanner(system. in ); system.out.print("enter number of students : "); num = ob.nextint(); names = new string[num][2]; marks = new double[num][4]; (int x = 0; x system.out.print("first name : "); names[x][0] = ob.next(); system.out.print("last name : "); names[x][1] = ob.next(); loop1: (int p = 1; p <= 1; p++) { system.out.print("\tfirst test marks : "); marks[x][0] = ob.nextdouble(); if ((marks[x][0] < 0) || (marks[x][0] > 15)) { system.out.println("marks should within 0 15"); continue loop1; } } loop2: (int p = 1; p <= 1; p++) { system.out.print("\tmid term marks : "); marks[x][1] = ob.nextdouble(); if (marks[x][1] < 0 || marks[x][1] > 20) { system.out.println("marks should within 0 20"); continue loop2; } } loop3: (int p = 1; p <= 1; p++) { system.out.print("\tlab test marks : "); marks[x][2] = ob.nextdouble(); if (marks[x][2] < 0 || marks[x][2] > 15) { system.out.println("marks should within 0 15"); continue loop3; } } loop4: (int p = 1; p <= 1; p++) { system.out.print("\tfinal marks : "); marks[x][3] = ob.nextdouble(); if (marks[x][3] < 0 || marks[x][3] > 50) { system.out.println("marks should within 0 20"); continue loop4; } } system.out.println(); } } public void total() { total = new double[num]; (int x = 0; x total[x] = marks[x][0] + marks[x][1] + marks[x][2] + marks[x][3]; } } public void grades() { grades = new string[num]; (int x = 0; x if (total[x] >= 80) { grades[x] = "a"; } else if (total[x] >= 70) { grades[x] = "b"; } else if (total[x] >= 60) { grades[x] = "c"; } else if (total[x] >= 50) { grades[x] = "d"; } else { grades[x] = "f"; } } } void print() { system.out.println("\t\t\tresult sheet\n\n\tfirst name\tlast name\tgrade"); (int x = 0; x system.out.println("\t" + names[x][0] + "\t\t" + names[x][1] + "\t\t" + grades[x]); } } } class test { public static void main(string[] args) { details data = new details(); data.getdata(); data.total(); data.grades(); data.print(); } }
the problem continue keyword, doesn't work weather give wrong input
i don't think you've understood meaning of continue
keyword. continue
s you've used won't @ current position. take instance segment:
loop1: (int p = 1; p <= 1; p++) { system.out.print("\tfirst test marks : "); marks[x][0] = ob.nextdouble(); if ((marks[x][0] < 0) || (marks[x][0] > 15)) { system.out.println("marks should within 0 15"); continue loop1; } }
when if-condition succeeds, prints , continues next iteration of loop1. would've continued next iteration anyways, since continue
keyword @ end of looped segment. however, since for-loops run once, there is no next iteration , loop stopped.
perhaps better solution use while-loop this:
while(true) { system.out.print("\tfirst test marks : "); marks[x][0] = ob.nextdouble(); if ((marks[x][0] < 0) || (marks[x][0] > 15)) { system.out.println("marks should within 0 15"); } else { break; } }
Comments
Post a Comment