Post History
A clever way to replace Math functions You can shorten if-else statements through the a?b:c syntax, where a is the statement of if, b is the result of if, and c is the result of else. static vo...
Answer
#2: Post edited
- # A clever way to replace `Math` functions
- You can shorten `if-else` statements through the `a?b:c` syntax, where `a` is the statement of `if`, `b` is the result of `if`, and `c` is the result of `else`.
- <!-- language-all: lang-java -->
- <pre><code>static void main(String[]a){
- 	int i = 95;
	System.out.print(i>0?"positive":"negative");- }
- </code></pre>
- [Try it online!][TIO-kurz24jb]
[TIO-kurz24jb]: https://tio.run/##y0osS9TNSsn@n5lXklqUlpicquBb/b@4JLEkM1mhLD8zRSE3MTNPI7ikKDMvPTo2UbOaixOoVCFTwVbB0tSaizO4srgkNVcvv7RErwCopkQj087AXqkgvzizJLMsVclKKS81PRHM1LTmqv1f@x8A "Java (JDK) – Try It Online"- With this, you can take out the following functions from `java.util.Math`:
- - `Math.max()` and `Math.min()`
- <!-- language-all: lang-java -->
- <pre><code>import java.util.*;
- interface M{
- 	static void main(String[]x){
- 		int a = 20, b = 30;
- 		System.out.println(Math.max(a,b));
- 		System.out.println(a>b?a:b);
- 		System.out.println(Math.min(a,b));
- 		System.out.println(a<b?a:b);
- 	}
- }
- </code></pre>
- [Try it online!][TIO-kurz8p62]
- [TIO-kurz8p62]: https://tio.run/##fY69DoIwFIVn@hR3pAYbopv48wRMjMbhFlBvpYXAhWAMz147ucl0knO@fDkGJ9ya6uU92a7tGUwo1MjUqE0mBDmu@zuWNeQfEQ2MTCVMLVVgkVxccE/ucb3NMqxRgAHhBLs0AR1yn2ahLd4D11a1I6su0Ny4OEd@KotzjImW8g@EZ33Bg5arjvBh1XH8ORaxeP8F "Java (JDK) – Try It Online"
- # A clever way to replace `Math` functions
- You can shorten `if-else` statements through the `a?b:c` syntax, where `a` is the statement of `if`, `b` is the result of `if`, and `c` is the result of `else`.
- <!-- language-all: lang-java -->
- <pre><code>static void main(String[]a){
- 	int i = 95;
- 	System.out.print(i>0?"positive":"negative/zero");
- }
- </code></pre>
- [Try it online!][TIO-kurz24jb]
- [TIO-kurz24jb]: https://tio.run/##FcqxDoIwEADQmX7FpRMMogsDEuELmBiNwwUOchha0p5NlPDtFbY3vBkDXubhHdkIuRF7gnaLXlC4h2B5gAXZpJ04NtPzhdmmkqMCwwPKolJJ9/VCS24/kq/HkZTrW6NX61k4kL5rQxOevP7IWZ1Vao97/AM "Java (JDK) – Try It Online"
- With this, you can take out the following functions from `java.util.Math`:
- - `Math.max()` and `Math.min()`
- <!-- language-all: lang-java -->
- <pre><code>import java.util.*;
- interface M{
- 	static void main(String[]x){
- 		int a = 20, b = 30;
- 		System.out.println(Math.max(a,b));
- 		System.out.println(a>b?a:b);
- 		System.out.println(Math.min(a,b));
- 		System.out.println(a<b?a:b);
- 	}
- }
- </code></pre>
- [Try it online!][TIO-kurz8p62]
- [TIO-kurz8p62]: https://tio.run/##fY69DoIwFIVn@hR3pAYbopv48wRMjMbhFlBvpYXAhWAMz147ucl0knO@fDkGJ9ya6uU92a7tGUwo1MjUqE0mBDmu@zuWNeQfEQ2MTCVMLVVgkVxccE/ucb3NMqxRgAHhBLs0AR1yn2ahLd4D11a1I6su0Ny4OEd@KotzjImW8g@EZ33Bg5arjvBh1XH8ORaxeP8F "Java (JDK) – Try It Online"
#1: Initial revision
# A clever way to replace `Math` functions You can shorten `if-else` statements through the `a?b:c` syntax, where `a` is the statement of `if`, `b` is the result of `if`, and `c` is the result of `else`. <!-- language-all: lang-java --> <pre><code>static void main(String[]a){ 	int i = 95; 	System.out.print(i>0?"positive":"negative"); } </code></pre> [Try it online!][TIO-kurz24jb] [TIO-kurz24jb]: https://tio.run/##y0osS9TNSsn@n5lXklqUlpicquBb/b@4JLEkM1mhLD8zRSE3MTNPI7ikKDMvPTo2UbOaixOoVCFTwVbB0tSaizO4srgkNVcvv7RErwCopkQj087AXqkgvzizJLMsVclKKS81PRHM1LTmqv1f@x8A "Java (JDK) – Try It Online" With this, you can take out the following functions from `java.util.Math`: - `Math.max()` and `Math.min()` <!-- language-all: lang-java --> <pre><code>import java.util.*; interface M{ 	static void main(String[]x){ 		int a = 20, b = 30; 		System.out.println(Math.max(a,b)); 		System.out.println(a>b?a:b); 		System.out.println(Math.min(a,b)); 		System.out.println(a<b?a:b); 	} } </code></pre> [Try it online!][TIO-kurz8p62] [TIO-kurz8p62]: https://tio.run/##fY69DoIwFIVn@hR3pAYbopv48wRMjMbhFlBvpYXAhWAMz147ucl0knO@fDkGJ9ya6uU92a7tGUwo1MjUqE0mBDmu@zuWNeQfEQ2MTCVMLVVgkVxccE/ucb3NMqxRgAHhBLs0AR1yn2ahLd4D11a1I6su0Ny4OEd@KotzjImW8g@EZ33Bg5arjvBh1XH8ORaxeP8F "Java (JDK) – Try It Online"