java loop break with if statement? -
While trying to do some practice to learn Java, I used to calculate work hours and pay a decent Program written.
package horulycalc; Import java.util.Arrays; Import java.util.Scanner; Public class HorulyCalc {public static zero main (string [] args) {scanner param = new scanner (System.in); String [] titles = {"developer", "designer", "data entry", "manager", "CEO"}; System.out.println ("Hello and welcome to job counters"); System.out.println ("Please enter the title of your job"); String title = param.nextLine (); While (Arrays.asList (title) .included (title)) {System.out.println ("Please enter a valid work title"); Title = param.nextLine (); If (Arrays.asList (title) is included (title)) {System.out.println ("Please enter your hours for this week:"); String count = param.nextLine (); System.out.printf ("Your salary is: $% f", hour math (hourly (title), integer. From (calculation))); break ; }}} Public Stable Ent Hourly (String JobTitle) {Int rate = 0; Switch (JobTitle) {case "developer": rate = 10; break; Case "Designer": Rate = 8; break ; Case "data entry": rate = 6; break ; Case "Manager": Rate = 15; break; Case "CEO": Rate = 36; break ; } The rate of return ; } Public static float hour mathematics (int rate, ant count) {float total; Total = rate * count; Total return; }}
If I add a wrong job title for the first time, then the program runs fine, I mean the input that is not included within the job title array.
When I first enter a valid job title, for example, how it is finished as "CEO" program brakes and netbeans
< Div class = "post-text" itemprop = "text">
Since your loop already tests for a valid title, you should not even check it inside the loop.
It's easy:
(Arrays .slist (title) .included (title)) {System.out.println ("Please enter valid work title "); Title = param.nextLine (); } System.out.println ("Please enter your time for this week:"); String count = param.nextLine (); System.out.printf ("Your salary is: $% f", hour math (hourly (title), integer. From (calculation)));
You do not have to break this from the loop in this way. Once you are out of the loop, you know that you have a valid title.
Comments
Post a Comment