0

As you can see from the photos

enter image description here

if a side is a single value digit, then the "hypotenuse" tests won't match. How can I make it so that regardless of whether the sides are one or two value digits, the text of output will match?

Only looking to use beginner-ish statements.

public class PyTheorem {
    public static void main(String[] args) {
            
        double x = 5 + (Math.random() * (23-1)); //generates random number between 5 and 22 for side x of triangle 1
        double y = 5 + (Math.random() * (23-1)); //generates random number between 5 and 22 for side y of triangle 1
        double q = 5 + (Math.random() * (23-1)); //generates random number between 5 and 22 for side q of triangle 2
        double r = 5 + (Math.random() * (23-1)); //generates random number between 5 and 22 for side r of triangle 2
        
        
        int a = (int)(x); //a inherits the value of side x in an integer form 
        int b = (int)(y); //b inherits the value of side y in an integer form 
        int c = (int)(q); //c inherits the value of side q in an integer form 
        int d = (int)(r); //d inherits the value of side r in an integer form 
      
        double hyp1 = Math.sqrt(Math.pow(a,2) + Math.pow(b,2)); //finds hypotenuse for triangle 1
        double hyp2 = Math.sqrt(Math.pow(c,2) + Math.pow(d,2)); //finds hypotenuse for triangle 2
        
        System.out.print("Triangle 1      Side 1: " + a);
        System.out.println("        Side 2: " + b + "        Hypotenuse: " + hyp1);
        System.out.println("Triangle 2      Side 1: " + c + "        Side 2: " + d + "        Hypotenuse: " + hyp2);      
    }
}
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • 1
    Does this answer your question? [How to print a table of information in Java](https://stackoverflow.com/questions/18672643/how-to-print-a-table-of-information-in-java) – akuzminykh Sep 12 '20 at 20:05
  • [Java printf formatting to print items in a table or columns](https://stackoverflow.com/q/33466526/12323248) – akuzminykh Sep 12 '20 at 20:07
  • Take a moment to go through [`Format`](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#format-java.lang.String-java.lang.Object...-) and some [examples](https://dzone.com/articles/java-string-format-examples) – SomeDude Sep 12 '20 at 20:09

2 Answers2

0

You use the tab character when printing. So add two or 3 tabs between each value. At first I thought your hypotenuse wasn’t the correct value. Anyway:

System.out.println(“Triangle 1 \t\t Side 1: “ + a + “ \t\t Side 2: “ + b + “ \t\t Hypotenuse: “ + hyp1);

Do the same for triangle 2, make sure you keep your tabs consistent to align

——

EDIT. System.out.format is another way as in the comments. This is the naive, simple way.

srv236
  • 509
  • 3
  • 5
0

You should use the String.format command to tell Java just how you'd like your output formatted. In addition to telling it you want your integer values to always take up two spaces, you can format your floating point value to always show two whole places and two decimal places as well:

System.out.println(String.format("Triangle 1    Side 1: %2d   Side 2: %2d  Hypotenuse: %2.2f", a, b, hyp1));
System.out.println(String.format("Triangle 2    Side 1: %2d   Side 2: %2d  Hypotenuse: %2.2f", c, d, hyp2));

Sample result:

Triangle 1    Side 1: 19   Side 2:  8  Hypotenuse: 20.62
Triangle 2    Side 1: 21   Side 2: 14  Hypotenuse: 25.24
CryptoFool
  • 21,719
  • 5
  • 26
  • 44