+ 1
Java -How to Return in Something like Choices or a Block of code.
So Theres A Choices - A & B If the user inputs other letters,Characters,Numbers that Choices Dont Have its gonna something like return in Choices A & B and can Enter a Input Again.
21 Respuestas
+ 1
Use swith statment
+ 1
Hi, Booztay!
In Java, every return statement is a primitive data type, an Object or null.
If you need to return more than one value or Object, you should use Collections API (Set or a List of Objects) or Arrays (sequence of variables that can be of Objects or primitive data types).
0
Description is not clear to me..
If you explain your doubt with a sample code, that will easy to understand..
But if you are taking about from function return, then you can return only one value. If you want ro return more than 1 value, then you have to use a data structure, a array, store those values in those and return that. Unpack in calinng function.. Only this is a possible way...
If you are taking about main function implementation, then give a try and share your code if it incomplete with specific doubt.. Hope this helps you..
0
Jayakrishna theres a Variable called a & b
And im gonna input like letter C, and my Code gonna Give me an Error but i can Still Input A and B to Continue
0
Oh. So input you are taking is char type?
0
Jayakrishna CharAt() Method Thingy?
Idontknow how to use that method
Can u Post an Code or explain how to use it in my case
0
Is it kind of a guessing game?
You ask the user to input a number and if it is not the answer then you ask them to enter again.
0
Try this...
do{
System.out.println("Input options A or B");
try {
char ch1=in.next();
char ch2=in.next();
}
catch(Exception e)
{
System.out.println ("Invalid inputs");
}
}while(ch1!='A' && ch2! ='B');
Edit:
Before this, you need to read about Exception handling...
0
Avinesh Kind of
0
ThankYou Jayakrishna Im So Gonna Save this Code :>
0
Booztay
If you are giving only proper input, but not correct input, means char type but not A or B, then you can neglect about try catch, only use do while...
&
Wel come..
0
while(true){
int a = sc.nextInt();
if(a>0 && a<10) {
System.out.println("In range");
break;
}
else {
System.out.println("Out of range: Try again");
}
}
// Just an example to check whether the number is in range from 0 to 10. If out of range, the loop runs again asking for a new input. If in range, the loop breaks.
0
Booztay
Are you taking 1 input or 2 ?..
Confused.. You said A and B..? If it is A or B, then use or(||) operator instead of &&. , take only one input....
0
Jayakrishna huh?
0
How many inputs you are taking..?
OK. Change code snippet according to your requiments..
@Avinesh code works fine for single input.. Try that..
0
Jayakrishna im taking 8 inp
0
Booztay
Ok.
Now you have 2 alternative solutions, try those..
0
Booztay I hope this is what you want
0
How it's possible