Sedikit Info Seputar
Console Calculator program using Java
Terbaru 2017
- Hay gaes kali ini team Game Android Apk, kali ini akan membahas artikel dengan judul Console Calculator program using Java, kami selaku Team Game Android Apk telah mempersiapkan artikel ini untuk sobat sobat yang menyukai Game Android Apk. semoga isi postingan tentang yang saya posting kali ini dapat dipahami dengan mudah serta memberi manfa'at bagi kalian semua, walaupun tidak sempurna setidaknya artikel kami memberi sedikit informasi kepada kalian semua. ok langsung simak aja sob
Judul:
Berbagi Info Seputar
Console Calculator program using Java
Terbaru
link: Console Calculator program using Java
Berbagi Console Calculator program using Java Terbaru dan Terlengkap 2017
This is sample Calculator program based on console input. You will be allowed to give input repeatedly until you type "exit".
Is does not check for precedence of character. It start to calculate from right to left recursively.
Here is the code:
import java.util.Scanner;
/**
* Sample Calculator without precedence check.
* @author ojhay
*
*/
public class CalculatorConsole {
public static void main( String[] args ) {
Scanner sc = new Scanner( System.in );
System.out.println( "Calculator:" );
while ( true ) {
System.out.print( "Type arithmetic Expression: " );
String expression = sc.nextLine( );
// Exit if user input: exit
if ( expression.equalsIgnoreCase( "exit" ) ) {
break;
}
double result = expre( expression );
System.out.println( expression + "= " + result );
}
sc.close( );
}
// Recursive method to evaluate arithmetic expression.
public static double expre( String expre ) {
if ( ! ( expre.contains( "+" ) || expre.contains( "-" ) || expre.contains( "*" ) || expre.contains( "/" ) ) ) {
return Integer.valueOf( expre );
}
double result = 0;
for ( int i = 0; i < expre.length( ); i++ ) {
char symbol = expre.charAt( i );
if ( !Character.isDigit( symbol ) ) { // Symbol
double operand1 = Integer.parseInt( expre.substring( 0, i ) );
switch ( symbol ) {
case '+':
result = operand1 + expre( expre.substring( i + 1 ) );
break;
case '-':
result = operand1 - expre( expre.substring( i + 1 ) );
break;
case '*':
result = operand1 * expre( expre.substring( i + 1 ) );
break;
case '/':
result = operand1 / expre( expre.substring( i + 1 ) );
break;
}
break;
}
}
return result;
}
}
Source: yro-tech.blogspot.com