Question: how can i simplify my java code

Ads

 
 
 

Share on Google+Share on Google+

Patric Henning
how can i simplify my java code
1 Answer(s)      6 years and 6 months ago
Posted in : Java Beginners

/* 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 


  }

}

Ads
View Answers

November 15, 2010 at 2:51 PM


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


Ads









Related Tutorials/Questions & Answers:
how can i simplify my java code
how can i simplify my java code  /* Create a program that prompts... Scanner (System.in); String pass=""; int c = 1; int code=0... c=0;int f=0;int i=0; int counter=0; int starter=0; int cot=1
How can I to my database to my application
How can I to my database to my application  How can I to my database to my application   Hi, Please see the JDBC discussion thread. Thanks
Advertisements
How i can send testing mail on my id using java?
How i can send testing mail on my id using java?  Which packages i should use to implementing java mail service??? Thanks
My Base Class is Changing every time in my code. How I can overcome this?
My Base Class is Changing every time in my code. How I can overcome this?  I have had the below question asked in interview, i'm curious to learn the answer. I have a base class, 'GeneratorBaseClass' that is extended
How can I protect my database password ?
How can I protect my database password ?   How can I protect my database password ? I'm writing a client-side java application that will access... in as plain text. What can I do to protect my passwords
How can i modify my account in roseindia
How can i modify my account in roseindia  Presently am not using my gmail id. I have to modify my roseindia account. Please send the answer to following mail id
How can I connect my database to my application ?
How can I connect my database to my application ?  How can I connect my database to my application?   Hi, You can use JDBC API to connect to database from your Java application. Here is the sample code to connect
How can I find my DHCP IP address on my computer?
How can I find my DHCP IP address on my computer?  Hi, I am using Ubuntu and I trying to find the IP address assigned to my computer by DHCP server. How to find the IP address? Thanks   Hi, You can try following
How can I open my Java desktop app by clicking on its file?
How can I open my Java desktop app by clicking on its file?  How can I open my Java desktop app by clicking on the file it creates? This app creates... for this app. For instance, imagine I create an editor app called BlueSky
How can i add a new count to this source code ? - Java Beginners
How can i add a new count to this source code ?  Hi everyone I...;ZHTYPE (Just Logical) Please tell me how can i see this output? Code... This output with this code is true but I want add ZHTYPE at the end of each line
How can i add a new count to this source code ? - Java Beginners
How can i add a new count to this source code ?  Hi everyone I...;ZHTYPE (Just Logical) Please tell me how can i see this output? Code... with this code is true but I want add ZHTYPE at the end of each line + XTSM/XTS please
how i can add an horizontal scrollbar at my PdfAnnotation ? - JSP-Servlet
how i can add an horizontal scrollbar at my PdfAnnotation ?  How to add the an horizontal scrollbar at my PdfAnnotation ? for example document d=new document(0. document.open
how can i use toggle button in my application
how can i use toggle button in my application  i want a on/off toggle button by dragging left-right..not by pressing on button
my hibernate showing the following exception so how i can resolve it
my hibernate showing the following exception so how i can resolve it   Caused by: java.lang.ClassNotFoundException: org.hibernate.Session at java.net.URLClassLoader$1.run(Unknown Source
How can I learn Java?
How can I learn Java?  Hi, I have just completed a course in HTML.... Now I want to learn Learn Java programming and become the expert in Java programming. How can I learn Java? in shortest possible time. I mean I just want to begin
I want to change my user name on your website ,how can i do this
I want to change my user name on your website ,how can i do this  I want to change my user name on your website ,how can i do this   You can create another account very easily with different username
how can i run tomcat server and my home page come when i double click on an icon in servlets
how can i run tomcat server and my home page come when i double click on an icon in servlets  Hi.. there if we are giving a small application to end user he does not know to run server by service runner... and he does not know
how can i run tomcat server and my home page come when i double click on an icon in servlets
how can i run tomcat server and my home page come when i double click on an icon in servlets  Hi.. there if we are giving a small application to end user he does not know to run server by service runner... and he does not know
how can i run tomcat server and my home page come when i double click on an icon in servlets
how can i run tomcat server and my home page come when i double click on an icon in servlets  Hi.. there if we are giving a small application to end user he does not know to run server by service runner... and he does not know
how can i run tomcat server and my home page come when i double click on an icon in servlets
how can i run tomcat server and my home page come when i double click on an icon in servlets  Hi.. there if we are giving a small application to end user he does not know to run server by service runner... and he does not know
How can I Convert my Image Files to Text Files? - IDE Questions
How can I Convert my Image Files to Text Files?  How can I Convert my Image Files to Text Files
how can i close a frame. - Java Beginners
how can i close a frame.  Hi, My question is how can we close...............my target is when i click on that button, a new frame is coming and updated table... frames as i go on clicking button...........can we have like when i click on button
how can i add wirecard option(or button ) at my web page plz tell all steps? View Answers
how can i add wirecard option(or button ) at my web page plz tell all steps? View Answers  how can i add wirecard option(or button ) at my web page plz tell all steps? View Answers
How can i implement the calculator programe in jsp code
How can i implement the calculator programe in jsp code  Please send code for the calculator using jsp technologies please guidelines to me.   Here is a simple jsp calculator code that accepts two numbers from the user
how can i create a mysql database to connect to this code - JDBC
how can i create a mysql database to connect to this code  i need help creating a mysql database for this code. code is import java.awt....";, "root", "root"); Statement st=con.createStatement(); int i
how can i create a mysql database to connect to this code - JDBC
how can i create a mysql database to connect to this code  i need help creating a mysql database for this code. code is import java.awt....";, "root", "root"); Statement st=con.createStatement(); int i
i want to find the byte code of a image file ... for my project..plz if anybody help me for java coding i will grateful..
i want to find the byte code of a image file ... for my project..plz if anybody help me for java coding i will grateful..   i want to convert... project.. but i cant convert Image file to its byte code format.. if anybody can plz
How can i draw a line using java swings
How can i draw a line using java swings  Sir my program contains different components i am using JFrame. I want to draw a straight line between components(Jtextfeilds, Jlabels).So could you help me for it. Thank You
How can i draw a line using java swings
How can i draw a line using java swings  Sir my program contains different components i am using JFrame. I want to draw a straight line between components(Jtextfeilds, Jlabels).So could you help me for it. Thank You
how can i connected jsp wih mysql - Java Beginners
how can i connected jsp wih mysql  i have a code jsp file and connection file connection file will be compiling successful. i m using session in my... to connect this and how can i use it plz help me its very urgent plz send my
i am unable to identify the error in my code
i am unable to identify the error in my code  class Program { public static void main(String[] args) { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("enter
Tutorials   
Java Spring Hibernate Struts Training What is the meaning of Java Platform? Why Java is a platform independent language? What is the benefits of learning Core Java? Which technology should I learn after Java? What is array in java with example? How to Convert ArrayList to Array? How to substring in Java? How to format number in Java? What is instance variable in Java? How to download MySQL JDBC driver? What is Calendar class in Java? Which is the best Java tutorials for beginners? How to rename a file in Java? How to delete file in Java code? How to get day from date in Java using Calendar? How to get day of week in Java? How to calculate Date Difference in Java? How to compare date in Java? How to declare array in Java? How to calculate average of array in Java? What is Array in Java? write a java program to find the summation of all the integers entered on command line Sum of two numbers using command line arguments in Java How to create and use Array in Java? How to pass command line arguments in Java? How to create Applet Hello World? Appending String efficiently in Java How to append String in Java? How to list even numbers between 1 and 100? How to add BigDecimal in Java? What is Abstraction In Java? Which is best Beginners Java Tutorial? What is java.util package? Create list from array in Java Filter collection in Java 8 What is the best way to filter a Java Collection? Easy way to transform Collection to Array? How to convert Collection to Array in Java? What are Basic Java Language Elements? Advanced Java Tutorials in 2017 Java brief history Best Reasons to learn Java Java Example Codes and Tutorials in 2017 How do I read a large file quickly in Java? Is learning Java worthwhile? How to create first Java Program? Retrieve database from the table dynamically in jsp from oracle using servlet What does core Java include? java What are some way to learn Java quickly?

Ads

 
Advertisement null

Ads