Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

A little help with java plz

Options
  • 28-09-2001 2:03pm
    #1
    Registered Users Posts: 1,684 ✭✭✭


    Ok heres the story i took i havent done java for a year and im way behind but manage to getthe programs done just but im never happ with them. If any one could see if they could improve this one (its allready been marked its just to see how otheres might finish it off that im curios about) (also is there a command in java when the program is activated that it does a cls?) heres the code ive done and is marked.

    import java.io.*;
    public class arithmetic {
    public static void main (String [] args) throws IOException {
        
        
        	int num1       = 0;
        	int num2       = 0;
       	String retry = "yes";
    
            char op = ' ';
            
      
    
       	 BufferedReader kbd = 	new BufferedReader (new InputStreamReader (System.in));
        	 Double ddd;  //Double Wrapper Class
    
        	 while (retry.equals("yes")){
    
      System.out.println        ("\n\n................................................................");    
      
        	System.out.println        ("\nEnter your first number:");
        	num1    = Integer.parseInt (kbd.readLine () );
    
          
         	if (num1 > 100 )
         
    	System.out.println("\nOnly numbers that are between 1 and 100 will be considered as a valid number.\n\n Please enter a valid number:");
     
        
      		 while (num1 >100){
       
      		 num1    = Integer.parseInt (kbd.readLine () );
        
       							} 
        
    
    	System.out.println        ("\nEnter your second number:");
      	  num2    = Integer.parseInt (kbd.readLine () );
        
    
    	if (num2 > 100 )
         
    	System.out.println("\nOnly numbers that are between 1 and 100 will be considered as a valid number.\n\n Please enter a valid number:");
     
        
      	 while (num2 >100){
       
      	num2    = Integer.parseInt (kbd.readLine () );
    
    							}
    
    	System.out.println        ("\nPlease enter one of the following signs * or / or -  or + ");
      	  op = (char) kbd.read ();
    			kbd.readLine ();
    
    
    	while (op != '+' && op != '-' && op != '*' && op != '/') {
                        	System.out.println("The only characters that will be excepted are + - * / enter one of these");
                       	 op = (char) kbd.read ();
    		    	kbd.readLine ();
    	}	
    	if (op=='/') {
                        System.out.println("Result:" + (num1 / num2));
                      } else if (op=='*') {
                         System.out.println("Result:" + (num1 * num2));
                      } else if (op=='-') {
                          System.out.println("Result:" +(num1 - num2));
                      } else if(op=='+') {
                          System.out.println("Result:" +(num1 + num2));
                       }
    
    		
                                      
    		System.out.println("Do you want to try another equation?:(yes/exit)");
    		retry = kbd.readLine ();
                 
    	
    }      }
        
    	}
    

    Any ideas?


Comments

  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Instead of going through the whole rigamarole of 'enter no. 1', 'enter no. 2' etc, just ask the user to input the entire equation, with our without spaces, it's up to you, and then work it out from there. Also instead of just doing all the ifs and whiles in the main method, design some methods to deal with finding the type of equation for example. Your code probably wouldn't be much shorter, but it would look way neater, more professional and well-thought out, and that's what gets you marks in assignments(at least in our class anyway). Hope this helps.

    seamus


  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    Or if you *really* want to impress them use Roman numerals, always a good one :)

    As for the cls, a simple trick is to output newlines until you get a blank page. (Did this before in C++ but not too sure how easy it would be to translate into Java).


  • Registered Users Posts: 932 ✭✭✭yossarin


    presuming that this is windows - you can write a *.bat file that runs:

    @echo off
    cls
    java arithmetic

    giving you a clean screen to write with+ saving you typing the java command over and over

    also in the code - the main method could just create a new instance of the arithmetic object - and you could split the code up into several methods in the object that can then be reused - one to read in from system.in that returned an int or an error code ( -1, or summat) iff someone gave a string instead of an int, for example


Advertisement