简体   繁体   中英

How to string or work with textbook style math in java

I want to string math as it is in textbook using java. Please help me with the best option to do this. The code is just for the idea. Thanks

package app1;
import java.util.Scanner;
public class Tasnim {
    public static void main (String[] args){

double a,v=4,u=0,t=7;
    a=(v-u)/t;
        System.out.println("       "+"v - u");
        System.out.println("a = —————————");
        System.out.println("         t");
        System.out.println("   ");
        System.out.println("     "+v+" - "+u);
        System.out.println("a = —————————");
        System.out.println("        "+t);
        System.out.println("   ");
        System.out.println("        "+(v-u));
        System.out.println("a = —————————");
        System.out.println("        "+t);
        System.out.println("   ");
        System.out.printf("a =%9.4f\n",a);
        System.out.println("   ");
    }
}

Here i use 3 print method for a single line output. I also want to use mathematical symbol as '÷' and '×' etc. Any site or link or any idea where i can find the solution.

/* Output example.
run:
       v - u
a = —————————
         t

     4.0 - 0.0
a = —————————
        7.0

        4.0
a = —————————
        7.0

a =   0.5714  */

Without external libs you hardly be able to avoid line by line printing, but can make it less concatenative. String.format or System.out.printf have a lot of nice formatting abilities which can help in simple cases ( java.util.Formatter ). There are also very skilled formatters for numbers in Java API.

For special symbols try to find the proper code page . Here are list of symbols and supported encoding list . Have no precise answer for the dvision sign, but the general idea is to use \\u\u003c/code> characters:

char c = '\u0025';
String s = new String(new byte[]{(byte) c}, Charset.forName("Cp850"));
System.out.printf("%c", c);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM