# Java Maths Calculation?

Discussion in 'Programmer's Corner' started by Ben_C, Feb 29, 2012.

1. ### Ben_C Thread Starter Active Member

Oct 19, 2010
65
4
Hi ive been given the task of programming code that solves a calculation of variable inputs from the Scanner, from f and n. I've got upto the stage of inputting the variables and it compiles in cmd but thats it... I dont know have a clue how to expand it and put the values of f and n into the equation.

gain = [275/(23^2+(0.5f)^2)^0.5]^n

I've to use a do-while loop whilst gain and f are to be declared as a double whilst n is an int. The answer should be printed out to 2 decimal places and the program should only break out of the loop when a valid value of n has been entered. n can only be from 1 to 10.

Example:
Enter a frequency:
100
Enter the number of stages:
0
Enter a value between 1 and 10 please try again.
Enter the number of stages:
6
gain = 15563.18

Heres where I've got up to>
Code ( (Unknown Language)):
1.  import java.util.Scanner;
2.  public class Assignment1
3. {
4.  public static void main (String [] args)
5.  {
6.  Scanner input = new Scanner(System.in);
7.  System.out.print ("Please enter the frequency: ");
8.  int frequency = input.nextInt ();
9.  System.out.println (+ frequency);
10.  System.out.print ("Please enter the number of stages:");
11.  int stages = input.nextInt ();
12.   if (stages >=1)
13.   if (stages <=10)
14.    {
15.    System.out.println (+ stages);
16.   }
17.   else
18.   {
19.    System.out.println ("Enter a value between 1 and 10. PLease try again.");
20.   }
21.
22.  }
23. }
I think the if and else statements are wrong these may need to be the do while loops Im not very confident with this

2. ### nigelwright7557 AAC Fanatic!

May 10, 2008
528
75
//this is better

if (stages>=1 && stages <=10)

3. ### Ben_C Thread Starter Active Member

Oct 19, 2010
65
4
Okay, so ive finally figured out how to do it just one simple problem which seems complex to me haha... Heres my code..
Code ( (Unknown Language)):
1.
2. import java.util.Scanner;
3. public class Assignment1
4. {
5. public static void main (String [] args) {
6.
7.  Scanner input = new Scanner(System.in);
8.
9.  System.out.println ("Please enter the frequency: ");
10.  double f = input.nextDouble ();
11.  System.out.println ("Please enter the number of stages:");
12.  int n = input.nextInt();
13.
14.        if (n > 1)
15.        if (n < 10)
16. do {
17.  double G = Math.pow(275/Math.sqrt((Math.pow(23,2))+(Math.pow(0.5*f,2))),n);
18.  System.out.printf ("The Voltage Gain is: %.2f", G);
19.  n = input.nextInt();
20.  }while (n > 1 || n < 10);
21.
22. else {
23.  System.out.println ("Enter a value between 1 and 10. PLease try again.");
24.  n = input.nextInt();
25.     }
26.   }
27. }
28.
This code does what I want it to do when I put a value between 1 and 10 it calculates it. Above 1 and 10 it give the error message
"Enter a value between 1 and 10. PLease try again."
But say I put 4 or any digit after this message it just ends the program.. I should be able to enter a 4 after the please try again message and it calculate it. I need the Do { somewhere where the program executes all of the program. Ive spent like a whole day on messing around with the code but nothing.. Any help will be massively appreciated.

4. ### cheezewizz Active Member

Apr 16, 2009
82
10
In that case you need to put the code for reading the inputs inside an infinite loop which you only break out of once valid input has been received...
Code ( (Unknown Language)):
1. while(true)
2. {