JDAY 0: word swapping, input / output, calculation of hypotenese, Random numbers
package day1;
//import java.util.Scanner;
public class day1 {
public static void main(String[] args) {
String x= "hey";
String y= "what";
String z;
z=x;
x=y;
y=z;
System.out.println("my word is " +x);
System.out.println("my word is " +y );
}
}
Enter your name, age and get the your name and age as output
package day1;
import java.util.Scanner;
public class day1 {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("your name is? ");
String name=sc.nextLine();
System.out.println("What is your age?");
int age = sc.nextInt();
System.out.println("my name is " +name );
System.out.println("my age is " +age );
}
}
using sc.nextLine(); to clear the /n stored in the box
package day1;
import java.util.Scanner;
public class day1 {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("your name is? ");
String name=sc.nextLine();
System.out.println("What is your age?");
int age = sc.nextInt();
sc.nextLine();
System.out.println("What is your food?");
String food=sc.nextLine();
System.out.println("my name is " +name );
System.out.println("my age is " +age );
System.out.println("my food is " +food );
}
}
Calculation of hypotenuse:
package day1;
// import javax.swing.JOptionPane;
import java.util.Scanner;
// program to calculate the hypotenese of rt-triangle
public class day1 {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("What is the perpendicular? " );
double p=sc.nextDouble();
System.out.println("What is the base? " );
double b=sc.nextDouble();
double h= Math.sqrt(p*p + b*b);
System.out.println("The hypo is: " +h);
}
}
Random number:// rolling a dice
package day1;
import java.util.Random;
import java.util.Scanner;
// program to calculate the hypotenese of rt-triangle
public class day1 {
public static void main(String[] args) {
Random r= new Random();
int x= r.nextInt(6)+ 1;
//boolean y= r.nextBoolean();
System.out.println("The random is: " +x);
}
}
Comments
Post a Comment