practice programs

4
Page 1 of 4 Practice Programs public class Test { public static void main(String args[]){ int x = 30; if( x == 10 ){ System.out.print("Value of X is 10"); }else if( x == 20 ){ System.out.print("Value of X is 20"); }else if( x == 30 ){ System.out.print("Value of X is 30"); }else{ System.out.print("This is else statement"); } } } import java.util.Scanner; class GetInputFromUser { public static void main(String args[]) { int a; float b; String s; Scanner in = new Scanner(System.in); System.out.println("Enter a string"); s = in.nextLine(); System.out.println("You entered string "+s); System.out.println("Enter an integer"); a = in.nextInt(); System.out.println("You entered integer "+a); System.out.println("Enter a float"); b = in.nextFloat(); System.out.println("You entered float "+b); } }

Upload: preston-university

Post on 15-Jul-2015

30 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Practice programs

Page 1 of 4

Practice Programs

public class Test {

public static void main(String args[]){

int x = 30;

if( x == 10 ){

System.out.print("Value of X is 10");

}else if( x == 20 ){

System.out.print("Value of X is 20");

}else if( x == 30 ){

System.out.print("Value of X is 30");

}else{

System.out.print("This is else statement");

}

}

}

import java.util.Scanner;

class GetInputFromUser

{

public static void main(String args[])

{

int a;

float b;

String s;

Scanner in = new Scanner(System.in);

System.out.println("Enter a string");

s = in.nextLine();

System.out.println("You entered string "+s);

System.out.println("Enter an integer");

a = in.nextInt();

System.out.println("You entered integer "+a);

System.out.println("Enter a float");

b = in.nextFloat();

System.out.println("You entered float "+b);

}

}

Page 2: Practice programs

Page 2 of 4

Switch statement

What is an Array?

So far, you have been working with variables that hold only one value. The integer variables you have set up have held only one number, and the string variables just one long string of text. An array is a way to hold more than one value at a time. It's like a list of items. Think of an array as the columns in a spreadsheet. You can have a spreadsheet with only one column, or lots of columns. The data held in a single-list array might look like this:

Page 3: Practice programs

Page 3 of 4

Arrays and Loops

Page 4: Practice programs

Page 4 of 4