java - How to get my program to start over when the user hits "y" on -
so i've used system.out.print("enter more test scores? (y/n): ");
yet when run , scores summarizes user isn't given chance again here code. guys think may have put in wrong place.
public class testscoreapp { public static void main(string[] args) { // display operational messages system.out.println("please enter number of test scores entered"); system.out.println("to end program enter 999."); system.out.println(); // print blank line int scoretotal = 0; int scorecount = 0; int testscore = 0; int min = 100; int max = 0; int counter = 0; int setnumber = 0; string useranswer = "n"; scanner sc = new scanner(system.in); // series of test scores user outerloop: { // user enters number of test scores entered system.out.print("enter number of test scores entered: "); setnumber = sc.nextint(); if (setnumber > 0 && setnumber != 999) { while (setnumber > 0) { // user enters test scores system.out.print("enter score: "); testscore = sc.nextint(); // accumulate score count , score total if (testscore <= 100) { scorecount += 1; scoretotal += testscore; setnumber --; } //added exercise 2-2, #4 modified if statement else if (testscore > 100 || testscore < 0) { system.out.println("invalid entry, score not counted"); } else if (testscore == 999) { system.out.println("average test score complete"); } if (testscore > max && testscore <= 100) { max = testscore; } if (testscore < min && testscore >= 0) { min = testscore; } if (setnumber == counter) { break outerloop; } //end of test scores while loop } useranswer = sc.next(); } }// end of loop while(useranswer.compareto("y") == 0 ); system.out.print("enter more test scores? (y/n): "); // display score count, score total, , average score // added casting int ot double exercise 3-2 #5 double averagescore = (double) scoretotal / (double) scorecount; // added number formatting ( 1 decimal place) numberformat number = numberformat.getnumberinstance(); number.setmaximumfractiondigits(1); string message = "\n" + "score count: " + scorecount + "\n" + "score total: " + scoretotal + "\n" + "average score: " + averagescore + "\n" //added exercise 3-2 #4 add min/max + "max score: " + max + "\n" + "min score: " + min + "\n"; system.out.println(message); } }
there several modifications done in program.
when asking user enter choice inputting more, should accept his/her choice in useranswer variable before closing off do-while loop user choice can checked after each iteration!
there no need break outer-loop without checking user's input!
scorecount & scoretotal need initialised 0 again in beginning of do-while loop.
the corrected program along imports needed :-
import java.text.numberformat; import java.util.scanner; public class javaapplication7 { public static void main(string[] args) { system.out.println("please enter number of test scores entered"); system.out.println("to end program enter 999."); system.out.println(); // print blank line int scorecount = 0,scoretotal = 0; int testscore = 0; int min = 100; int max = 0; int counter = 0; int setnumber = 0; string useranswer = "n"; scanner sc = new scanner(system.in); // series of test scores user { // user enters number of test scores entered system.out.print("enter number of test scores entered: "); setnumber = sc.nextint(); if (setnumber > 0 && setnumber != 999) { scorecount=0; scoretotal=0; while (setnumber > 0) { // user enters test scores system.out.print("enter score: "); testscore = sc.nextint(); if (testscore <= 100) { scorecount += 1; scoretotal += testscore; setnumber --; } //added exercise 2-2, #4 modified if statement else if (testscore > 100 || testscore < 0) { system.out.println("invalid entry, score not counted"); } else if (testscore == 999) { system.out.println("average test score complete"); } if (testscore > max && testscore <= 100) { max = testscore; } if (testscore < min && testscore >= 0) { min = testscore; } } // display score count, score total, , average score // added casting int ot double exercise 3-2 #5 double averagescore = (double) scoretotal / (double) scorecount; // added number formatting ( 1 decimal place) numberformat number = numberformat.getnumberinstance(); number.setmaximumfractiondigits(1); string message = "\n" + "score count: " + scorecount + "\n" + "score total: " + scoretotal + "\n" + "average score: " + averagescore + "\n" //added exercise 3-2 #4 add min/max + "max score: " + max + "\n" + "min score: " + min + "\n"; system.out.println(message); } system.out.print("enter more test scores? (y/n): "); useranswer=sc.next(); // single error----only corrected piece of code. }while(useranswer.compareto("y") == 0 ); // end of loop }
}
Comments
Post a Comment