Console Calc
2 posters
Page 1 of 1
Whats Your Favorite Team?
Console Calc
We Are Trying To Make This Into A Complex Calculator Application Post Your Ideas & Ways To Improve It!!
Remember To Vote!! ^^^^^ XD!!
Remember To Vote!! ^^^^^ XD!!
- Code:
import java.util.Scanner;
/**
*
* @author Petahwil
*/
public class ConsoleCalc {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.println("Petahwil");
Scanner in = new Scanner (System.in);
System.out.print("Enter a Expression: ");
String expression = in.next();
String save = expression;
String num1 = "";
String num2= "";
String operator = "";
int result = 0;
boolean done = false;
boolean error = false;
for(int i = 0; i < expression.length(); i++)
{
if(expression.charAt(i) >= '0' && expression.charAt(i) <= '9')
{
if (done == false)
{
num1 = num1 +""+ expression.charAt(i);
}
else
{
num2 = num2 +""+ expression.charAt(i);
}
}
else
{
if (expression.charAt(i) == '+' || expression.charAt(i) == '-'
|| expression.charAt(i) == '*'
||expression.charAt(i) == '/')
{
done = true;
operator = "" + expression.charAt(i);
}
else
{
error = true;
System.out.print("Wrong Format"); break;
}
}
}
if (error == false)
{
if (operator.equals("+"))
{
result = Integer.valueOf(num1) + Integer.valueOf(num2);
}
else if (operator.equals("-"))
{
result = Integer.valueOf(num1) - Integer.valueOf(num2);
}
else if (operator.equals("*"))
{
result = Integer.valueOf(num1) * Integer.valueOf(num2);
}
else if (operator.equals("/"))
{
result = Integer.valueOf(num1) / Integer.valueOf(num2);
}
save += " = "+result;
System.out.print(save);
}
}
}
Last edited by ThienDinh on Sun Dec 02, 2012 5:59 pm; edited 2 times in total (Reason for editing : Put code in [code] tag)
Petahwil- Posts : 8
Join date : 2012-11-30
Age : 31
Console Calc
Basketball teams haha!! Thats why there is a guy haha!! It said to write Break after it and it is showing it now so idk what it is doing that lol and wont let me re-edit it!
Petahwil- Posts : 8
Join date : 2012-11-30
Age : 31
Re: Console Calc
You should be able to edit ur message. I checked the permission and it allows member to edit their own messages.
Console Calc
It Lets me Just not the pole is showed up the first time but now it doesn't its not big deal just know that the break is irrelevant lol
Petahwil- Posts : 8
Join date : 2012-11-30
Age : 31
Re: Console Calc
Boom!
- Code:
import java.util.Scanner;
/**
*
* @author Petahwil
*/
public class ConsoleCalc {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.println("Petahwil");
Scanner in = new Scanner(System.in);
System.out.print("Enter a Expression: ");
String expression = in.next();
String save = expression;
String num1 = "";
String num2 = "";
String operator = "";
int result = 0;
boolean done = false;
boolean error = false;
for (int i = 0; i < expression.length(); i++) {
if (expression.charAt(i) >= '0' && expression.charAt(i) <= '9') {
if (done == false) {
num1 = num1 + "" + expression.charAt(i);
} else {
num2 = num2 + "" + expression.charAt(i);
}
} else {
if (expression.charAt(i) == '+' || expression.charAt(i) == '-'
|| expression.charAt(i) == '*'
|| expression.charAt(i) == '/') {
done = true;
operator = "" + expression.charAt(i);
} else {
error = true;
System.out.print("Wrong Format");
break;
}
}
}
if (error == false) {
if (operator.equals("+")) {
result = Integer.valueOf(num1) + Integer.valueOf(num2);
} else if (operator.equals("-")) {
result = Integer.valueOf(num1) - Integer.valueOf(num2);
} else if (operator.equals("*")) {
result = Integer.valueOf(num1) * Integer.valueOf(num2);
} else if (operator.equals("/")) {
result = Integer.valueOf(num1) / Integer.valueOf(num2);
}
save += "=" + result;
System.out.print(save);
}
expression = in.next();
while (expression.equals("q") == false) {
operator = "" + expression.charAt(0);
num1 = "";
for (int i = 1; i < expression.length(); i++) {
num1 += "" + expression.charAt(i);
}
if (operator.equals("+")) {
result += Integer.valueOf(num1);
} else if (operator.equals("-")) {
result -= Integer.valueOf(num1);
} else if (operator.equals("*")) {
result *= Integer.valueOf(num1);
} else if (operator.equals("/")) {
result /= Integer.valueOf(num1);
}
save += expression + "=" + result;
System.out.print(save);
expression = in.next();
}
}
}
Petahwil- Posts : 8
Join date : 2012-11-30
Age : 31
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
Sat Feb 15, 2014 10:12 am by doriperni
» Sync2 V2.20.1312 X86x64.rar.rar
Fri Feb 14, 2014 7:37 pm by carmibire
» 9.1 Inheritance Hierarchies
Tue Feb 26, 2013 12:50 pm by ThienDinh
» Useful website
Sat Dec 08, 2012 6:52 pm by tpham1991
» The Machine that Changed the World: Giant Brains. 1992 Documentary
Sat Dec 08, 2012 2:16 am by ThienDinh
» Problems in practice test.
Thu Dec 06, 2012 2:32 pm by ThienDinh
» Newton's Method
Mon Dec 03, 2012 8:32 pm by ThienDinh
» Console Calc
Sun Dec 02, 2012 5:57 pm by Petahwil
» Law Of Cosines
Sat Dec 01, 2012 4:54 pm by Petahwil