My program so far:
import java.util.Scanner;
public class Temperature
{
public static void main(String[] args)
{
int count = 20;
double fahrenheit;
double celsius;
String input;
// Scanner object for keyboard input.
Scanner kb = new Scanner(System.in);
// Get the information.
System.out.print("Enter your starting temperature in Fahrenheit: ");
fahrenheit = kb.nextDouble();
// Display the results in a table.
System.out.println("Fahrenheit Celsius");
System.out.println("-----------------------");
for (fahrenheit = fahrenheit; fahrenheit <= count; fahrenheit += 5)
{
// Calculate
celsius = (fahrenheit - 32)*(5.0 / 9.0);
System.out.printf("%,.2f \t\t\t %,.2f\n", fahrenheit, celsius);
}
}
}
Now what I need it do is to produce a table of 20 temperature conversions from Fahrenheit to Celsius.
If the User entered 0, here is a sample of the first 3 lines of what your output could look like:
FAHRENHEIT: 0 CELSIUS: -17.78
FAHRENHEIT: 5 CELSIUS: -15.00
FAHRENHEIT: 10 CELSIUS: -12.22
etc...
The problem is that it won't loop the correct amount of times if the input is more than 20.
Here is an example of what would happen if the user input 5:
Output:
Enter your starting temperature in Fahrenheit: 5
Fahrenheit Celsius
5.00 -15.00
10.00 -12.22
15.00 -9.44
20.00 -6.67
And then that is where the output ends.
Does anyone know how I can make it so that the program will display 20 F to C equivalents no matter what the user inputs?
Your for loop is all messed up: for (fahrenheit = fahrenheit; fahrenheit <= count; fahrenheit += 5) it is comparing tempratures to a count (fahrenheit <= count).
If you always want 20 steps then something like:
for (int i = 0; i < count; i++) {
...
fahrenheit += 5;
}
would do it.
Option 1, add a counter and use it.
for (int i = 0; i < count; i++, fahrenheit += 5)
{
// Calculate
celsius = (fahrenheit - 32)*(5.0 / 9.0);
System.out.printf("%,.2f \t\t\t %,.2f\n", fahrenheit, celsius);
}
Option 2, decrement count (and test against zero) while you loop.
for (; count > 0; count--, fahrenheit += 5)
{
// Calculate
celsius = (fahrenheit - 32)*(5.0 / 9.0);
System.out.printf("%,.2f \t\t\t %,.2f\n", fahrenheit, celsius);
}
What you need to do is keep a separate index that starts at 0 and goes up to 20. That means that there would be 2 variables being changed each time, index and fahrenheit, and the for-loop would look as follows:
int index = 0
for (fahrenheit = fahrenheit; index < count; fahrenheit += 5) {
...
index++;
}
Related
I have a Printf formating question. I am to print only 10 numbers, before going to the next line and printing 10 more numbers and so on. with the end goal being like a table, with all the columns lining up and being aligned to the right. I am using a while statement as well. I have tried a few different things that I have found in my research, with no success. Would I use a different print statement for it other than Printf? Such as Print, or PrintLn? Also thought about using an If statement as well. Any help would be greatly appreciated! Thank you.
System.out.printf("Please enter a maximun integer value: ");
Scanner scan = new Scanner(System.in);
double n = scan.nextDouble();
System.out.printf("The number you entered was: %.0f \n", n); // Just to check if user input is correct
double startNum = 0;
double sqrt = startNum;
System.out.printf("Squares less than %.0f are: ", n);
while ( sqrt < n) {
sqrt = Math.pow(startNum, 2);
System.out.printf("%6.0f", sqrt);
startNum ++;
}
Using a MOD condition, You can ensure 10 output per line.
import java.util.Scanner;
class Test {
public static void main(String[] args) {
System.out.printf("Please enter a maximun integer value: ");
Scanner scan = new Scanner(System.in);
double n = scan.nextDouble();
System.out.printf("The number you entered was: %.0f \n", n); // Just to check if user input is correct
double startNum = 0;
double sqrt = startNum;
System.out.printf("Squares less than %.0f are: ", n);
while (sqrt < n) {
sqrt = Math.pow(startNum, 2);
if(startNum != 0 && startNum % 10 == 0) {
System.out.println();
}
System.out.printf("%6.0f", sqrt);
startNum++;
}
}
}
Output -
Please enter a maximun integer value: 150
The number you entered was: 150
Squares less than 150 are: 0 1 4 9 16 25 36 49 64 81
121 144 169
while ( sqrt < n) {
sqrt = Math.pow(startNum, 2);
System.out.printf("%6.0f", sqrt);
startNum ++;
if(startNum%10==0){
System.out.printf("/n");
}
}
I have an assignment where I am supposed to determine whether the average of three values is 'above average' or 'below average'. For some reason whatever is input will always be above average as the result. Here is my code below, thank you for any help!
import java.util.Scanner;
class Lesson_12_Activity_One {
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter three values");
double x = scan.nextDouble();
double y = scan.nextDouble();
double z = scan.nextDouble();
double t = (double)Math.round(100*((x+y+z)/3));
System.out.print("The average is " + (t/100));
if(t >= 89.5)
System.out.print(" ABOVE AVERAGE");
else
System.out.print(" BELOW AVERAGE");
}
}
The average is t/100 but in your condition you test if t > 89.5 (which is always true since t is the average multiplied by 100).
Just remove both the multiplication by 100 and the division by 100. They don't seem necessary.
double t = Math.round((x+y+z)/3);
System.out.print("The average is " + t);
if(t >= 89.5)
System.out.print(" ABOVE AVERAGE");
else
System.out.print(" BELOW AVERAGE");
}
if(t/100 >= 89.5)
System.out.print(" ABOVE AVERAGE");
else
System.out.print(" BELOW AVERAGE");
by the way why are you multiplying and then dividing by 100?
I'm gonna guess that you're mixing up perunages and percentages. That means, at one point in your program you use 0.5 and in the other 50, both as 50%.
double t = (double)Math.round(100*((x+y+z)/3));
System.out.print("The average is " + (t/100));
With x, y and z all as 50, this will output 50. t = 100 * (50 + 50 + 50)/3 = 5000, the output is (t/100) = 50.
if(t >= 89.5) however tests with t = 5000.
To solve this, go down one of two paths.
Replace all percentages for perunages. This means inputting numbers from 0 to 1.
To do this, do the following:
change your t-initialization for double t = (double)Math.round(1000*((x+y+z)/3)) / 1000 This will make T be in between 0 and 1 with 3 digits precision.
Replace your if with if (t >= 0.895)
Replace all perunages with percentages. This means inputting numbers from 0 to 100.
To do this, remove the 100* from your double t = (double)Math.round(100*((x+y+z)/3));, and the /100 from the output message.
This is a program to calculate average grades and I cant figure out whats wrong with my code. It is returning the wrong answer.
Editing post to remove personal information.
:
/**
* This program will calculate grade average of user input
* Date: 10/2/2015
*/
import java.util.Scanner;
public class GradeAVG {
public static void main(String[] args) {
avgGrade();
}
public static void avgGrade() {
Scanner keyboard = new Scanner (System.in);
double count = 0;
double avgGrade = 0 ;
double grade;
double total = 0;
System.out.println("Please input the grade");
grade = keyboard.nextDouble();
while(true){
System.out.println("Please input the grade");
grade= keyboard.nextDouble();
count = count + 1;
if (grade < 0) break;
total += grade;
avgGrade = total/count;
}
System.out.println ("Sum is " +total);
System.out.printf("The average of the %.0f grades are %.2f " ,count ,avgGrade);
}
}
Output:
Please input the grade
100
Please input the grade
50
Please input the grade
-9
Sum is 50.0
The average of the 2 grades are 50.00
Sum should have been 150 and average 75.
The problem is that you are reading a grade from the user before the while loop begins and you are ignoring this value afterwards.
You should remove those 2 lines and things will work as expected. I commented those lines in the snippet below to explicitely show you the problem.
public static void avgGrade() {
Scanner keyboard = new Scanner(System.in);
double count = 0;
double avgGrade = 0;
double grade;
double total = 0;
// System.out.println("Please input the grade");
// grade = keyboard.nextDouble();
while(true){
System.out.println("Please input the grade");
grade = keyboard.nextDouble();
count = count + 1;
if (grade < 0) break;
total += grade;
avgGrade = total/count;
}
System.out.println ("Sum is " +total);
System.out.printf("The average of the %.0f grades are %.2f " ,count ,avgGrade);
}
As a side note, you should always try to minimize the scope of each of your variable. Here, the grade variable is only use inside the while loop so you can directly write double grade = keyboard.nextDouble(); and remove the declaration in the beginning of the method.
You are not adding the first grade which you are accepting outside of the while loop to the total.
Also there is no point in incrementing the count, if the grade is not acceptable, so increment your count only after the grade check.
You can rewrite your while block something like
while (true) {
System.out.println("Please input the grade");
grade = keyboard.nextDouble();
if (grade < 0)
break;
count = count + 1;
total += grade;
}
avgGrade = total / count;
System.out.println("Sum is " + total);
System.out.printf("The average of the %.0f grades are %.2f ", count,
avgGrade);
}
Thanks guys. Here is the final code
/**
* This program will calculate grade average of user input
*
* Date: 10/2/2015
*/
import java.util.Scanner;
public class GradeAVG {
public static void main(String[] args) {
avgGrade()
}
public static void avgGrade()
{
Scanner keyboard = new Scanner (System.in);
double count = 0;
double avgGrade = 0 ;
double total = 0;
while(true){
System.out.println("Please input the grade");
double grade= keyboard.nextDouble();
if (grade < 0) break;
count = count + 1;
total += grade;
avgGrade = total/count;
}
System.out.println ("Sum is " +total);
System.out.printf("The average of the %.0f grades are %.2f " ,count ,avgGrade);
}
}
Several problems:
First, you do two assignments into grade before first reading from it (first one before the while loop and second inside the while loop), so the first input will be ignored entirely
Second, you increment the count variable before checking whether to break the while loop, so you end up with count higher by 1 than should be
Third, the average is computed inside the loop and will not be recalculated in the last (partial) iteration
The program will go like this:
input: 100, count: 0, total: 0, avg: 0
input was ignored
input: 50, count: 1, total: 50, avg: 50
input: -9, count: 2, total: 50, avg: 50
loop exit, but incremented count before; did not recalculate avg
I need to create a program that will output a formatted two column list converting Celsius to Fahrenheit ending at the temperature 40 degrees from the starting temperature, which is entered by the user. It's supposed to look like this: (except it counts up from whatever the user enters as Celsius)
I've been working at this for hours and I have no idea how to fix it. Not only is the Celsius starting at 1 (which I think is that (cel=1;) bit), but I have no idea why Fahrenheit is not calculating correctly.
Here's my current source:
import java.util.Scanner;
public class TempTable {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
float cel;
double column;
double Fahrenheit;
final double C_2_F = (9.0 / 5.0);
System.out.println("Enter your city's temperature in Celsius.");
cel = kb.nextFloat();
System.out.println("Here is the conversion from " + cel);
System.out.printf("%2s%12s%n", "Celcius", "Fahrenheit");
for (cel = 1; cel <= 10; cel++) {
column = cel;
for (Fahrenheit = 1; Fahrenheit <= 10; Fahrenheit++) {
Fahrenheit = cel * C_2_F + 32;
System.out.printf("%2.0f%12.0f%n", cel, Fahrenheit);
}
}
kb.close();
}
}
Fix your loop to not reset 'Fahrenheit' each time through.
for ( Fahrenheit = 1; Fahrenheit <=10; Fahrenheit++) {
Fahrenheit = cel * C_2_F + 32;
System.out.printf("%2.0f%12.0f%n",cel,Fahrenheit);
}
To
for ( Fahrenheit = 1; Fahrenheit <=10; Fahrenheit++) {
double f = cel * C_2_F + 32;
System.out.printf("%2.0f%12.0f%n",cel,f);
}
Would be one way. Also rename 'Fahrenheit' to 'fahrenheit'
Actually, that's only one issue. That's just going to print out the same value 10 times since 'cel' and 'C_2_F' don't change during the loop...
I need to convert Fahrenheit to Celsius in a Java program with methods, a for loop statement, and no user input. It is supposed to display Fahrenheit temperatures 0 through 20 and it's Celsius conversions. Any solutions?
import java.util.Scanner;
public class celsiusTempTable
{
public static void main(String[] args)
{
System.out.println("Fahrenheit to Celsius Conversion Table");
double tempC = celsiusConversion(tempC);
int tempF = fahrenheit(tempF);
displayData(tempF, tempC);
}
public static int fahrenheit(int F)
{
for(F = 0; F <= 20; F++)
{
return F;
}
}
public static double celsiusConversion(double C)
{
Scanner input = new Scanner(System.in);
for(int F = 0; F <= 20; F++)
{
C = (5.0/9.0) * (F - 32);
return C;
}
}
public static void displayData(int F, double C)
{
for(F = 0; F <= 20; F++)
{
System.out.println("\nThe temperature in Fahrenheit is: " + F);
System.out.println("The temperature in Celsius is: " + C);
}
}
}
I'll only give hints.
Without user input. So forget about using a Scanner and System.in.
You need to understand what method arguments and method return values are. Arguments are usually the inputs of a method. And the return value is the output of the method. You're being asked to translate a temperature in fahrenheit degrees to a temperature in celsius degrees. This is a perfect situation where a method is useful. The input of the translation method is thus a unique integer value (the temperature in fahrenheit degrees), and the output is another single integer value (the temperature in celsius degrees).
You must do that 21 times. Once with 0 as input, once with 1 as input, etc. until 20. This means you need a loop, and that at each iteration, you will translate the current temperature (0, 1, 2, etc.) into celsius degrees by calling the translation method, and print the result. Read your text book about for loops. This part should be in the main method.
A quick search and have found the following:
Error: celsius cannot be resolved to a variable
Java Fahrenheit to Celsius loop (with methods)
Fahrenheit to Celsius conversion
Celsius/Fahrenheit conversion error