Results 1 to 5 of 5
  1. #1
    ytrewqc is offline Member
    Join Date
    Nov 2011
    Posts
    12
    Rep Power
    0

    Default how do i reload my program?

    I am new to java and I just build a very simple calculator.
    you enter 2 numbers and you get the sum of those numbers.
    I want to be able to make several calculations without having to restart the program. how do I do that?

    Java Code:
    import java.util.Scanner;
    
    class apples {
    	public static void main(String args[]) {
    		Scanner pie = new Scanner(System.in);
    		
    		int firstnumber;
    		int secondnumber;
    		int total;
    		
    		System.out.println("enter first number");
    		firstnumber = pie.nextInt();
    		
    		System.out.println("enter second number");
    		secondnumber = pie.nextInt();
    		
    		total = firstnumber + secondnumber;
    		
    		System.out.println(firstnumber + " + " + secondnumber + " = " + total);
    	}
    }

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default Re: how do i reload my program?

    Just use while loop for this aim.
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default Re: how do i reload my program?

    Maybe you could put the whole code into a for loop like so:

    Java Code:
    import java.util.Scanner; 
      
    class For { 
        public static void main(String args[]) { 
            Scanner pie = new Scanner(System.in); 
              
            int firstnumber; 
            int secondnumber; 
            int total; 
              
    		for(int x = 1;x <= 10; x++) { 
                 System.out.println("enter first number"); 
                 firstnumber = pie.nextInt(); 
              
                 System.out.println("enter second number"); 
                 secondnumber = pie.nextInt(); 
              
                 total = firstnumber + secondnumber; 
              
                 System.out.println(firstnumber + " + " + secondnumber + " = " + total); 
    		 }
        } 
    }
    Now, you can set the conditional part of the for statement to whichever number you wish. At this moment, there is 10 questions. Only problem here is that only division is calculated. If you wanted to use the different mathematical operators you would need to do a lot more. But the good news is that I was given some very good help from here with a very similar query, so if this is your aim I can help you by referring back.

    Hope this helps.

    Regards,

    NM.
    Last edited by Nanomech; 11-04-2011 at 05:23 PM.
    [A!B]Java

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default Re: how do i reload my program?

    Change this:

    Java Code:
    public static void main(String[] args) {
       // a whole lot of code
    }
    to this:

    Java Code:
    private static void stuff(String[] args) {
       // a whole lot of code
    }
    
    public static void main(String[] args) {
       while (true)
          stuff(args);
    }
    If you're not using the 'args' parameter(s), you can leave them out.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    ytrewqc is offline Member
    Join Date
    Nov 2011
    Posts
    12
    Rep Power
    0

    Default Re: how do i reload my program?

    thanks! it works even though it doesnt work more times then the number i type in, its enough for this "program"

    Quote Originally Posted by Nanomech View Post
    Maybe you could put the whole code into a for loop like so:

    Java Code:
    import java.util.Scanner; 
      
    class For { 
        public static void main(String args[]) { 
            Scanner pie = new Scanner(System.in); 
              
            int firstnumber; 
            int secondnumber; 
            int total; 
              
    		for(int x = 1;x <= 10; x++) { 
                 System.out.println("enter first number"); 
                 firstnumber = pie.nextInt(); 
              
                 System.out.println("enter second number"); 
                 secondnumber = pie.nextInt(); 
              
                 total = firstnumber + secondnumber; 
              
                 System.out.println(firstnumber + " + " + secondnumber + " = " + total); 
    		 }
        } 
    }
    Now, you can set the conditional part of the for statement to whichever number you wish. At this moment, there is 10 questions. Only problem here is that only division is calculated. If you wanted to use the different mathematical operators you would need to do a lot more. But the good news is that I was given some very good help from here with a very similar query, so if this is your aim I can help you by referring back.

    Hope this helps.

    Regards,

    NM.

Similar Threads

  1. reload the value of the variable
    By kiranjava in forum Advanced Java
    Replies: 1
    Last Post: 05-05-2010, 08:20 AM
  2. Properties reload
    By koala993 in forum Advanced Java
    Replies: 0
    Last Post: 04-29-2010, 05:39 PM
  3. How to reload JInternalFrame
    By chyrl in forum AWT / Swing
    Replies: 42
    Last Post: 04-16-2010, 01:29 PM
  4. reload form problem
    By porta325 in forum New To Java
    Replies: 2
    Last Post: 11-01-2007, 04:07 PM
  5. how to reload a jsp page
    By Heather in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-14-2007, 11:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •