Java Input & Control St
Java Input & Control St
Topic: JAVA
1
scanf(“%d”,&age);
Java User Input scanf(“%s”,name);
• The Scanner class is used to get user input, and it is found in the java.util package.
• To use the Scanner class,
• Create an object of the class, and
• Use any of the available methods found in the Scanner class documentation.
3
Program - 1 In your window, try getting
import java.util.Scanner; user inputs: integer, Boolean,
float, character.
class MyClass {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
Enter name, age and salary:
System.out.println("Enter name, age and Mary
salary:"); 21
30000
// String input
String name = myObj.nextLine(); Name: Mary
Age: 21
// Numerical input Salary: 30000
int age = myObj.nextInt();
double salary = myObj.nextDouble();
// Output input by user
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
}
}
4
Control Statements
Control
Statements
Syntax: Syntax:
break; continue;
12
Program 3
13
Import java.util.Scanner*;
else if (finalscore>60)
public class students_marks{
public static void main(String[] args) {
While System.out.println(“Good”);
else if (finalscore >40)
int maths, phy, che, eng, compsc; System.out.println(“Average”);
else
double finalscore;
System.out.println(“Poor”);
boolean result=true;
Scanner marks = new Scanner(System.in); System.out.println(“Any more students?
(true or false)”);
while(result)
result = marks.nextBoolean();
{
System.out.println(“Enter Marks for Maths: “); }
}
maths=marks.nextInt();
}
System.out.println(“Enter Marks for Phy: “);
phy=marks.nextInt();
System.out.println(“Enter Marks for Che: “);
che=marks.nextInt();
System.out.println(“Enter Marks for Eng: “);
eng=marks.nextInt();
System.out.println(“Enter Marks for CompSc: “);
compsc=marks.nextInt();
finalscore=(maths+eng+phy+che+compsc)/5;
if (finalscore >90)
System.out.println(“Excellent”);
else if (finalscore>80) 14
import java.util.*;
else if (finalscore>60)
Do-while
public class students_marks{ System.out.println(“Good”);
public static void main(String[] args) { else if (finalscore >40)
int maths, phy, che, eng, compsc; System.out.println(“Average”);
else
double finalscore; System.out.println(“Poor”);
boolean result;
When to exit from loop?
Scanner marks = new Scanner(System.in);
do System.out.println(“Any more students?
(true or false)”);
{ result = marks.nextBoolean();
System.out.println(“Enter Marks for Maths: “);
maths=marks.nextInt(); } while(result);
}
System.out.println(“Enter Marks for Phy: “); }
phy=marks.nextInt();
System.out.println(“Enter Marks for Che: “);
che=marks.nextInt();
System.out.println(“Enter Marks for Eng: “);
eng=marks.nextInt();
System.out.println(“Enter Marks for CompSc: “);
compsc=marks.nextInt();
finalscore=(maths+eng+phy+che+compsc)/5;
if (finalscore >90)
System.out.println(“Excellent”);
else if (finalscore>80) 15
System.out.println(“V.Good”);
Import java.util.*;
public class students_marks{
public static void main(String[] args) {
For
int maths, phy, che, eng, compsc, studentnumber; else if (finalscore>60)
double finalscore; System.out.println(“Good”);
Scanner marks = new Scanner(System.in); else if (finalscore >40)
System.out.println(“Average”);
studentnumber = marks.nextInt();
else
for(int i=0; i<studentnumber; i++) System.out.println(“Poor”);
{
System.out.println(“Enter Marks for Maths: “); }
}
maths=marks.nextInt();
}
System.out.println(“Enter Marks for Phy: “);
phy=marks.nextInt();
System.out.println(“Enter Marks for Che: “);
che=marks.nextInt();
System.out.println(“Enter Marks for Eng: “); When you know total number of
eng=marks.nextInt(); students in class.
System.out.println(“Enter Marks for CompSc: “);
compsc=marks.nextInt();
finalscore=(maths+eng+phy+che+compsc)/5;
if (finalscore >90)
System.out.println(“Excellent”);
else if (finalscore>80)
System.out.println(“V.Good”);
16
THANK YOU….
17