/* Create a program that prompts the user to enter a password. Display a suitable message for each correct or incorrect attempt made. The user must be given three tries after which the program should terminate. */ import java.util.*; import java.util.Random; import javax.swing.*;
public class PatricHenningspassword
{ public static void main (String [] args) { Scanner kb = new Scanner (System.in); String pass=""; int c = 1; int code=0; String tryer; String out= rev_num();
while (!pass.equals(out) && c<=3)
{
c++;
System.out.println("password is : "+out);
System.out.println(" Please enter password");
pass = kb.next();
if (pass == null)
System.exit(0);
if (pass.equals(out))
{
System.out.println("Well Done You Right on Target");
}
else
{
if (c<=2 && !pass.equals(out))
{ System.out.println("nice try sorry \n \n");
System.out.println("Enter password its your second last chance!!!!");
pass = kb.next();
c++;
if (pass.equals(out))
{
System.out.println("Well Done You Right on Target");
}
else
{ if (c==3 && !pass.equals(out))
{
System.out.println("nice try sorry last try \n \n");
System.out.println("Enter password its your last chance!!!!");
pass = kb.next();
c++;
if (pass.equals(out))
{
System.out.println("Well Done You Right on Target");
}
else
{
c++;
System.out.println("Access Denied !!!!!!!!!");
System.exit(0);
}
}
}
}
}
}
}
public static String rev_num()
{
int a=0;int d=0;int g=0;int j=0;
int b=0;int e=0;int h=0;
int c=0;int f=0;int i=0;
int counter=0;
int starter=0;
int cot=1;
for (int y=9;y<2;)
{
counter--;
}
for (int z=7;z<=4;)
{
z--;
cot++;
starter=(int)(Math.random() *counter +1);
}
String outr="";
a = (int)(Math.random() * 9 +counter);
b = (int)(Math.random() * 9 +starter);
c = (int)(Math.random() * 9 +cot);
d = (int)(Math.random() * 9 +counter);
e = (int)(Math.random() * 9 +1);
f = (int)(Math.random() * 9 +starter);
g = (int)(Math.random() * 9 +cot);
h = (int)(Math.random() * 9 +counter);
i = (int)(Math.random() * 9 +starter);
j = (int)(Math.random() * 9 +cot);
String k=Integer.toString(a); String l=Integer.toString(b); String m=Integer.toString(c);
String n=Integer.toString(d); String o=Integer.toString(e); String p=Integer.toString(f);
String q=Integer.toString(g); String r=Integer.toString(h); String s=Integer.toString(i);
String t=Integer.toString(i);
outr=k+l+m+n+o+p+q+r+s+t;
return outr;
//cannot return again; illegal
}
}
Hi Friend,
Try the following code:
import java.util.*;
import javax.swing.*;
public class PatricHenningspassword{
public static void main (String [] args) {
Scanner kb = new Scanner (System.in);
String pass="";
int c = 1;
int code=0;
String tryer;
String out= rev_num();
while (!pass.equals(out) && c<=3){
c++;
System.out.println("password is : "+out);
System.out.println(" Please enter password");
pass = kb.next();
if (pass == null)
System.exit(0);
System.out.println();
if (pass.equals(out)){
System.out.println("Congratulations!");
}
else{
System.out.println();
if (c<=2 && !pass.equals(out)){
System.out.println("Invalid Password! Try Again!");
pass = kb.next();
c++;
if (pass.equals(out)){
System.out.println("Congratulations");
}
else{
if (c==3 && !pass.equals(out)){
System.out.println();
System.out.println("Invalid Password! Try Again!");
System.out.println("**Note! This is your last attempt!");
pass = kb.next();
c++;
System.out.println();
if (pass.equals(out)){
System.out.println("Congratulations!");
}
else{
c++;
System.out.println("Access Denied !!!!!!!!!");
System.exit(0);
}
}
}
}
}
}
}
public static String rev_num(){
int a=0;int d=0;int g=0;int j=0;
int b=0;int e=0;int h=0;
int c=0;int f=0;int i=0;
int counter=0;
int starter=0;
int cot=1;
for (int y=9;y<2;){
counter--;
}
for (int z=7;z<=4;){
z--;
cot++;
starter=(int)(Math.random() *counter +1);
}
String outr="";
a = (int)(Math.random() * 9 +counter);
b = (int)(Math.random() * 9 +starter);
c = (int)(Math.random() * 9 +cot);
d = (int)(Math.random() * 9 +counter);
e = (int)(Math.random() * 9 +1);
f = (int)(Math.random() * 9 +starter);
g = (int)(Math.random() * 9 +cot);
h = (int)(Math.random() * 9 +counter);
i = (int)(Math.random() * 9 +starter);
j = (int)(Math.random() * 9 +cot);
String k=Integer.toString(a); String l=Integer.toString(b); String m=Integer.toString(c);
String n=Integer.toString(d); String o=Integer.toString(e); String p=Integer.toString(f);
String q=Integer.toString(g); String r=Integer.toString(h); String s=Integer.toString(i);
String t=Integer.toString(i);
outr=k+l+m+n+o+p+q+r+s+t;
return outr;
}
}
Thanks