java - My while loop prints menu twice -
this question has answer here:
i wrote program called soccer team roaster. stores player's jerseys number , player rating. , ever user wants within menu option. reason menu option keeps on printing twice. i've been informed because when ask int input playerrating[i]
takes enter button , input when first ask user's input string "menuoption". can help?
here code
import java.util.scanner; public class soccerteamroaster { public static void main(string[] args) { scanner sc = new scanner(system.in); final int jersy_nums = 5; int[] jersynumber = new int[jersy_nums]; int[] playerrating = new int[jersy_nums]; string[] jput = new string[jersy_nums]; int = 0; boolean quit = false; (i = 0; < jersy_nums; i++) { system.out.println("enter player " + (i + 1) + "'s jersey number: "); jersynumber[i] = sc.nextint(); system.out.println("enter player " + (i + 1) + "'s rating: "); playerrating[i] = sc.nextint(); system.out.println(""); } system.out.println(""); system.out.println("roster"); int n = 0; (n = 0; n < jersy_nums; n++) { system.out.println("player " + (n + 1) + " -- jersey number: " + jersynumber[n] + ", rating: " + playerrating[n]); } while (!quit) { string stg = "menu\n" + "u - update player rating\n" + "a - output players above rating\n" + "r - replace player\n" + "o - output roster\n" + "q - quit\n"; system.out.println(""); system.out.println(stg); system.out.println("choose option: "); string menuoption = "?"; menuoption = sc.nextline(); boolean correctinput = false; if (menuoption.equals("u") || menuoption.equals("a") || menuoption.equals("r") || menuoption.equals("o") || menuoption.equals("q")) { correctinput = true; menuoption = menuoption.trim(); } else { correctinput = false; } if (menuoption.equals("u")) { system.out.println("enter jersey number: "); int jerseynum = sc.nextint(); system.out.println("new rating player: "); int newrate = sc.nextint(); int m = 0; (m = 0; m < jersy_nums; m++) { if (jersynumber[m] == jerseynum) { playerrating[m] = newrate; } } } else if (menuoption.equals("a")) { system.out.println("enter rating: "); int rating = sc.nextint(); int k = 0; (k = 0; k < jersy_nums; k++) { if (playerrating[k] > rating) { system.out.println("player " + (k + 1) + " -- jersey number: " + jersynumber[k] + ", rating: " + playerrating[k]); } } } else if (menuoption.equals("o")) { system.out.println("roster"); int j = 0; (j = 0; j < jersy_nums; j++) { system.out.println("player " + (j + 1) + " -- jersey number: " + jersynumber[j] + ", rating: " + playerrating[j]); } } else if (menuoption.equals("q")) { quit = true; } else if (menuoption.equals("r")) { system.out.println("enter jersey number: "); int jernum = sc.nextint(); int l = 0; (l = 0; l < jersy_nums; l++) { if (jersynumber[l] == jernum) { system.out.println("enter new jersey number: "); jersynumber[l] = sc.nextint(); system.out.println("enter new player rating: "); playerrating[l] = sc.nextint(); } } int = 0; (a = 0; < jersy_nums; a++) { system.out.println("player " + (a + 1) + " -- jersey number: " + jersynumber[a] + ", rating: " + playerrating[a]); } } } return; } }
the reason why prints 2 times because of code...
menuoption = sc.nextline();
just change
menuoption = sc.next();
now, prints 1 time
Comments
Post a Comment