Friday 15 May 2015

java - How do I make this if statement? -



java - How do I make this if statement? -

apologies silly question, struggling larn java. need code work repeat unless '0' entered studentnumber, i'm unsure of how "please come in pupil number" part work when have declare int before if statement? i'm not sure if i've approached wrong or what, need able repeat info entry unless "0" entered studentnumber. help!

class main { public static void main( string args[] ) { int studentnumber = bio.getint(); if(studentnumber > 0) { system.out.print("#please come in pupil number : "); system.out.print("#please come in coursework mark : "); int coursework = bio.getint(); system.out.print("#please come in exam mark : "); int exammark = bio.getint(); double average = (double)(coursework + exammark) / 2; system.out.printf("sn = " + studentnumber + " ex = " + exammark + " cw = " + coursework + " mark = " + average); } else { system.out.print("#end of data"); } } } }

use while() instead of if, along next changes:

system.out.print("#please come in pupil number : "); int studentnumber = bio.getint(); while(studentnumber > 0) { system.out.print("#please come in coursework mark : "); int coursework = bio.getint(); system.out.print("#please come in exam mark : "); int exammark = bio.getint(); double average = (double)(coursework + exammark) / 2; system.out.printf("sn = " + studentnumber + " ex = " + exammark + " cw = " + coursework + " mark = " + average); system.out.print("#please come in pupil number : "); studentnumber = bio.getint(); } system.out.print("#end of data");

this, opposed other answers, ensure in first iteration, perform check (and promt user pupil number).

java if-statement int

No comments:

Post a Comment