Advertisement
joseleonweb

Untitled

Sep 21st, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.10 KB | None | 0 0
  1. import java.util.Scanner;
  2. //Un programa en què cal endevinar un nombre.
  3.  
  4. public class EndevinaControlErrorsEntrada {
  5.     //PAS 1
  6.     //El nombre per endevinar serà el 4.
  7.     public static final int VALOR_SECRET = 4;
  8.     public static void main (String[] args) {
  9.         Scanner lector = new Scanner(System.in);
  10.         System.out.println("Comencem el joc.");
  11.         System.out.print("Endevina el valor enter, entre 0 i 10: ");
  12.         boolean tipusCorrecte = lector.hasNextInt();
  13.         if (tipusCorrecte) {
  14.             //S’ha escrit un enter correctament. Ja es pot llegir.
  15.             int valorUsuari = lector.nextInt();
  16.             lector.nextLine();
  17.             if (VALOR_SECRET == valorUsuari) {
  18.                 System.out.println("Exacte! Era " + VALOR_SECRET + ".");
  19.             } else {
  20.                 System.out.println("T’has equivocat!");
  21.                 } 21 System.out.println("Hem acabat el joc.");
  22.         } else {
  23.             //No s’ha escrit un enter.
  24.             System.out.println("El valor introduït no és un enter.");
  25.         }
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement