# need help using do-while loop ?

Page 1 of 1

## 1 Replies - 368 Views - Last Post: 08 April 2012 - 07:01 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=274417&amp;s=73b8e4b4300cdf5336f25950790ccbf3&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 misiek

Reputation: 0
• 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( "2 MULTIPLY & DIVISION");
System.out.println( "3 Exit");

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

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

Reputation: 8363
• Posts: 31,955
• Joined: 06-March 08

## Re: need help using do-while loop ?

Posted 08 April 2012 - 07:01 PM

Duplicated post closed