Click here to Skip to main content
15,071,430 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Tomorrow is my project and l have to make a conditional statement. But how to connect scanner class with conditional statement. For example-

import java. util. Scanner;
Scanner sc = new Scanner(System. in);
System. out. println ("Enter your age : ");
int age = sc. nextInt();

System. out. println ( 2 * age);

Now here the output comes Enter your age and when l type my age means any number it multiplies it by 2. So in the same way l want that with conditional statements and logical operators - if, else, else if. I want that it ask my grade and when l enter my grade it says like this---

What I have tried:

--char grade = 'A';

if ( grade == 'A') {
System. out. println ("your grade is A");
} else if ( grade == 'B' ) {
System. out. println ("very good, keep learning ");
} else if ( grade == 'D' ) {
System. out. println ("keep improving ");
} else {
System. out. println (" invalid grade");
}

So this a program so l want that when l run it then first it will ask enter your grade as l told previously and when l will enter it it will tell the result. So this was just an example really l wanted to make that it will ask two things name and age in the consol then after entering that it will act accordingly. Please help me. I'll be remembering your help as tomorrow is my project submission. Please tell me the syntax of that. Love codeproject
Posted
Updated 18-Jul-21 4:39am

1 solution

I do not understand how name and age are related to grade, however:
Java
Scanner sc = new Scanner(System. in);
System.out.print("Enter your name : ");
String name = sc. next();
System.out.print("Enter your age : ");
int age = sc. nextInt();
System.out.printf("Hi %s, you are %d years old, it's time you learned Java%n", name, age);
   
Comments
[no name] 18-Jul-21 21:43pm
   
Excellent sir, thank you so much l am successfully able to do that. Sir you asked how name and age are related to grade so sir l was just giving an example that when l Will run my code it will ask two things suppose name and age and it will act accordingly. Thanks once again.
[no name] 18-Jul-21 22:59pm
   
Sir you mean to say that l will type import java. util. Scanner;
Scanner sc = new Scanner(System. in);
System. out. println ("Enter your age : ");
int age = sc. nextInt();
Richard MacCutchan 19-Jul-21 3:44am
   
Yes.
[no name] 18-Jul-21 23:00pm
   
And also l will type
String name = sc. next();
System.out.print("Enter your age : "); and then l will do this --
char grade = 'A';

if ( grade == 'A') {
System. out. println ("your grade is A");
} else if ( grade == 'B' ) {
System. out. println ("very good, keep learning ");
} else if ( grade == 'D' ) {
System. out. println ("keep improving ");
} else {
System. out. println (" invalid grade");
}
[no name] 19-Jul-21 0:54am
   
What do you mean by this sir?? - System.out.printf("Hi %s, you are %d years old, it's time you learned Java%n", name, age);
Richard MacCutchan 19-Jul-21 3:44am
   
Run the code and you will see.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900