Java Maths Calculation?

Thread Starter

Ben_C

Joined Oct 19, 2010
65
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>
Rich (BB code):
 import java.util.Scanner;
 public class Assignment1
{
 public static void main (String [] args)
 {
 Scanner input = new Scanner(System.in);
 System.out.print ("Please enter the frequency: ");
 int frequency = input.nextInt ();
 System.out.println (+ frequency);
 System.out.print ("Please enter the number of stages:");
 int stages = input.nextInt ();
  if (stages >=1)
  if (stages <=10)
   {
   System.out.println (+ stages);
  }
  else
  {
   System.out.println ("Enter a value between 1 and 10. PLease try again.");
  }
  
 }
}
I think the if and else statements are wrong these may need to be the do while loops Im not very confident with this :confused:
 

Thread Starter

Ben_C

Joined Oct 19, 2010
65
Okay, so ive finally figured out how to do it just one simple problem which seems complex to me haha... Heres my code..
Rich (BB code):
import java.util.Scanner;
public class Assignment1
{
public static void main (String [] args) {
 
 Scanner input = new Scanner(System.in);

 System.out.println ("Please enter the frequency: ");
 double f = input.nextDouble ();
 System.out.println ("Please enter the number of stages:");
 int n = input.nextInt();
 
       if (n > 1)
       if (n < 10)
do {
 double G = Math.pow(275/Math.sqrt((Math.pow(23,2))+(Math.pow(0.5*f,2))),n);
 System.out.printf ("The Voltage Gain is: %.2f", G);
 n = input.nextInt();
 }while (n > 1 || n < 10);
 
else {
 System.out.println ("Enter a value between 1 and 10. PLease try again.");
 n = input.nextInt();
    }
  }
}
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.
 

cheezewizz

Joined Apr 16, 2009
82
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...
Rich (BB code):
while(true)
{
      //read input
      if(input_is_valid)
            break;
}
 
Top