need help using do-while loop ?

Page 1 of 1

1 Replies - 289 Views - Last Post: 08 April 2012 - 07:01 PM Rate Topic: -----

#1 misiek  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 30-March 12

need help using do-while loop ?

Posted 08 April 2012 - 06:55 PM

Hi there, what im tryin to do here is to include a add&subtract loop so that i will display for example

sum 2 2 4
sum 3 3 6
diff 3 2 1
diff 5 2 3
this is my source code that i have already made it but idont know if i am puttin my do-while statements in the right lines..so that it goes back to do the same operation..it eventually goes to the next statement which is a multiplication but that im still workin on it..
import java.util.*;
public class Formatting3 
{ 
 public static void main(String[] args) 
    { 
    // This program calculates two decimal numbers  
    double real1, real2, result;
    
    int number1,number2,number3;

    // declare input as a new Scanner from the keyboard
    Scanner input = new Scanner(System.in);
   
       // Ask the user to enter two values with decimals
    System.out.print("Enter two values with decimals ");
    real1= input.nextDouble();
    real2= input.nextDouble();

    	 result=real1+real2;
	 
    // Now add code for integers 
 number1= (int) real1; 
number2= (int) real2;  
number3= number1 + number2; 
 


    // Prompt the user to get a value 
    String formathd = "   Operation      Value1     Value2     Result \n";
    System.out.println(formathd);
    // Real format for decimals
    String op = "   SUM      ";
   	 // Print Reals
    String formatf = op + "%10.2f %10.2f %10.2f \n";
        // show the integers
    System.out.printf(formatf, real1, real2, result);

do{ 

// Ask the user to enter two values whole numbers
    
    real1= input.nextInt();
    real2= input.nextInt();

    result= real1+real2;


	// Real format for the sum 
	String format = " sum " ;
     String formatF = op + "% 5s %5d %5d %5d \n";

    // show the whole numbers
    System.out.printf( formatf, real1, real2, result);


// Ask the user to enter two values with decimals
    System.out.print("Enter two values with decimals ");

    real1= input.nextDouble();
    real2= input.nextDouble();


     result=real1-real2;


 // tell the user that get a number1 and subtract from number2


number3= number1 - number2; 

// Real format for decimals
    String formatdc =  "   Difference      ";
	// Print Reals
    String oput =  " %10.2s %10.2f %10.2f %10.2f \n";
    // show the whole numbers
    System.out.printf(formatf, real1, real2, result);
// Ask the user to enter two values whole numbers
    
    real1= input.nextInt();
    real2= input.nextInt();

    result= real1-real2;
 

number3= number1 - number2; 

    // show the whole numbers
    System.out.printf(formatf, real1, real2, result);



 // ask the user to choose an operation
System.out.println( "please choose an operation");
System.out.println( "1 ADD & SUBTRACTION");
System.out.println( "2 MULTIPLY & DIVISION");
System.out.println( "3 Exit");
   

// ask the user if they want addition and subtraction
  System.out.print (" Do you want 1 for add & subtract ");
      
// Ask the user to enter two values whole numbers
     System.out.print("Enter two whole numbers ");
    
    real1= input.nextInt();
    real2= input.nextInt();

     result=real1-real2;
     result=real1+real2;

      } while (real1<=1);

// now ask the user if they want multiplication and division
   System.out.println (" Do you want 2 for multiplication & division "); 



Is This A Good Question/Topic? 0
  • +

Replies To: need help using do-while loop ?

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: need help using do-while loop ?

Posted 08 April 2012 - 07:01 PM

Duplicated post closed
Please avoid double posting
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1