Good basic calculator,
Slightly modified to so it gives you an error when you divide by zero unless you divide 0 by 0 itself if your interested...
import java.util.Scanner;
/
@author Co-exist aka eXero /
public class Main {
/
@param args the command line arguments
/
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
String operator;
double result = 0;
System.out.println("Please enter a number:");
double num1 = keyboard.nextDouble();
System.out.println("Please enter another number:");
double num2 = keyboard.nextDouble();
System.out.println("Enter an expression: ");
operator = keyboard.next();
if (operator.equals("+"))
{
result = num1 + num2;
System.out.println("\n" + num1 + operator + num2 + "=" + result + "\n" );
}
else if (operator.equals("-"))
{
result = num1 - num2;
System.out.println("\n" + num1 + operator + num2 + "=" + result + "\n" );
}
else if (operator.equals("*"))
{
result = num1 num2;
System.out.println("\n" + num1 + operator + num2 + "=" + result + "\n" );
}
if (operator.equals("/"))
{
if ((num1 == 0) && (num2 == 0))
{
System.out.println("\n" + num1 + operator + num2 + "=" + result + "\n" );
}
else if ((num1 != 0) && (num2 == 0))
{
System.out.println ("\n" + "Cannot Divide by Zero mate" + "\n" + "\n");
}
else
{
result = num1 / num2;
System.out.println("\n" + num1 + operator + num2 + "=" + result + "\n" );
}
}
}
}
I am not flaming, just posting this if you wanted to find out how to fix this 'problem'.
I like it..I mean im not into Java im into Python but still this is a good and easy tutorial even I could understand.
Thanks Mate =). This was helpful. Good Share.
thanks all you positive commenters. and thanks postmortis for you zero devision source code.
I know i'm bringing this thread back from the grave, but I am a just starting java as of Wednesday and I was wondering if it was possible to put a space between the numbers. I put a space around the " = " so the result and second number would be space out, but my problem was figuring out a way to put a space around the operator. Since operator could be 1 of 4 commands, how would you right the if statement for each command to put a space around the operator. If you know what I'm trying to describe...
(Nov 13, 2010 09:44 AM)pro-phecy Wrote: I know i'm bringing this thread back from the grave, but I am a just starting java as of Wednesday and I was wondering if it was possible to put a space between the numbers. I put a space around the " = " so the result and second number would be space out, but my problem was figuring out a way to put a space around the operator. Since operator could be 1 of 4 commands, how would you right the if statement for each command to put a space around the operator. If you know what I'm trying to describe...
At least your post isn't a retarded one like most grave digging posts.
Try something like this:
Code System.out.println(num1 + " " + operator + " " + num2 + " = " + result);
Is that what you mean?
yeah, that's exactly what I meant! I tried to put the space between the + signs like
Code System.out.println(num1 " " + operator " " + num2 + " = " + result);
when using a space in a calculation, do you always put (+) around a space? so it prints it or how does that work?
and thanks once again Ben!
How is this an advanced calculator?
DO NOT DEAL WITH THIS MEMBER
Ehh it's not what I would call "advanced"
Maybe if it did like log and trig functions, or at the very least square roots and exponents
(Nov 13, 2010 08:20 PM)BeezyB Wrote: How is this an advanced calculator?
read the entire post and you'll see that your post was redundant
(Nov 14, 2010 12:28 AM)pro-phecy Wrote: read the entire post and you'll see that your post was redundant
Nah bro 😅
It's a public forum for public opinions, if that's how I feel, I can most certainly express it 👍
(Nov 14, 2010 07:17 AM)BeezyB Wrote: Nah bro 😅
It's a public forum for public opinions, if that's how I feel, I can most certainly express it 👍
I'm not saying you can't express it, but your question and answer has already been posed and answered, which makes it "seem" redundant.
(Nov 13, 2010 09:44 AM)pro-phecy Wrote: I know i'm bringing this thread back from the grave, but I am a just starting java as of Wednesday and I was wondering if it was possible to put a space between the numbers. I put a space around the " = " so the result and second number would be space out, but my problem was figuring out a way to put a space around the operator. Since operator could be 1 of 4 commands, how would you right the if statement for each command to put a space around the operator. If you know what I'm trying to describe...
I'm not 100% sure what you're asking but maybe this little snippet of code will help:
Code System.out.println(String.format("1 + 2 = %1$d", 1 + 2));
Try that, is that what you meant by "space around the operator"?
fucking [censored].. meeehhhh -_--
Sorry bout the pics. I don't even have the pics anymore.
|