My new problem is that myMin is equalling the last distance before the numbers are equal instead of the actual minimum. e.g. say the first two numbers I enter are 1 and 2, and the next are 1 and 3, and then 1 and 1. It is saying my minimum is 2.0. This is what I'm supposed to get for the assignment.
Enter number 1: 9
Enter number 2: 1
Enter number 1: 7
Enter number 2: 2
Enter number 1: 4
Enter number 2: 4
The minimum distance is: 5.0.
Enter number 1: 20
Enter number 2: 3
Enter number 1: 23
Enter number 2: 23
5.0 + 17.0 = 22.0
MY CODE:
double myMin = Double.MAX_VALUE;
double Min1,Min2;
while ( !(num1==num2) ) {
pairsMin( num1, num2, myMin);
Min1 = pairsMin( num1, num2, myMin);
System.out.print("Enter number 1: ");
num1 = in.nextDouble();
System.out.print("Enter number 2: ");
num2 = in.nextDouble();
if (num1==num2) {
System.out.print("\nThe minimum distance is: " + Min1 + "\n\n");
myMin = Double.MAX_VALUE;
System.out.print("Enter number 1: ");
num1 = in.nextDouble();
System.out.print("Enter number 2: ");
num2 = in.nextDouble();
while ( !(num1==num2)) {
pairsMin( num1, num2, myMin);
Min2 = pairsMin(num1,num2,myMin);
System.out.print("Enter number 1: ");
num1 = in.nextDouble();
System.out.print("Enter number 2: ");
num2 = in.nextDouble();
if(num1==num2) {
double totMin = Min1+Min2;
System.out.print("\n" + Min1 + " + " + Min2 + " = " + totMin + "\n");
}
}
}
} // end while loop
} // end main method
public static double pairsMin( double num1, double num2, double myMin){
double dist = Math.abs(num1-num2);
if ( dist<myMin) { // if dist is smaller than the minimum, then dist will be the new minimum
myMin = dist;
}
return myMin;
}
}
Change the two lines
pairsMin( num1, num2, myMin);
to
myMin = pairsMin( num1, num2, myMin);
At the moment you are always comparing to Double.MAX_VALUE and not to the new minimum value.
Related
first question here. I have to use a previous assignment of calculating BMI, and reformat it to accept command line arguments as inputs for height and weight.
"Your program shall obtain the weight and the height via main(String[] args), i.e,, when you run your program you must do the following:
java MyProgramName 180 5 7
where MyProgramName is the name of your program, 180 is the weight in pounds, 5 is the feet and 7 is the inch values.
The program shall output the BMI value in the terminal window as it was before (item f below)."
I am confused on how to call the arguments into the code while performing operands on them.
Here is my original code:
'int weight;
int heightInInches;
int bmi;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter your weight in pounds: ");
weight = keyboard.nextInt();
System.out.print("Enter your height in inches: ");
heightInInches = keyboard.nextInt();
bmi = ((weight * 703)/(heightInInches * heightInInches));
System.out.println("Your height is " + heightInInches + " and your
weight is: " + weight + " pounds");
System.out.println("Your BMI is " + bmi);'
I have seen something like this for just adding two numbers, but am confused how to alter it to the BMI formula.
int sum = 0;
for (int i = 0; i < args.length; i++) {
sum = sum + Integer.parseInt(args[i]);
}
System.out.println("The sum of the arguments passed is " + sum);
Thanks
Your String[] args would look something like the s: ["180","5","7"]
So args[0] would be your weight.
args[1] would be the heightInFeet (multiply by 12 to get heightInInches)
args[2] would be the inches part of the height
So the code becomes:
int weight = Integer.parseInt(args[0]);
int heightInInches = Integer.parseInt(args[1])*12 + Integer.parseInt(args[2]);
bmi = ((weight * 703)/(heightInInches * heightInInches));
The task is to create a loan calculator based on the user input of min and max years of loan payments, loan amount, and min and max % rate with incremented value given by user as well for rate and number of years.
Desired output should look like this:
Principle: $275000.0
Years to repay: 10
Interest Monthly
Rate Payment
6.25 3087.7
6.75 3157.66
7.25 3228.53
Principle: $275000.0
Years to repay: 15
Interest Monthly
Rate Payment
6.25 2357.91
6.75 2433.5
7.25 2510.37
Principle: $275000.0
Years to repay: 20
Interest Monthly
Rate Payment
6.25 2010.05
6.75 2091.0
7.25 2173.53
Please help me fix errors. Thanks!
public static void main(String[] args){
Scanner console = new Scanner (System.in);
System.out.println("This program computes monthly " + "mortgage payments.");
System.out.print("Enter the loan amount: ");
double loan = console.nextDouble();
System.out.print("Enter the starting number of years to repay the loan: ");
int startingYears = console.nextInt();
System.out.print("Enter the ending number of years to repay the loan: ");
int endingYears = console.nextInt();
System.out.print("Enter the years increment between tables: ");
int incrementYears = console.nextInt();
System.out.print("Enter the starting loan yearly interest rate, %: ");
double startingRate = console.nextDouble();
System.out.print("Enter the ending loan yearly interest rate, %: ");
double endingRate = console.nextDouble();
System.out.print("Enter the increment interest rate, %: ");
double incrementRate = console.nextDouble();
System.out.println();
System.out.println("Principle: $" + (double) loan);
System.out.printf("Years to repay: %d\n", startingYears);
System.out.println("-------- -------");
double payment;
System.out.println("Rate\tPayment");
for (int j = startingYears; j <= endingYears; incrementYears++) {
for (double i = startingRate; i <= endingRate; incrementRate++){
int n = 12 * startingYears;
double c = startingRate / 12.0 / 100.0;
payment = loan * c * Math.pow(1 + c, n) / (Math.pow(1 +c, n) - 1);
System.out.println(i + " " + payment);
// System.out.println(round2(startingRate) + "\t" + round2(payment));
startingYears += incrementYears;
}
}
}
}
for (int j = startingYears; j <= endingYears; incrementYears++) {
for (double i = startingRate; i <= endingRate; incrementRate++) {
You are never incrementing the values of your counters i.e. i and j and hence the values of i and j will always be equal to the initialized values and these loops will run for infinite times.
Increment both the counters to be able to reach the termination condition i.e. j <= endingYears and i <= endingRate respectively.
Here is the code snippet:
System.out.println("Principle: $" + (double) loan);
for (int j = startingYears; j <= endingYears; j+=incrementYears) {
System.out.printf("Years to repay: %d\n", j);
System.out.println("-------- -------");
System.out.println("Rate\tPayment");
for (double i = startingRate; i <= endingRate; i+=incrementRate){
int n = 12 * j;
double c = i / 12.0 / 100.0;
double payment = loan * c * Math.pow(1 + c, n) / (Math.pow(1 + c, n) - 1);
System.out.println(i + "\t" + payment);
}
System.out.println();
}
Here is my code.
import java.util.Scanner;
import java.util.ArrayList;
public class SysQ {
public static void main(String[] args) {
Scanner keyboard = new Scanner (System.in);
System.out.print("Enter A: ");
double a = keyboard.nextDouble();
System.out.print("Enter B: ");
double b = keyboard.nextDouble();
System.out.print("Enter C: ");
double c = keyboard.nextDouble();
System.out.print("Enter X: ");
double x = keyboard.nextDouble();
System.out.print("Enter Y: ");
double y = keyboard.nextDouble();
System.out.print("Enter Z: ");
double z = keyboard.nextDouble();
double fixedequation2 = x * b;
double fixedequation3 = x * c;
double fixedequation5 = a * y;
double fixedequation6 = a * z;
double final1 = fixedequation2 - fixedequation5;
double final2 = fixedequation3 - fixedequation6;
double yanswer = final2/final1;
double x1 = c - (b * yanswer);
double xanswer = x1/a;
System.out.println("X is " + xanswer);
System.out.println("Y is " + yanswer);
So the code works for most numbers for a 2 x 2 equation, but when I put in the values 5,5,5,5,5,5 for a,b,c,x,y,z, I get "NaN" I just want to know does "NaN" mean infinitely many solutions or No Solution..
NaN means "not a number". You get that solution because you are doing
(25-25)/(25-25) = 0/0, the result of which is undefined, or "Not a number"
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have been trying to make a basic calculator that calculates the mean of 9 numbers. The problem is it always skips the last line.
My code:
/* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package avarage.calc;
import java.util.Scanner;
/**
*
* #author taine
*/
public class AvarageCalc {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner(System.in);
double num1;
double num2;
double num3;
double num4;
double num5;
double num6;
double num7;
double num8;
double num9;
double num10;
double ans;
System.out.print("Enter Number #1:");
num1 = input.nextDouble();
System.out.print("Enter Number #2:");
num2 = input.nextDouble();
System.out.print("Enter Number #3:");
num3 = input.nextDouble();
System.out.print("Enter Number #4:");
num4 = input.nextDouble();
System.out.print("Enter Number #5:");
num5 = input.nextDouble();
System.out.print("Enter Number #6:");
num6 = input.nextDouble();
System.out.print("Enter Number #7:");
num7 = input.nextDouble();
System.out.print("Enter Number #8:");
num8 = input.nextDouble();
System.out.print("Enter Number #9:");
num9 = input.nextDouble();
ans = num1 + num2 + num3 + num4 + num5 + num6 + num8 + num9;
System.out.println("The Average of the numbers you gave is:" + ans / 9);
}
}
When the program runs:
run:
Enter Number #1:20
Enter Number #2:20
Enter Number #3:20
Enter Number #4:20
Enter Number #5:20
Enter Number #6:20
Enter Number #7:20
Enter Number #8:20
Enter Number #9:20
The Average of the numbers you gave is:17.77777777777778
BUILD SUCCESSFUL (total time: 10 seconds)
Your average is incorrect because you are missing num7 in your sum
ans = num1 + num2 + num3 + num4 + num5 + num6 + num8 + num9;
should be
ans = num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9;
You have 10 variables, but you calculate average of 9. Is it what you want? Anyway, #the-tom has showed your mistake. You've forgot about num7 variable.
The less lines of code you have the less possibility to get an error.
Probably will be better to do something like that:
double ans = 0;
Scanner input = new Scanner(System.in);
int x = 9 //Or some other number
for(int i = 1; i <= x; i++){
System.out.print("Enter Number #" + i);
ans += input.nextDouble();
}
System.out.println("The Average of the numbers you gave is:" + ans / x);
Now if you need to calculate average of 20 elements, all you need is just set the value of x to 20.
This is probably a easy question for most of you but the answer has evaded me for the most part. I'm writing a program to sort three numbers from lowest to highest and in the command prompt the inputs must be all on one line. I have the program working but for whatever reason I cannot get the inputs to show on one line. Instead I get something like:
Please enter three numbers: 1
2
3
Sorted numbers are: 1, 2, 3
Where it should show
Please enter three numbers: 1 2 3
Sorted numbers are: 1, 2, 3
My code:
import java.util.Scanner;
public class Ch5PA1
{
public static void main(String[] args) {
// Declarations
Scanner input = new Scanner(System.in);
System.out.print("Enter three values: ");
double num1 = input.nextDouble();
double num2 = input.nextDouble();
double num3 = input.nextDouble();
displaySortedNumbers(num1, num2, num3);
}
/** Sort Numbers */
public static void displaySortedNumbers(double num1, double num2, double num3){
double highest = num1 > num2 && num1 > num3 ? num1 : num2 > num1 && num2 > num3 ? num2 : num3;
double lowest = num1 < num2 && num1 < num3 ? num1 : num2 < num1 && num2 < num3 ? num2 : num3;
double middle = num1 != highest && num1 != lowest ? num1 : num2 != highest && num2 != lowest ? num2 : num3;
System.out.println("The sorted numbers are " + lowest + " " + middle + " " + highest);
}
}
You can take input from user like provide 3 numbers in comma seperated or space seperated. And split the string into array.
You don't need to change your code. Just separate your doubles with whitespace. (Spaces)