最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Return values beside each other - Stack Overflow

programmeradmin4浏览0评论

I've been assigned a temperature conversion assignment that requires values to be printed next to each other, using two static methods and a single System.out.print statement. For reference, I've added an image of the intended result.

This is everything I've written so far:

package main;

public class TempConversion {
    static double cel;
    static double fah;

    public static void convCel(double cel) {
        String verticle;
        double temp;
        cel = -40;
        verticle = "|";
        System.out.println("\tTemperature \n\t (degrees) ");
        System.out.print("   F \t\t    C");
        System.out.println();
        for (cel = -40; cel <= 455; cel += 5) {
            if (cel <= 455) {
                System.out.printf("%7.3f ", cel);
                if (cel <= 455) {
                    temp = (cel - 32) * 5 / 9;
                    System.out.printf("\t %7.3f  ", temp);
                    System.out.print(verticle);
                }
            }
            System.out.println();
        }
    }

    public static void convFah(double fah) {
        double temp;
        fah = -40;
        System.out.println("\tTemperature \n\t (degrees) ");
        System.out.print("   C \t\t    F");
        System.out.println();
        for (fah = -40; fah <= 455; fah += 5) {
            if (fah <= 455) {
                System.out.printf("%7.3f ", fah);
                if (fah <= 455) {
                    temp = (fah * 9 / 5) + 32;
                    System.out.printf("\t %7.3f  ", temp);
                }
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        System.out.print("      Temperature Conversion Table \n");
        System.out.println();
        convCel(cel);
        convFah(fah);
    }
}

The end result prints one loop after the other and I can't seem to find any other way that doesn't end in errors.

I'm trying to achieve the intended result as displayed in the image above. So far, I'm able to print the values, just not next to each other.

I've been assigned a temperature conversion assignment that requires values to be printed next to each other, using two static methods and a single System.out.print statement. For reference, I've added an image of the intended result.

This is everything I've written so far:

package main;

public class TempConversion {
    static double cel;
    static double fah;

    public static void convCel(double cel) {
        String verticle;
        double temp;
        cel = -40;
        verticle = "|";
        System.out.println("\tTemperature \n\t (degrees) ");
        System.out.print("   F \t\t    C");
        System.out.println();
        for (cel = -40; cel <= 455; cel += 5) {
            if (cel <= 455) {
                System.out.printf("%7.3f ", cel);
                if (cel <= 455) {
                    temp = (cel - 32) * 5 / 9;
                    System.out.printf("\t %7.3f  ", temp);
                    System.out.print(verticle);
                }
            }
            System.out.println();
        }
    }

    public static void convFah(double fah) {
        double temp;
        fah = -40;
        System.out.println("\tTemperature \n\t (degrees) ");
        System.out.print("   C \t\t    F");
        System.out.println();
        for (fah = -40; fah <= 455; fah += 5) {
            if (fah <= 455) {
                System.out.printf("%7.3f ", fah);
                if (fah <= 455) {
                    temp = (fah * 9 / 5) + 32;
                    System.out.printf("\t %7.3f  ", temp);
                }
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        System.out.print("      Temperature Conversion Table \n");
        System.out.println();
        convCel(cel);
        convFah(fah);
    }
}

The end result prints one loop after the other and I can't seem to find any other way that doesn't end in errors.

I'm trying to achieve the intended result as displayed in the image above. So far, I'm able to print the values, just not next to each other.

Share Improve this question edited Jan 18 at 11:02 Jens 69.5k15 gold badges103 silver badges130 bronze badges asked Jan 18 at 9:38 TheNomadTheNomad 11 silver badge 5
  • 2 Your problem is being caused by faulty analysis of parts of responsibility. For instance, you shouldn't mix in looping and presentation into a conversion method. The conversion method should take in one value and return one value. That's it – g00se Commented Jan 18 at 10:15
  • 1 The code posted in the question has three methods and multiple System.out.print statements. The requirements are two methods and one System.out.print statement. The System.out.print statement should print a String, which you create. Start by just printing the table header. Once that's correct, try printing one line of the table. Once that's correct, try putting in a while loop to print all the table values. Do one small step at a time. – Gilbert Le Blanc Commented Jan 18 at 10:23
  • 2 copy from Staged Ground: "(Hint?) basically you want to have one single larger table instead of two tables? Please check Why should I not upload images of code/data/errors? on Meta Stack Overflow – user85421 Commented Jan 18 at 10:43
  • For future reference, questions of this type should include examples. Each example should include input, if applicable (✔), expected output (✔), and actual output (✗). You might also find these resources, with their links, helpful: The Tour, How to Ask a Good Question, and other pages in the Help Center. – Old Dog Programmer Commented Jan 18 at 15:39
  • Understood. Thank you for the feedback, I'll certainly keep this in mind. – TheNomad Commented Jan 18 at 19:20
Add a comment  | 

2 Answers 2

Reset to default 4

First you have to determine how the output will look. In order to do that, I open a text editor (in my case it was Notepad). I determine the maximum value that will appear in the conversion table and then write a single row of the table with the maximum values. I add the | separator and then format the row the way I want it to look. Then I add in the headings and format them also according to the way I want it to look. This is how my Notepad looks.
(Note that the | in front of -123.456 is the cursor and is not part of the text.)

Now I can start to code.

The headings are constant strings so I just copy them from Notepad to my Java IDE (in my case Eclipse) and put them in a System.out.println() call.

What remains is to print the contents of the conversion table. I need to print both conversions, i.e. Fahrenheit-to-Celsius and Celsius-to-Fahrenheit, on the same line, so I need to perform both calculations in a single iteration of a single loop.

public class TempConversion {

    private static void printTemperatureConversionTable() {
        System.out.println("     Temperature Conversion Tables");
        System.out.println();
        System.out.println();
        System.out.println("   Temperature     |     Temperature");
        System.out.println("    (degrees)      |      (degrees)");
        System.out.println("   F         C     |     C         F");
        double celsius;
        double fahrenheit;
        for (int degrees = -40; degrees <= 65; degrees += 5) {
            fahrenheit = degrees;
            celsius = convertFahrenheitToCelsius(fahrenheit);
            System.out.printf("% 7.3f  % 8.3f  |  ", fahrenheit, celsius);
            celsius = degrees;
            fahrenheit = convertCelsiusToFahrenheit(celsius);
            System.out.printf("% 7.3f  % 8.3f", celsius, fahrenheit);
            System.out.println();
        }
    }

    private static double convertCelsiusToFahrenheit(double celsius) {
        return (celsius * 9.0 / 5.0) + 32.0;
    }

    private static double convertFahrenheitToCelsius(double fahrenheit) {
        return (fahrenheit - 32.0) * 5.0 / 9.0;
    }

    public static void main(String[] args) {
        printTemperatureConversionTable();
    }
}

When I run the above code, I get the following output:

     Temperature Conversion Tables


   Temperature     |     Temperature
    (degrees)      |      (degrees)
   F         C     |     C         F
-40.000   -40.000  |  -40.000   -40.000
-35.000   -37.222  |  -35.000   -31.000
-30.000   -34.444  |  -30.000   -22.000
-25.000   -31.667  |  -25.000   -13.000
-20.000   -28.889  |  -20.000    -4.000
-15.000   -26.111  |  -15.000     5.000
-10.000   -23.333  |  -10.000    14.000
 -5.000   -20.556  |   -5.000    23.000
  0.000   -17.778  |    0.000    32.000
  5.000   -15.000  |    5.000    41.000
 10.000   -12.222  |   10.000    50.000
 15.000    -9.444  |   15.000    59.000
 20.000    -6.667  |   20.000    68.000
 25.000    -3.889  |   25.000    77.000
 30.000    -1.111  |   30.000    86.000
 35.000     1.667  |   35.000    95.000
 40.000     4.444  |   40.000   104.000
 45.000     7.222  |   45.000   113.000
 50.000    10.000  |   50.000   122.000
 55.000    12.778  |   55.000   131.000
 60.000    15.556  |   60.000   140.000
 65.000    18.333  |   65.000   149.000

I didn't realize we were providing solutions to homework problems and not just hints.

As I said in my comment, break the problem into steps. The requirements state to use two methods and one System.out.print statement. The System.out.print statement goes in one method. It prints a String. Therefore, the other method creates the strings for printing.

Here are the test results from one of my tests,

                 Temperature Conversion Tables

         Temperature         |             Temperature
          (degrees)          |              (degrees) 
       F              C      |           C              F
    -40.000        -40.000   |        -40.000        -40.000
    -35.000        -37.222   |        -35.000        -31.000
    -30.000        -34.444   |        -30.000        -22.000
    -25.000        -31.667   |        -25.000        -13.000
    -20.000        -28.889   |        -20.000         -4.000
    -15.000        -26.111   |        -15.000          5.000
    -10.000        -23.333   |        -10.000         14.000
     -5.000        -20.556   |         -5.000         23.000
      0.000        -17.778   |          0.000         32.000
      5.000        -15.000   |          5.000         41.000
     10.000        -12.222   |         10.000         50.000
     15.000         -9.444   |         15.000         59.000
     20.000         -6.667   |         20.000         68.000
     25.000         -3.889   |         25.000         77.000
     30.000         -1.111   |         30.000         86.000
     35.000          1.667   |         35.000         95.000
     40.000          4.444   |         40.000        104.000
     45.000          7.222   |         45.000        113.000
     50.000         10.000   |         50.000        122.000
     55.000         12.778   |         55.000        131.000
     60.000         15.556   |         60.000        140.000
     65.000         18.333   |         65.000        149.000
     70.000         21.111   |         70.000        158.000
     75.000         23.889   |         75.000        167.000
     80.000         26.667   |         80.000        176.000
     85.000         29.444   |         85.000        185.000
     90.000         32.222   |         90.000        194.000
     95.000         35.000   |         95.000        203.000
    100.000         37.778   |        100.000        212.000

I formatted one table line to see how long it was. It turned out to be 60 characters, which made creating the header lines easier.

I created one header line at a time so I could get the spacing correct.

Here's the complete runable code. Two methods and one System.out.print statement.

public class TempertureConversion {

    public static void main(String[] args) {
        String formatter = "%15.3f";
        String title = " ".repeat(21) + "Temperature Conversion Tables";
        String blankLine = "";
        String subtitle1 = " ".repeat(13) + "Temperature"
                + "         |             " + "Temperature";
        String subtitle2 = " ".repeat(13) + " (degrees) "
                + "         |             " + " (degrees) ";
        String subtitle3 = " ".repeat(11) + "F" + " ".repeat(14) + "C"
                + "      |           " + "C" + " ".repeat(14) + "F";

        printLine(title);
        printLine(blankLine);
        printLine(subtitle1);
        printLine(subtitle2);
        printLine(subtitle3);

        for (int value = -40; value <= 100; value += 5) {
            double initial = value;
            double celsius = (initial - 32.0) * 5.0 / 9.0;
            double fahrenheit = initial * 9.0 / 5.0 + 32.0;

            String valueString = String.format(formatter, initial);
            String celsiusString = String.format(formatter, celsius);
            String fahrenheitString = String.format(formatter, fahrenheit);

            String output = valueString + celsiusString + "   |" + valueString
                    + fahrenheitString;
            printLine(output);
        }

    }

    private static void printLine(String line) {
        System.out.println(line);
    }

}
              

   
发布评论

评论列表(0)

  1. 暂无评论