java - Looping a menu to display after a choice -
so have code:
import java.util.scanner; public class studentgradedriver { public static void main(string[] args) { scanner scanner = new scanner(system.in); system.out.println("enter student name: "); string name = scanner.nextline(); studentgrade grade = new studentgrade(name); system.out.println("welcome student grade interface!"); system.out.println("you modyfing grades john doe"); system.out.println("==============================================="); system.out.println("(1) add homework score"); system.out.println("(2) add passed lab"); system.out.println("(3) add midterm exam score"); system.out.println("(4) set final exam score"); system.out.println("(5) calculate final grade"); system.out.println("(-1) exit"); system.out.println("==============================================="); system.out.print("please input choice: "); int choice = scanner.nextint(); while(true) { if (choice == 1); { system.out.println("enter total points earned: "); double _hwearned = scanner.nextdouble(); system.out.println("enter total points possible: "); double _hwpossible = scanner.nextdouble(); grade.addhomework(_hwearned, _hwpossible); system.out.println("welcome student grade interface!"); system.out.println("you modyfing grades john doe"); system.out.println("==============================================="); system.out.println("(1) add homework score"); system.out.println("(2) add passed lab"); system.out.println("(3) add midterm exam score"); system.out.println("(4) set final exam score"); system.out.println("(5) calculate final grade"); system.out.println("(-1) exit"); system.out.println("==============================================="); system.out.print("please input choice: "); choice = scanner.nextint(); } if (choice == 2) { system.out.println("a passed lab has been added " + "this student. "); grade.passedlab(); } else if (choice == 3) { system.out.println("enter total points earned midterm: "); double _midearned = scanner.nextdouble(); system.out.println("enter total points possible midterm: "); double _midpossible = scanner.nextdouble(); grade.addexam(_midearned, _midpossible); system.out.println("welcome student grade interface!"); system.out.println("you modyfing grades john doe"); system.out.println("==============================================="); system.out.println("(1) add homework score"); system.out.println("(2) add passed lab"); system.out.println("(3) add midterm exam score"); system.out.println("(4) set final exam score"); system.out.println("(5) calculate final grade"); system.out.println("(-1) exit"); system.out.println("==============================================="); system.out.print("please input choice: "); choice = scanner.nextint(); } else if (choice == 4) { system.out.println("enter total points earned final: "); double _finalearned = scanner.nextdouble(); grade.setfinalexam(_finalearned); system.out.println("welcome student grade interface!"); system.out.println("you modyfing grades john doe"); system.out.println("==============================================="); system.out.println("(1) add homework score"); system.out.println("(2) add passed lab"); system.out.println("(3) add midterm exam score"); system.out.println("(4) set final exam score"); system.out.println("(5) calculate final grade"); system.out.println("(-1) exit"); system.out.println("==============================================="); system.out.print("please input choice: "); choice = scanner.nextint(); } else if (choice == 5) { system.out.println("the score " + name + " " + grade.getfinalscore()); } else if(choice == -1) { break; } } } }
is there easier or way loop menu display after choices 1, 3, 4, , 5, not 2 , still exit when user input -1?
me copying , pasting menu, appears if (choice ==1) still executes after have pressed different number. after execution , menu pops again, if hit same number execute. example when first choice 3, execute if (choice == 1), 2nd choice 2 , execute if (choice == 2), , 3rd choice 3 again, go if (choice == 1), know why happens? willl appreciated.
not quite sure you're trying do, see have while loop , first choice if statement separate 2nd choice if statement. did mean else if(choice == 2) or misunderstanding you're trying do?
Comments
Post a Comment