Escolar Documentos
Profissional Documentos
Cultura Documentos
SEMESTRE: 3RO
VARIABLES EJERCICIOS
Ejercicio 1 Escribe un programa en el que se declaren las
variables enteras x e y. Asignales los valores 144 y 999
respectivamente. A continuación, muestra por pantalla el valor de
cada variable, la suma, la resta, la división y la multiplicación.
/*
* To change this license header, choose License
Headers in Project Properties.
* To change this template file, choose Tools |
Templates
* and open the template in the editor.
*/
package javaapplication24;
/**
*
* @author hp
*/
public class JavaApplication24 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int X = 144;
int Y = 999;
int suma = X + Y;
int resta = X - Y;
int mult = X * Y;
int div = X / Y;
int modulo = X % Y;
System.out.print("Suma :");
System.out.println(suma );
System.out.print("Resta :");
System.out.println(resta);
System.out.print("Multiplicacion :");
System.out.println(mult);
System.out.print("Division :");
System.out.println(div);
System.out.print("Modulo :");
System.out.println(modulo);
}
}
Ejercicio 2. Escribe un programa con variables nombre, dirección
y teléfono y asígnale los valores correspondientes. Muestra los
valores de esas variables por pantalla de tal forma que el
resultado del programa sea el mismo.
/*
* To change this license header, choose License
Headers in Project Properties.
* To change this template file, choose Tools |
Templates
* and open the template in the editor.
*/
package javaapplication31;
/**
*
* @author hp
*/
public class JavaApplication31 {
//atributos de del objeto
String nombre;
float edad;
String fechaNt;
String correo;
String direccion;
float celular;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
//int vec[]=new int [5];
System.out.println("NOMBRE
COMPLETO:"+LA.nombre);
System.out.println("EDAD:"+LA.edad);
System.out.println("FECHA DE
NACIMIENTO:"+LA.fechaNt);
System.out.println("CORREO:"+LA.correo);
System.out.println("DIRECCION:"+LA.direccion);
System.out.println(" NUMERO DE
CELULAR:"+LA.celular);
}
Ejercicio 3 Realiza un conversor de euros a pesetas. La cantidad
en euros que se quiere convertir deberá estar almacenada en una
variable.
/*
* To change this license header, choose License
Headers in Project Properties.
* To change this template file, choose Tools |
Templates
* and open the template in the editor.
*/
package javaapplication25;
import java.util.Scanner;
/**
*
* @author hp
*/
public class JavaApplication25 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
/*
* To change this license header, choose License
Headers in Project Properties.
* To change this template file, choose Tools |
Templates
* and open the template in the editor.
*/
package javaapplication26;
import java.util.Scanner;
/**
*
* @author hp
*/
public class JavaApplication26 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
//Se crea objeto scanner
Scanner teclado =new Scanner(System.in);
//Se declaran variables
int num;
int dm, um, c, d, u;
package javaapplication30;
import java.util.Scanner;
public class JavaApplication30
{
}
3. Pedir tres números y mostrarlos ordenados de mayor a
menor.
/*
* To change this license header, choose License
Headers in Project Properties.
* To change this template file, choose Tools |
Templates
* and open the template in the editor.
*/
package javaapplication32;
import java.util.Scanner;
/**
*
* @author
*/
public class JavaApplication32 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner scn=new Scanner (System.in);
System.out.println("INGRESAR PRIMER
NUMERO");
int num1=scn.nextInt();
System.out.println("INGRESAR SEGUNDO
NUMERO");
int num2=scn.nextInt();
System.out.println("INGRESAR TERCER
NUMERO");
int num3=scn.nextInt();
}
4. Pedir dos números y mostrarlos ordenados de mayor a
menor.
package javaapplication34;
import java.util.Scanner;
int n1,n2;
System.out.println("INTRUDUZCA PRIMER
NUMERO:");
n1=n.nextInt();
System.out.println("INTRUDUZCA SEGUNDO
NUMERO:");
n2=n.nextInt();
if (n1>n2){
System.out.println(n1+"y"+n2);
else
System.out.println(n2+"y"+n1);
}
5. Pedir dos números y decir cual es el mayor o si son iguales.
package javaapplication35;
import java.util.Scanner;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int n1,n2;
Scanner Entrada=new Scanner (System.in);
if(n1==n2)
System.out.println("Son iguales");
else
{
if(n1>n2)
System.out.println(n1 + " es mayor que " + n2);
else
System.out.println(n2 + " es mayor que " + n1);
}}
6. Pedir una nota de 0 a 10 y mostrarla de la forma:
Insuficiente, Suficiente, Bien, notable.
package javaapplication36;
import java.util.Scanner;
public class JavaApplication36 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// 6. Pedir una nota de 0 a 10 y
mostrarla de la forma: Insuficiente, Suficiente,
Bien, notable.
Scanner Entrada=new Scanner (System.in);
int nota;
System.out.print("Introduzca una nota: ");
nota=Entrada.nextInt();
// tanto los if's como los else's encierran a una
sola instrucción
// y no es necesario utilizar llaves { }
if(nota==9 || nota==10 )
System.out.println("SOBRESALIENTE");
}
}
7. Pedir el día, mes y año de una fecha correcta y mostrar la
fecha del día siguiente. suponer que todos los meses tienen
30 días.
package javaapplication37;
import java.util.Scanner;
public class JavaApplication37 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner insertar=new Scanner (System.in);
int dia,mes,año;
// para que una fecha sea correcta se tiene que
cumplir
// día en el rango 1..30
// mes en el rango 1..12
// año cualquiera distinto del 0
System.out.print("Introduzca día: ");
dia=insertar.nextInt();
System.out.print("Introduzca mes: ");
mes=insertar.nextInt();
System.out.print("Introduzca año: ");
año=insertar.nextInt();
if (año != 0)
System.out.println ("Fecha correcta");
else
System.out.println ("Año incorrecto");
else
System.out.println("Mes incorrecto");
else
System.out.println("Día incorrecto");
}
8. Pedir dos fechas y mostrar el número de días que hay de
diferencia. Suponiendo todos los meses de 30 días.
package javaapplication38;
import java.util.Scanner;
public class JavaApplication38 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner insertar=new Scanner (System.in);
int dia1,mes1,año1;
int dia2,mes2,año2;
int total_dias;
System.out.println ("PRIMERA FECHA:");
System.out.print("Introduzca día: ");
dia1=insertar.nextInt();
System.out.print("Introduzca mes: ");
mes1=insertar.nextInt();
System.out.print("Introduzca año: ");
año1=insertar.nextInt();
System.out.println ("SEGUNDA FECHA:");
System.out.print("Introduzca día: ");
dia2=insertar.nextInt();
System.out.print("Introduzca mes: ");
mes2=insertar.nextInt();
System.out.print("Introduzca año: ");
año2=insertar.nextInt();
// suponemos que las fecha introducidas son
correctas
// convertimos las dos fechas a días y calculamos
la diferencia
total_dias = dia2-dia1 + 30*(mes2-mes1)+365*(año2-
año1);
System.out.println ("Días de diferencia: " +
total_dias);
}
}
package javaapplication39;
import java.util.Scanner;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner Entrada=new Scanner (System.in);
int h,m,s; // hora, minutos y segundos
System.out.print("INSERTE HORA: ");
h=Entrada.nextInt();
System.out.print("INSERTE MINUTOS: ");
m=Entrada.nextInt();
System.out.print("INSERTE SEGUNDOS: ");
s=Entrada.nextInt();
// suponemos que la hora introducida es correcta
package javaapplication40;
import java.util.Scanner;
public class JavaApplication40 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner Entrada=new Scanner (System.in);
int num;
System.out.print("Introduzca un número: ");
num=Entrada.nextInt();
while(num!=0) // mientras num sea distinto de 0
{
if(num>0)
// mayor que cero: positivo
System.out.println("Positivo");
else
// si no es positivo: es negativo
System.out.println("Negativo");
// repetimos el proceso y volvemos a leer num
System.out.print("Introduzca otro número: ");
num=Entrada.nextInt();
// al salir del mientras tenemos la certeza que num
es 0
}}}