java - Else If and Do while does not work as intended -


edit: recent happenings of me compiling program know not compile lead me believe simultaneously having issue compiler. no doubt due running in wine on mac opposed native application. thank responses. test out responses , make changes when have fixed said error compiler or have moved computers 1 working one.

i relatively new programming , website please bear me. getting error 2 of if statements , 1 do/while unable resolve.

the entire program works intended 2 blocks below. problem when enter character 'y', works intended , ("results =" + arrays.tostring(row)) prints expected to. proceeds continue through original loop , start program again.

however, when enter other character, (i.e not 'y' or 'n') code not print "input must either 'y' or 'n'" , waits input. when 'n' entered, not follow out of loop wanted, continues loop , not proceed else if had thought would. indefinitely, not accepting other input 'y' continue passed loop can never receive print "negative".

does have thoughts why happening? while not technically homework, tagged such want know, if possible, happening opposed how fix it.

        {             ans = input.next().charat(0);                 if (!ans.equals('y')  ||  !ans.equals('n')) {                     system.out.println ("input must either 'y' or 'n'");                 }         } while (!ans.equals('y')  ||  !ans.equals('n')); 

and

if (ans.equals('y')) {             (object[] row : prevresults) {                     system.out.println("results = " + arrays.tostring(row));             }         } //         else if (ans.equals('n')) {             system.out.println("negative");             //system.exit(0);          } 

full code below

import java.util.*;  public class averages {     public static void main (string [] args) {          //declare variables         int course, exam, average = 0;         char ans;         string pass;          //creating objects         scanner input =  new scanner(system.in);         list<object[]> prevresults = new arraylist<object[]>();          //full loop         (int = 0; < 5; i++ ) {              system.out.println ("loop " + (++i) + " out of 5");              //course loop             {             system.out.println ("please enter course mark out of 100");             course = input.nextint();                 if (course > 100) {                     system.out.println ("number entered on 100");                 }             } while (course > 100);              //exam loop             {             system.out.println ("please enter exam mark out of 100");             exam = input.nextint();                 if (exam > 100) {                     system.out.println ("number entered on 100");                 }             } while (exam > 100);               average = (course + exam)/2;              // final grade             system.out.println ("the average mark " + average);              if ( average >= 50 && course > 40 && exam > 40)  {                 system.out.println ("the final grade pass");                 pass = "pass";             }             else {                 system.out.println ("the final grade fail");                 pass = "fail";             }              //add array             prevresults.add(new object[] { "course mark: " + course, "exam mark: " + exam,"average: " + average, "grade: " + pass});               system.out.println ("would see previous results? y/n");               //'previous results' question loop             {                 ans = input.next().charat(0);                     if (!ans.equals('y')  ||  !ans.equals('n')) {                         system.out.println ("input must either 'y' or 'n'");                     }             } while (!ans.equals('y')  ||  !ans.equals('n'));               // close or array if statement             if (ans.equals('y')) {                 (object[] row : prevresults) {                         system.out.println("results = " + arrays.tostring(row));                 }             } //             else if (ans.equals('n')) {                 system.out.println("negative");                 //system.exit(0);              }         }// end      }//end main }//end class 

edit 2: have switch computers , suggested answers indeed work. being

while (ans != 'y' && ans != 'n'); 

and

while (!(ans.equals('y')  ||  ans.equals('n'))); 

and

making separate method suggested chris browne.

for benefit of else reads this, these solutions work wonderfully although have not had time @ bufferedreader suggested greg hewgill implement because seems better option has stated.

first, error in code:

system.out.println ("loop " + (++i) + " out of 5"); 

you should not increment i incremented in for update statement - getting wrong iteration count result.

next, should not use equals compare char values - use == instead. when use equals, lot of unnecessary things happen due auto-boxing, result in characterobject.equals(anothercharacterobject), objects being of type java.lang.character. use e.g. ans == 'y' instead.

last, folks have pointed out, should rewrite do-while as:

do {     ans = input.next().charat(0);     if (ans != 'y' && ans != 'n') {         system.out.println ("input must either 'y' or 'n'");     } } while (ans != 'y' && ans != 'n'); 

or have separate method condition check (thanks chris browne).


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -