Java Skipping last variable [closed] - java

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.

Related

I think my program is giving an inaccurate result for Riemann Sums

this is my first post here!
So, as an extra credit project for my Calculus course, the professor offered us an opportunity to write a simple program that calculates the area under a user specified curve. I realize this isn't the best way to implement this, but he say's that's fine, but I think this is giving me the wrong answer. Could anyone help?
import java.util.*;
public class RiemannSum2 {
public static void main(String args []) {
System.out.println("This is a Riemann Sum Calculator. This calculator accepts polynomials in the form of a(x)^ex + b(x)^ex2 + c, where c is a constant.");
System.out.print("Enter the first coeffecient of the polynomial: ");
Scanner sc = new Scanner(System.in);
int firstCoe = sc.nextInt();
System.out.print("Enter the exponent of the first term: ");
int firstExp = sc.nextInt();
System.out.print("Enter the second coeffecient of the polynomial: ");
int secondCoe = sc.nextInt();
System.out.print("Enter the exponent of the second term: ");
int secondExp = sc.nextInt();
System.out.print("Enter the third term of the polynomial: ");
int thirdTerm = sc.nextInt();
System.out.print("Enter the x value that you want to start the Riemann Sum: ");
int startX = sc.nextInt();
System.out.print("Enter the x value to stop the Riemann Sum: ");
int endX = sc.nextInt();
String poly = (firstCoe+"x^"+firstExp+"+"+secondCoe+"x^"+secondExp+"+"+thirdTerm);
System.out.println("Your polynomial is: "+poly);
System.out.print("Enter the number of rectangles you want: ");
int rectangles = sc.nextInt();
double numerator = (endX-startX);
double rectanglesD = (double)rectangles;
double constantWidth = numerator/rectanglesD;
System.out.println("This is the constant width: " + constantWidth);
double totalSum = 0;
//System.out.println(totalSum);
for(int i = 0; i < rectangles ; i++) {
totalSum = totalSum+((Math.pow((firstCoe * (i/constantWidth)), firstExp)) + (Math.pow((secondCoe * (i/constantWidth)), secondExp))+thirdTerm);
}
totalSum = totalSum*constantWidth;
System.out.println("The Riemann Sum of your polynomial is roughly equivalent to: "+ totalSum);
}
}
You use (i/constantWidth) to calculate the argument of your function (x). However, it should be
double x = startX + i * constantWidth;
Furthermore, your coefficients should be outside of the pow function. Otherwise, they will get exponentiated too. Removing some of the superfluous parentheses makes the formula a lot easier to read. Like this:
double x = startX + i * constantWidth;
totalSum = totalSum
+ firstCoe * Math.pow(x, firstExp)
+ secondCoe * Math.pow(x, secondExp)
+ thirdTerm;
Unrelated to the code: Since you have a simple polynomial, you can calculate the antiderivative analytically and simply evaluate that function instead.

Calculate BMI using command line arguments inputs

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));

Method taking newest information instead of just updating

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.

What is wrong with the formula in my code? [duplicate]

This question already has answers here:
Why double width = 50/110000; the output is 0.000000000000000?
(3 answers)
Closed 9 years ago.
We've been set a task to calculate user input. My code compiles, however when I test it my output for the refund is always 0 :(
Users are meant to enter their distance and self contribution when prompted, but what exactly is wrong with my formula for refund? Can anybody shed some light on this for me?
import java.util.*;
public final class UserCalc {`
public static void main(String[] args) {
Scanner scanner = new Scanner(System. in );`
System.out.print("Please enter the distance ");
int distance = scanner.nextInt();
System.out.print("Please enter the percentage of self contribution ");
int selfcon = scanner.nextInt();
int trainprice = (75 + (2 / 10)) * distance;
int carprice = (26 + (7 / 10)) * distance;
int refund = (Math.min(trainprice, carprice)) * ((100 - selfcon) / 100);
System.out.print("You get a refund of " + refund + " pounds");
int age = scanner.nextInt();
}
}
Because result of ((100 - selfcon)/100) is always zero and you works with integers you should use float or double.
You are using integer arithmetic rather than floating point arithmetic, so expressions like 2/10 evaluate to 0.
double distance = scanner.nextDouble();
System.out.print("Please enter the percentage of self contribution ");
double selfcon = scanner.nextDouble();
double trainprice = (75 + (2 / 10)) * distance;
double carprice = (26 + (7 / 10)) * distance;
doulbe refund = (Math.min(trainprice, carprice)) * ((100 - selfcon) / 100);
System.out.print("You get a refund of " + refund + " pounds");
int age = scanner.nextInt();

How do you put multiple inputs on one line?

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)

Categories