Solución Problema 1:
import Console.*;
import java.util.*;
public class Juego
{
public static void main(String args[]){
Console C = new Console();
Random R = new Random();
int piedras = 10 + R.nextInt(31);
int i, respuesta;
int jugador = 1; // Indica el turno de cada jugador
while (piedras > 0){
// Imprimiendo el “monton” de piedras
for (i = 1; i <= piedras; i++)
C.print("*");
C.println();
// Pidiendo la respuesta del jugador corresponiente
C.println("Cuantas piedras saca el jugador " + jugador + "?");
respuesta = C.readInt();
// Verificando las reglas del juego
if (respuesta < 1)
C.println("Tiene que sacar al menos 1 piedra.");
else if (respuesta > 3)
C.println("No puede sacar mas de 3 piedras.");
else if (respuesta > piedras)
C.println("Quedan solo " + piedras + " piedras");
else{
piedras -= respuesta;
// Le toca al siguiente jugador
jugador +=1;
if (jugador > 2)
jugador = 1;
if (piedras == 0)
C.println("El ganador es el jugador " + jugador + ". Felicitaciones!");
}
}
}
}
Solución Problema 2:
import java.io.*;
public class Notas
{
public static void main(String args[]) throws IOException
{
String linea, nombre, aux1 = null, aux2 = null;
int n1 = 0, n2 = 0;
BufferedReader R = new BufferedReader(new FileReader("notas.txt"));
PrintWriter W = new PrintWriter(new FileWriter("notas2.txt"));
// Leyendo archivo de “notas.txt”
while((linea = R.readLine()) != null){
if (linea.indexOf(",") < 0){ // Si solo aparece el nombre del alumno
nombre = linea;
n1 = 10;
n2 = 10;
} else { // Si al menos hay una nota
nombre = linea.substring(0, linea.indexOf(","));
linea = linea.substring(linea.indexOf(",") + 1);
if (linea.indexOf(",") < 0){ // Si solo hay una nota
aux1 = linea;
aux2 = null;
}else{ // Si estan las dos notas
aux1 = linea.substring(0, linea.indexOf(","));
aux2 = linea.substring(aux1.length()+1);
}
// Identificando cada nota y transformando a entero
if (aux1.indexOf("P1") > 0){
n1 = Integer.parseInt(aux1.substring(aux1.indexOf("=")+1));
if (aux2 != null)
n2 = Integer.parseInt(aux2.substring(aux2.indexOf("=")+1));
else
n2 = 10;
} else if (aux1.indexOf(“P2”) > 0){
n2 = Integer.parseInt(aux1.substring(aux1.indexOf(“=”)+1));
if (aux2 ¡= null)
n1 = Integer.parseInt(aux2.substring(aux2.indexOf(“=”)+1));
else
n1 = 10;
}
}
W.println(nombre+”:”+n1+”:”+n2+”:”+((n1+n2)/2)); //Escribe las notas en el nuevo archivo
}
R.close();
W.close();
}
}