Multiple Computations in 1 file

• April 16th, 2013, 02:15 PM
Neo845
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 2 for Subtraction ");
System.out.println("Enter 3 for Multiplication ");
System.out.println("Enter 4 for Division");

System.out.print("What is " + number1 + " + " + number2 + "? ");

if (number1 + number2 == answer) {
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);
}
}

• April 16th, 2013, 02:25 PM
Norm
Re: Multiple Computations in 1 file
Please explain what problems you are having.

[code=java]
[/code]
to get highlighting and preserve formatting.
• April 16th, 2013, 02:49 PM
Neo845
Re: Multiple Computations in 1 file
Quote:

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

[code=java]
[/code]
to get highlighting and preserve formatting.

Sorry

Code java:

```  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
• April 16th, 2013, 03:20 PM
Norm
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?
• April 17th, 2013, 11:36 AM
Neo845
Re: Multiple Computations in 1 file
Quote:

Originally Posted by Norm
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.
• April 17th, 2013, 11:38 AM
Norm
Re: Multiple Computations in 1 file
Quote:

I would like to create a program which ...
Do you have any specific questions about the program?