Java_exercises
FLIP COINS
class HelloDavid {
public static void main(String[] args) {
String C1D = "HEAD";
String C2D = "HEAD";
String C3D = "HEAD";
int C1,C2,C3,I,Toss=0;
I=55;
System.out.println("Flip Three coins ");
while (I==55) {
C1= (int) Math.round(Math.random()*10);
C2= (int) Math.round(Math.random()*10);
C3= (int) Math.round(Math.random()*10);
Toss = Toss + 1;
if (C1<5) { C1D="HEAD"; } else { C1D="SEAL"; }
if (C2<5) { C2D="HEAD"; } else { C2D="SEAL"; }
if (C3<5) { C1D="HEAD"; } else { C3D="SEAL"; }
System.out.println("Toss:"+ Toss +" Coin1:"+ C1D + " Coin2:"+ C2D + " Coin3:"+ C3D );
if ((C1>4)&(C2>4)&(C3>4)) { I=60; }
}
System.out.println("Finish Target!");
}
}
================================================================
https://runmycode.online/codesheet.html
=================================================================
class HelloDavid {
public static void main(String[] args) {
String C1D = "HEAD";
String C2D = "HEAD";
String C3D = "HEAD";
int C1,C2,C3,I,Toss=0;
I=55;
System.out.println("Flip Three coins ");
while (I==55) {
C1= (int) Math.round(Math.random()*10);
C2= (int) Math.round(Math.random()*10);
C3= (int) Math.round(Math.random()*10);
Toss = Toss + 1;
if (C1<5) { C1D="HEAD"; } else { C1D="SEAL"; }
if (C2<5) { C2D="HEAD"; } else { C2D="SEAL"; }
if (C3<5) { C1D="HEAD"; } else { C3D="SEAL"; }
System.out.println("Toss:"+ Toss +" Coin1:"+ C1D + " Coin2:"+ C2D + " Coin3:"+ C3D );
if ((C1>4)&(C2>4)&(C3>4)) { I=60; }
}
System.out.println("Finish Target!");
}
}
================================================================
https://runmycode.online/codesheet.html
=================================================================
Flip Three coins
Toss:1 Coin1:HEAD Coin2:SEAL Coin3:HEAD
Toss:2 Coin1:HEAD Coin2:SEAL Coin3:SEAL
Toss:3 Coin1:HEAD Coin2:SEAL Coin3:SEAL
Toss:4 Coin1:HEAD Coin2:HEAD Coin3:SEAL
Toss:5 Coin1:HEAD Coin2:HEAD Coin3:SEAL
Toss:6 Coin1:HEAD Coin2:SEAL Coin3:SEAL
Toss:7 Coin1:HEAD Coin2:SEAL Coin3:SEAL
Toss:8 Coin1:HEAD Coin2:SEAL Coin3:SEAL
Toss:9 Coin1:HEAD Coin2:SEAL Coin3:SEAL
Toss:10 Coin1:HEAD Coin2:SEAL Coin3:SEAL
Toss:11 Coin1:HEAD Coin2:SEAL Coin3:SEAL
Toss:12 Coin1:HEAD Coin2:SEAL Coin3:SEAL
Toss:13 Coin1:HEAD Coin2:HEAD Coin3:SEAL
Toss:14 Coin1:HEAD Coin2:SEAL Coin3:SEAL
Toss:15 Coin1:HEAD Coin2:HEAD Coin3:SEAL
Toss:16 Coin1:SEAL Coin2:SEAL Coin3:SEAL
Finish Target!
==========================================================================
POSTAL CODE - CANADA
import java.io.*;
import java.util.*;
class CanadaPostCode {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
System.out.println("ENTER CANADIAN POSTAL CODE:");
char CharPostCode[] = kb.nextLine().toCharArray();
boolean ValidPostCode = false;
int LenPostalCode = CharPostCode.length;
if (LenPostalCode != 7) {System.out.println("The Canadian Postal Code Entered is not valid");}
if (LenPostalCode == 7) {
if (Character.isLetter( CharPostCode[0]) && Character.isDigit( CharPostCode[1]) &&
Character.isLetter( CharPostCode[2]) && Character.isWhitespace( CharPostCode[3]) &&
Character.isDigit( CharPostCode[4]) && Character.isLetter( CharPostCode[5]) &&
Character.isDigit( CharPostCode[6]) )
{ ValidPostCode = true; System.out.println("The Canadian Postal Code is VALID");}
else { System.out.println("The Canadian Postal Code Entered is NOT valid");}
}
System.out.println("Canada Post Code Entered: ");
System.out.println( CharPostCode);
System.out.println("Goodbye");
}
}
=========================================================
PHONE NUMBER VALIDATION
import java.io.*;
import java.util.*;
public class PhoneCode
{
public static void main(String[] args)
{
//int LenPhoneNumber = 15;
char Numberp1='-';
char Numberp2='(';
char Numberp3=')';
boolean Finish = false;
char Leter1 = 'E';
char Leter2 = 'X';
char Leter3 = 'I';
char Leter4 = 'T';
while (Finish == false)
{
Scanner kb = new Scanner(System.in);
System.out.println("ENTER PHONE NUMBER:");
char CharPhoneNumber[] = kb.nextLine().toCharArray();
int LenPhoneNumber = CharPhoneNumber.length;
boolean Validlen = false;
boolean ValidPhone = false;
int i=0;
int countnum=0;
if ((CharPhoneNumber[0] == Leter1) && (CharPhoneNumber[1] == Leter2) &&
(CharPhoneNumber[2] == Leter3) && (CharPhoneNumber[3] == Leter4)) {
Finish = true;}
if ((LenPhoneNumber == 10) ||(LenPhoneNumber == 12)||(LenPhoneNumber == 13)) { Validlen = true;}
if (Validlen == true) {
for (i=0;i<LenPhoneNumber;i++) {
if (Character.isDigit( CharPhoneNumber[i])) { countnum = countnum +1; } }
System.out.println( "Qty_Numbers : "+ countnum ); }
if (countnum != 10) {ValidPhone = false;}
if ( ( (Validlen == true) && (countnum == 10) ) ||
((Validlen == true) && (countnum == 10) && (LenPhoneNumber == 12) &&
(CharPhoneNumber[4] == Numberp1) && (CharPhoneNumber[8] == Numberp1)) ||
((Validlen == true) && (countnum == 10) && (LenPhoneNumber == 13) &&
(CharPhoneNumber[0]== Numberp2) && (CharPhoneNumber[4]== Numberp3) &&
(CharPhoneNumber[8]== Numberp1)) )
{ System.out.println("Phone Number Entered is valid");
ValidPhone = true;
Finish = true;
}
if (ValidPhone == false) {System.out.println("Phone Number Entered is NOT valid");
System.out.println("Entered phone again or entry EXIT for finish"); }
}
}
}
=========================================================================
===============================================================
Nota: Dados
import java.util.Random;
public class ThreeDice {
public static void main(String[] args) {
int total = 0, count = 0, dice1 = 0, dice2 = 0, dice3 = 0;
boolean cond1=false; boolean cond2=false;
System.out.println("Roll #\tDice1\tDice2\tDice3");
Random rdm = new Random();
while ((total != 6) || ((dice1 == dice2) || (dice2 == dice3) || (dice3 == dice1) || (dice1 != 1)) && (!cond2)) {
dice1 = rdm.nextInt(6) + 1; dice2 = rdm.nextInt(6) + 1; dice3 = rdm.nextInt(6) + 1;
total = dice1 + dice2 + dice3;
if (total == 6 && (dice1 != dice2) && (dice2 != dice3) && (dice1 != dice3)){
count++; cond1=true; System.out.println(count + " \t" + dice1 + " \t" + dice2 + " \t" + dice3);
if (total == 6 && (dice1 != dice2) && (dice2 != dice3) && (dice1 != dice3) && (dice1 !=1)&& (!cond1)){ cond2=true;}
}
else count++;
}
System.out.println("Number of rolls: " + count);
}
}
public class ThreeDice {
public static void main(String[] args) {
int total = 0, count = 0, dice1 = 0, dice2 = 0, dice3 = 0;
boolean cond1=false; boolean cond2=false;
System.out.println("Roll #\tDice1\tDice2\tDice3");
Random rdm = new Random();
while ((total != 6) || ((dice1 == dice2) || (dice2 == dice3) || (dice3 == dice1) || (dice1 != 1)) && (!cond2)) {
dice1 = rdm.nextInt(6) + 1; dice2 = rdm.nextInt(6) + 1; dice3 = rdm.nextInt(6) + 1;
total = dice1 + dice2 + dice3;
if (total == 6 && (dice1 != dice2) && (dice2 != dice3) && (dice1 != dice3)){
count++; cond1=true; System.out.println(count + " \t" + dice1 + " \t" + dice2 + " \t" + dice3);
if (total == 6 && (dice1 != dice2) && (dice2 != dice3) && (dice1 != dice3) && (dice1 !=1)&& (!cond1)){ cond2=true;}
}
else count++;
}
System.out.println("Number of rolls: " + count);
}
}
Comments
Post a Comment