public class suma { public static void main (String [] args

Anuncio
Primer Ejemplo
import java.io.*;
public class suma
{
public static void main (String [] args)
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader (isr);
try
{
System.out.print("Sumando 1 : ");
int s1 = Integer.parseInt(br.readLine());
System.out.print("Sumando 2 : ");
int s2 = Integer.parseInt(br.readLine());
int suma=s1+s2;
System.out.println ("La suma es " + s1 + "+" + s2 +"="+ suma);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
import java.util.*;
public class PruebaScanner {
public static void main(String[] args) {
Scanner sTextor=new Scanner(System.in);
do {
sTexto = reader.next();
System.out.println(sTexto);
} while(!sTexto.equals("FIN"));
}
}
import java.util.Scanner;
public class TimeConverter {
//hours to minutes
public static void hoursToMinutes() {
int hrs, mins;
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of hours: ");
hrs = input.nextInt();
input.close();
mins = hrs * 60;
String output = "There are " + mins + " minutes in " + hrs + " hours.";
System.out.println(output);
}
//days to hours
public static void daysToHours() {
int hrs, days;
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of days: ");
days = input.nextInt();
input.close();
hrs = days * 24;
String output = "There are " + hrs + " hours in " + days + " days.";
System.out.println(output);
}
//minutes to hours
public static void minutesToHours() {
int mins, hrs;
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of minutes: ");
mins = input.nextInt();
input.close();
hrs = mins / 60;
String output = "There are " + hrs + " hours in " + mins + " minutes.";
System.out.println(output);
}
//hours to days conversion
public static void hoursToDays() {
int hrs, days;
Scanner input = new Scanner (System.in);
System.out.print("Enter the number of hours: ");
hrs = input.nextInt();
input.close();
days = hrs / 24;
String output = "There are " + hrs + " hours in " + days + " days.";
System.out.println(output);
}
//set up choice menu
public static void main(String[] args ){
int choice;
final int FLAG = -1;
Scanner input = new Scanner(System.in);
/* Prompt user for selection of conversion*/
System.out.println("1. Hours to minutes.");
System.out.println("2. Days to hours.");
System.out.println("3. Minutes to hours.");
System.out.println("4. Hours to days.");
System.out.println("To quit enter (-1).");
System.out.println("Enter your choice:");
choice = input.nextInt();
while (choice != FLAG) {
if (choice==1) {
hoursToMinutes();
}else if (choice==2){
daysToHours();
}else if (choice==3){
minutesToHours();
}else if(choice==4) {
hoursToDays();
}else {
System.out.println("Selection is not valid.");
}
System.out.print("Enter a correct selection (-1 to quit): ");
choice = input.nextInt();
}
}//End Main
} // end TimeConverter
Descargar