It may be rather simple but I am having issues returning a value for "circum" here is my code.
double circRad = 0;
double circum = 0;
if(userOption == 2){
circRad = myRad();
System.out.println("The Radius of the circle is " + circRad + " and it perimeter is "+ circum);
public static double myRad(){
int i= 1;
double circRad = 0;
Scanner myInput=new Scanner(System.in);
switch(i)
{
case 1:
System.out.print("Please enter a positive Radius for the circle ");
circRad = myInput.nextDouble();
while(circRad <0){
System.out.print("Please enter a POSITIVE Radius for the circle ");
circRad = myInput.nextDouble();
}
++i;
}
return circRad;
}
public static double myRad(double circRad){
double circum = 0;
circum = 2* Math.PI * circRad;
return circum;
}
Perhaps you are missing the following:
if(userOption == 2){
circRad = myRad();
circum = myRad(circRad); // <-- missing setting local circum value
System.out.println("The Radius of the circle is " + circRad + " and it perimeter is "+ circum);
}
Only thing I could guess about the code with the way it is presented is that circRad and circum are global variables, therefore when you call the first myRad() only circRad changes. However, circum remains 0.
If you could sent the entire code, it'll make everything easier.
Related
The first part of the code is working, the program takes inputs as height and weight as displays an output message. When I press n I can go to the second part and take inputs for height in inches and the weight in pounds. As soon as I take them the program outputs the resulting bmi is: Nan. and the variable bmi might not have been initialized. i want to convert the height and weight from inches and pounds to meters and then show the resulting bmi This is my code:
package uly14th;
import java.util.Scanner;
public class BmiCalculator {
private static float BMI2;
public static void main(String[] args) {
char Y;
char y;
char Q;
char n;
char N;
float heightInMeters, weightInKilograms, BMI;
float heightInInches, weightInpounds;
float heightInMeters2 = 0, weightInKilograms2 = 0;
Scanner input = new Scanner(System.in);
System.out.println("Please state whether you are going to use kilograms & meters or, inches & pounds");
System.out.println("If you want to use the former please press Y or if you want to use the latter please press N");
Q = input.next().charAt(0);
if(Q == 'y' || Q == 'Y' ) {
System.out.println("Please enter the height in meters: ");
heightInMeters = input.nextFloat();
System.out.println(" and weight in kilograms: ");
weightInKilograms = input.nextFloat();
BMI = weightInKilograms / (heightInMeters * heightInMeters);
System.out.println("The resulting BMI of the person is: " +BMI);
if (BMI < 18.5){
System.out.println("The personis underweight");
}
else if ((BMI<=25.0) && (BMI >= 18.5)){
System.out.println("The person is normal");
}
else if ((BMI >= 25.0) && (BMI <=30.0 )){
System.out.println("The personis obese");
}
else if(BMI>=30.0) {
System.out.println("The person is obese and should exercise");
}
}
if ((Q == 'n') || (Q == 'N')){
System.out.println("Please enter the height in inches: ");
heightInInches = input.nextFloat();
System.out.println("Please enter the weight in pounds: ");
weightInpounds = input.nextFloat();
heightInInches = 0.0254f * heightInMeters2;
weightInpounds = 0.453592f * weightInKilograms2;
BMI = weightInKilograms / (heightInMeters * heightInMeters);
System.out.println("The resulting BMI of the person is: " +BMI);
if (BMI < 18.5){
System.out.println("The personis underweight");
}
else if ((BMI<=25.0) && (BMI >= 18.5)){
System.out.println("The person is normal");
}
else if ((BMI >= 25.0) && (BMI <=30.0 )){
System.out.println("The personis obese");
}
else if(BMI>=30.0) {
System.out.println("The person is obese and should exercise");
}
}
else {
System.out.println("Please make sure you run the program again and then only use the characters n,N,y,Y. ");
}
}
}
This is the error message: Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - variable BMI might not have been initialized
at uly14th.BmiCalculator.main(BmiCalculator.java:82)
At the row (line 82)
BMI = weightInKilograms / (heightInMeters * heightInMeters);
you use the variables weightInKilograms and heightInMeters which has never been initialized, hence your error.
I suspect you want to do the conversion to meters and kilograms from inches and pounds before calculating the BMI. You can do this by simply putting the following lines before the BMI calculation
heightInMeters = 0.0254f * heightInInches;
weightInKilograms = 0.453592f * weightInPounds;
BMI = weightInKilograms / (heightInMeters * heightInMeters); //Now you can calculate the BMI using your variables since they have been assigned a value and is therefore initialized
heightInMeters = 0.0254f * heightInInches ;
weightInKilograms = 0.453592f * weightInpounds ;
BMI = weightInKilograms / (heightInMeters * heightInMeters);
should do the trick (... in case the constants are correct)
import java.util.Scanner;
public class Tykinkuula {
public static void main(String[] args) {
Scanner lukija = new Scanner(System.in);
double highest = 0;
int seconds = 0;
double height = 0;
System.out.println("Syötä tykinkuulan lähtönopeus: ");
double startspeed = Double.parseDouble(lukija.nextLine());
System.out.println("Syötä painovoima: ");
double gravity = Double.parseDouble(lukija.nextLine());
System.out.println(seconds + "\t" + height + "\t" + startspeed);
while (true) {
if (height < 0.0) {
break;
}
height = height + startspeed;
seconds = seconds + 1;
startspeed = startspeed - gravity;
if (height > highest) {
highest = height;
}
System.out.println(seconds + "\t" + height + "\t" + startspeed);
}
System.out.println("");
System.out.println("Tykinkuulan suurin korkeus oli " + highest);
}
This is a code for a calculator that gives me the speed and height of a canonball every second. You input a desired starting speed and gravity, and then run the code.
When I run this code, it gives me 3 columns, seconds, height and speed.
The loop is supposed to end before the canonball hits the ground, so basically before height = 0. But when I run the code it gives me a negative height, but I need the program to only show results of when the ball is in the air.
So, how do I remove the negative result?
Simply move
if (height < 0.0) {
break;
}
after the calculation. Since you only check once it is below 0, you need to check after calculating the next step. Currently you check at the start of the loop, where height still has the value from the previous iteration (0), while the next calculation outputs a negative number
I am making an algebra quiz in Java, and I want to check if the users answer is the same as the answer of the algebra equation. This is what i got already, the checking is towards the bottom. The first if statement is checking whether the 2nd number is positive or negative and calculates it. And the second if statement is trying to see whether the user answer is the same as the question answer. The problem is that Int answer cant be transferred over to the other if statement. Is there a way I can get around this? It is using an algebra equation like this: 1x + 2 = 5.
int numRight = 0;
Scanner input = new Scanner(System.in);
System.out.println("Welcome to your algebra test!");
System.out.println("What is your name?");
String name = input.nextLine();
System.out.println("Ok, " + name + ", how many questions do you want on your test?");
int numQuestions = input.nextInt();
for(int x = 0; x <= numQuestions; x++){
int coefficient = 7;
int num1 = 3;
int num2 = 23;
while((num2 - num1) % coefficient != 0){
coefficient = (int) (Math.random()*8)+2;
num1 = (int) (Math.random()*19)-9;
num2 = (int) (Math.random()*90)+10;
}
if(num1 > 0){
int part1pos = num2 - num1;
int answer = (coefficient / part1pos);
} else if(num1 < 0){
int part1neg = num1 + num2;
int answer = (coefficient / part1neg);
}
System.out.println(coefficient + "x + " + num1 + " = " + num2);
int userAnswer = input.nextInt();
if(userAnswer == answer){
System.out.println("The Answer is correct!");
numRight++;
} else{
System.out.println("The answer is wrong!");
}
}
The problem is that your answer integer is declared within a different scope from your boolean statements that check for answer correctness. You should declare and initialize your answer as 0 outside of that boolean statement, around where you declare your coefficient, num1, and num2.
Desired:
The first coordinate is (input1, input2).
What I Get:
The first coordinate is (input1
, input2
).
The code I used:
Scanner Narwhal = new Scanner(System.in);
System.out.print("The first coordinate value is (");
double x = Narwhal.nextDouble();
System.out.print(", ");
double y = Narwhal.nextDouble();
System.out.print(").");
Thanks!
Print it all out at once using System.out.printf. This assumes you've moved the nextDouble calls before your print statement.
System.out.printf("The first coordinate value is (%0.1f, %0.1f).", x, y);
First you need to get the values from a user's input and after that you should print. You are mixing input and output.
You need something like this:
Scanner Narwhal = new Scanner(System.in);
double x = Narwhal.nextDouble();
double y = Narwhal.nextDouble();
System.out.print("The first coordinate value is (" + x);
System.out.print(", " + y);
System.out.print(").");
You did not print the variables:
System.out.print("The first coordinate value is (");
double x = Narwhal.nextDouble();
System.out.print(x + ", ");
double y = Narwhal.nextDouble();
System.out.print(y + ").");
I need to print the circumference with Math.random() * Math.Pi; but i'm doing something wrong or missing something. Each random generated number equals the radius of the circle. My idea was to calculate Pi in the getRandomNumberInRange method but when I do, I get error:
Bad operand for type double
import java.util.Random;
import java.util.Scanner;
final static double PI = 3.141592564;
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
//ask the player to enter a number less than or equal to 18 and higher to 9.
System.out.println(" Please enter a number less than or equal to 18 and above 9: ");
int random = sc.nextInt ();
//send error message if bad input
if (random < 9 || random > 18) {
System.out.println(" Error. Unauthorized entry . You need to enter a number less than or equal to 18 and above 9 ");
} else
//If the answer is yes , generate nine different random numbers from 0.
for (int i = 0; i < 9; i++) {
double surface = PI * (random * 2);
System.out.println(getRandomNumberInRange(9, 18) + " : " + " The circumference is : " + surface );
}}
The method called:
private static int getRandomNumberInRange(int min, int max) {
Random r = new Random();
return r.nextInt((max - min) + 1) + min;
}
You call getRandomNumberInRange() in the for loop, but don't assign it to anything, or use it.
This is probably closer to what you want:
for (int i = 0; i < 9; i++) {
int r2 = getRandomNumberInRange(9, 18);
double surface = PI * (r2 * 2);
System.out.println(r2 + " : " + " The circumference is : " + surface);
}