0

i have rewritten this in a do while loop but how can i make the division part retun/accept just three decimal place for example is if i get 1/4 i should be able to enter 0.25 and also if i get 1/7 i should be able to get 0.14.

 import java.util.*;
public class HelloWorld
 {
  public static void main (String[] args)
{
    Scanner keyboard=new Scanner(System.in);
    Random quest=new  Random();
    int module,range,number,count,quest1,quest2,answer,score;
    double result;

    System.out.print("Enter module(1 for addition, 2 for subtraction, 3 for multiplication, 4 for division, -1 for exit)?");
    module=keyboard.nextInt();
    do{
        System.out.print("Enter range of numbers(1-100)");
        range=keyboard.nextInt();

        System.out.print("How many questions do you want to practice(minimum 3)?");
        number=keyboard.nextInt();

        count=1;
        score=0;
        result=0;

        while (count<= number)
        {
            quest1=quest.nextInt(range)+1;
            quest2=quest.nextInt(range)+1;

            if (module==1)
            {
                result=quest1+quest2;
                System.out.print(quest1+"+" +quest2+"=");
            }
            else
            {
                if (module==2)
            {
                result=quest1-quest2;
                System.out.print(quest1+"-" +quest2+"=");
            }
            else
            {
                if (module==3)
            {
                result=quest1*quest2;
                System.out.print(quest1+"*" +quest2+"=");
            }
            else
            {
                if (module==4)
            {
                result=(double)quest1/quest2;
                System.out.print(quest1+"/" +quest2+"=");
            }
            }
            }
            }
            answer=keyboard.nextInt();
            if(answer==result)
            {
                score=score+1;
                System.out.println("you are correct!");
            }
            else {
                System.out.println("You are wrong, the correct answer is " +result);
            }
            count=count+1;
            if(count>number)
            {
                System.out.println("you scored "+score+" out of "+number+".");
                count=1;
                score=0;
                break;
            }
        }
        System.out.print("Enter module(1 for addition, 2 for subtraction, 3 for multiplication, 4 for division, -1 for exit)?");
    module=keyboard.nextInt();
    }while((module>0) && (module<5));
    System.out.println("The program is terminating......");


}

}

Michael
  • 1
  • 2
  • Those two are not the same, so are you sure you want it ? Simply use the correct syntax `do{...}while(condition);` note the `;` at the end. For you math problem, see [My answer about Why is the result of 1/3 == 0?](http://stackoverflow.com/a/41822430/4391450) – AxelH May 17 '17 at 05:38
  • PS : you should use a switch for your module ;) – AxelH May 17 '17 at 05:47
  • @Michael this is a question(singular) and answer site. please narrow down your post to 1 question, and shorten code to a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) –  May 17 '17 at 05:56
  • rather than using `else { if (condition){` use `else if (condition) {`, it is the same result with less confusing braces –  May 17 '17 at 06:38

1 Answers1

-1

Simple put whatever you have in the while loop, into a do - while loop like so:

do{
   // Execute whatever
} while(conditionIsTrue);

Note that the difference between a while loop and a do while is that the do while loop executes 1 time always and then loops, where the while loop may not execute not even once because of the condition.

Personally I prefer tu use the while loop.

  • 2
    "_Personally I prefer tu use the while loop._" but it doesn't do the same ... so that not's a preference but a need ;) – AxelH May 17 '17 at 05:43
  • This is a generic explanation about the language construct. That is not an answer to the question – Sharon Ben Asher May 17 '17 at 05:46
  • Thanks for your reply but what about my other questions? – Michael May 17 '17 at 05:48
  • @Michael your other question as duplicate, see my comment above – AxelH May 17 '17 at 05:53
  • 1
    @Michael StackOverflow is designed to be a site that collects questions and answers so that later, others who have the same or similar question can find answers to their questions. Posting a laundry list of questions in one post goes against the format, and defeats the purpose of the site. If you have multiple questions, then post multiple questions. – ajb May 17 '17 at 05:56
  • okay thanks i'm new here, my other question is if i use choose divide and i get a question like 1/4, i enter my answer as 0.25 i get an error – Michael May 17 '17 at 06:06
  • can i just use result=(double)quest1/(double)quest2 – Michael May 17 '17 at 06:08
  • @Michael I meant "post multiple questions" by starting a new question for each one, with the Ask Question button. – ajb May 17 '17 at 13:16