Jump to content
Sign in to follow this  
pendee ramone

Camne Nak Ulang...

Recommended Posts

salam, saya bru blajar java,masih beginner tahap 'ayam' lah...
nk mitk tlg ni pada brader2 n akak2 yg pro...

ni coding yg saya buat:



[b]import java.util.Scanner;[/b]

[b]public class jantina { [/b]

[b]public static void main (String[] args) {[/b]
[b]Scanner gender = new Scanner(System.in);[/b]
[b]int type;[/b]


[b]System.out.print ("\n CHOOSE YOUR GENDER");[/b]
[b]System.out.print ("\n 1 - MALE ");[/b]
[b]System.out.print ("\n 2 - FEMALE ");[/b]
[b]System.out.print ("\n Choose number 1 or 2 for the type of gender: ");[/b]
[b]type = gender.nextInt();[/b]
[b]if (type == 1)[/b]
[b]System.out.print ("\n Type of gender : MALE ");[/b]
[b]else if (type ==2)[/b]
[b]System.out.print ("\n Type of gender : FEMALE ");[/b]
[b]else[/b]
[b]System.out.print ("\n invalid input");[/b]

[b]}[/b]
[b]}[/b]

masalahnya, bila user inputkan "invalid input " tu, camne saya nk back balik,
nk bg user masukkan semula input yg sah???
mksudnya program kembali pada:
[b]System.out.print ("\n Choose number 1 or 2 for the type of gender: ");[/b]
cmne ek??
mtk tlg ye..
kalo ada link yg best n senang paham blaja java blh bg skali.. :-)

tq

Share this post


Link to post
Share on other sites
aritu aku tak try buat pung.. main cakap jer... guna loop laa... aku try dah nih... ko cuba tengok camni ke ko nak..?

[CODE]
Scanner gender = new Scanner(System.in);
int type=0;

while(true){
System.out.print ("\n CHOOSE YOUR GENDER");
System.out.print ("\n 1 - MALE ");
System.out.print ("\n 2 - FEMALE ");
System.out.print ("\n Choose number 1 or 2 for the type of gender: ");
type = gender.nextInt();
if (type == 1)
System.out.print ("\n Type of gender : MALE \n");
else if (type ==2)
System.out.print ("\n Type of gender : FEMALE \n");
else
System.out.print ("\n invalid input");
}
[/CODE]

kalau soalan salah baru kuar soalan.. cuba gini..
[CODE]
Scanner gender = new Scanner(System.in);
int type=0;
boolean result= true;

while(result){
System.out.print ("\n CHOOSE YOUR GENDER");
System.out.print ("\n 1 - MALE ");
System.out.print ("\n 2 - FEMALE ");
System.out.print ("\n Choose number 1 or 2 for the type of gender: ");
type = gender.nextInt();
if (type == 1){
System.out.print ("\n Type of gender : MALE \n");
result= false;
}
else if (type ==2){
System.out.print ("\n Type of gender : FEMALE \n");
result= false;
}
else{
System.out.print ("\n invalid input");
}

}
[/CODE]

Share this post


Link to post
Share on other sites
lama benor dah nih... boleh close dah nih.. aku rasa mesti dia copy masuk assignment dah coding kat atas tuh... hahaha

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this  

×
×
  • Create New...