java - How to loop this if statement? -
basically looking question asks. abook
class looks this:
import java.io.*; import java.util.*; public class abook { public static void main (string args[]) { linkedlist addressbook = new linkedlist(); scanner input = new scanner(system.in); system.out.println("would add friend? (say y or n)"); string reply = input.nextline(); if(reply.equals("y")) { system.out.println("what name of friend?"); string name = input.nextline(); system.out.println("what age of friend?"); int age = input.nextint(); friend newfriend = new friend(name,age); addressbook.add("name: " + newfriend.name + "; " + "age: " + newfriend.age); system.out.println("this address book far: " + addressbook); } else if(reply.equals("n")){ system.out.println("thank time"); } } }
and in case if need friend
class use here, it:
public class friend { public string name; public int age; public friend(string n, int a) { name = n; age = a; } }
i unsure whether or not use "do" or "while" loop or how use it. awesome if explained why or how loop work on other.
thank you.
edit: did not realize how active community before posted question, , did not think many answers did in such short period of time. so, figured out own way loop before saw replies using do-while loop looked this.
import java.io.*; import java.util.*; public class abook { public static void main(string args[]) { linkedlist addressbook = new linkedlist(); scanner input = new scanner(system.in); int n = 0; { system.out.println("would add friend? (say y or n)"); string reply = input.nextline(); if (reply.equals("y")) { system.out.println("what name of friend?"); string name = input.nextline(); system.out.println("what age of friend?"); int age = input.nextint(); friend newfriend = new friend(name, age); addressbook.add("name: " + newfriend.name + "; " + "age: " + newfriend.age); system.out.println("this address book far: " + addressbook); n++; } else if (reply.equals("n")) { system.out.println("thank time"); system.out.println("would know in address book? (say y or n)"); string userinput = input.nextline(); if (reply.equals("y")) { system.out.println("this address book far: " + addressbook); system.out.println("goodbye!"); } else if (reply.equals("n")) { system.out.println("okay! goodbye!"); } n = 101; } } while (n < 100); } }
it works well, , got fit if - else statement in this. trying sort linkedlist integer/age of "friend", can in order youngest oldest when print linkedlist. if point me in right direction, that'd awesome!
anyways, thank came me, , hope can others come across looking answers.
use do-while
loop when want task @ least once no matter whether condition true or false. structure of do-while
loop is-
do{ //do }while(condition);
use while
loop otherwise - if condition true loop executed. structure of while
loop is-
while(condition){ //do }
Comments
Post a Comment