Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Multiple Computations in 1 file

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry Multiple Computations in 1 file

    having an issue with creating "choice" variable. I am trying to create a program which will ask you a math problem based upon selecting the type of problem.

    Here is the program:

    import java.util.Scanner;
    public class BasicfourLoop {
    public static void main(String[] args) {
    final int NUMBER_OF_QUESTIONS = 5;
    int correctCount = 0;
    int count = 0;
    int choice;
    choice = 4;
    long startTime = System.currentTimeMillis();
    String output = " ";
    Scanner input = new Scanner(System.in);

    while (count < NUMBER_OF_QUESTIONS) {
    int number1 = (int) (Math.random() * 100);
    int number2 = (int) (Math.random() * 100);

    if (number1 < number2) {
    int temp = number1;
    number1 = number2;
    number2 = temp;
    }

    System.out.println("Enter your Choice:");
    System.out.println("Enter 1 for Addition ");
    System.out.println("Enter 2 for Subtraction ");
    System.out.println("Enter 3 for Multiplication ");
    System.out.println("Enter 4 for Division");



    System.out.print("What is " + number1 + " + " + number2 + "? ");
    int answer = input.nextInt();

    if (number1 + number2 == answer) {
    System.out.println("You are One Smart Cookie!!!");
    correctCount++;
    }
    else
    System.out.println("Your answer is stupid.\n" + number1 + " + " + number2 + " should be " + (number1 + number2));
    System.out.print("What is " + number1 + " + " + number2 + "? ");

    count++;

    output += "\n" + number1 + "+" + number2 + "=" + answer + ((number1 + number2 == answer) ? " awesome" : " idiot");
    }

    long endTime = System.currentTimeMillis();
    long testTime = endTime - startTime;
    System.out.println("Correct count is " + correctCount + "\nTest time is " + testTime / 1000 + " seconds\n" + output);
    }
    }


    Please help!!!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,139
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Multiple Computations in 1 file

    Please explain what problems you are having.

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Multiple Computations in 1 file

    Quote Originally Posted by Norm View Post
    Please explain what problems you are having.

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    Sorry

     
    import java.util.Scanner;
    public class BasicfourLoop {
    public static void main(String[] args) {
    final int NUMBER_OF_QUESTIONS = 5;
    int correctCount = 0;
    int count = 0;
    int choice;
    choice = 4;
    long startTime = System.currentTimeMillis();
    String output = " ";
    Scanner input = new Scanner(System.in);
     
    while (count < NUMBER_OF_QUESTIONS) {
    int number1 = (int) (Math.random() * 100);
    int number2 = (int) (Math.random() * 100);
     
    if (number1 < number2) {
    int temp = number1;
    number1 = number2;
    number2 = temp;
    }
     
    System.out.println("Enter your Choice:");
    System.out.println("Enter 1 for Addition ");
    System.out.println("Enter 2 for Subtraction ");
    System.out.println("Enter 3 for Multiplication ");
    System.out.println("Enter 4 for Division");
     
     
     
    System.out.print("What is " + number1 + " + " + number2 + "? ");
    int answer = input.nextInt();
     
    if (number1 + number2 == answer) {
    System.out.println("You are One Smart Cookie!!!");
    correctCount++;
    }
    else
    System.out.println("Your answer is stupid.\n" + number1 + " + " + number2 + " should be " + (number1 + number2));
    System.out.print("What is " + number1 + " + " + number2 + "? ");
     
    count++;
     
    output += "\n" + number1 + "+" + number2 + "=" + answer + ((number1 + number2 == answer) ? " awesome" : " idiot");
    }
     
    long endTime = System.currentTimeMillis();
    long testTime = endTime - startTime;
    System.out.println("Correct count is " + correctCount + "\nTest time is " + testTime / 1000 + " seconds\n" + output);
    }
    }

    Like this? Sorry, I'm a newbie

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,139
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Multiple Computations in 1 file

    That is part of it. Now the code needs to be formatted.
    Nested statements need to be indented 3-4 spaces to make the code easier to read and understand.
    All statements should NOT start in the first column.

    Can you explain what problems you are having?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Multiple Computations in 1 file

    Quote Originally Posted by Norm View Post
    That is part of it. Now the code needs to be formatted.
    Nested statements need to be indented 3-4 spaces to make the code easier to read and understand.
    All statements should NOT start in the first column.

    Can you explain what problems you are having?
    Thanx so much Norm

    I would like to create a program which, when selecting one of four options, creates a math problem. After being asked 5 questions, the program would show all five problems and show how much time it took to complete the whole process.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,139
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Multiple Computations in 1 file

    I would like to create a program which ...
    Do you have any specific questions about the program?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 4
    Last Post: February 20th, 2013, 08:26 AM
  2. Populating multiple arrays with a File.
    By ccf in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 10th, 2012, 08:11 PM
  3. [SOLVED] Persisting multiple objects of the same type to a file.
    By mjr in forum Object Oriented Programming
    Replies: 6
    Last Post: August 15th, 2012, 07:25 PM
  4. multiple file uploading and downloading
    By priti in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 27th, 2012, 11:34 AM
  5. Reading from a file, multiple lines
    By MysticDeath in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: October 15th, 2009, 02:40 AM