 |
|  |
 |
|
devilmunchkin
|
|
first circle of the inferno
Jul 2001 time: 05:31
|
|
Help!
My boyfriend has been trying to figure out this program for his java class for over a week..to no avail. The teacher won't help him and he's at his wits end. I think it's supposed to be a calculator program. He says he's having trouble getting the operator character to read into the program. Can any of you figure this out? Here's what he has for his program so far:
quote:
import java.util.StringTokenizer;
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
import java.io.*;
public class Calculator
{
public static void main(String [] arg) throws IOException
{
BufferedReader keyboard = new
BufferedReader(new InputStreamReader(System.in));
StringTokenizer tokenizer;
int results, results2, results3, results4, num1, num2/*, operator*/, test;
String operator;//, num1, num2;
String pane1, pane2;
pane1 = JOptionPane.showInputDialog("The purpose of this program is to mimic a calculator."
+ "\n\n\nPlease enter the data in this format | 1 + 2 | .");
tokenizer = new StringTokenizer (pane1);
num1 = Integer.parseInt(tokenizer.nextToken());
operator = tokenizer.nextToken();
num2 = Integer.parseInt(tokenizer.nextToken());
test = (char) '+';
results = (num1 + num2);
results2 = (num1 - num2);
results3 = (num1 * num2);
results4 = (num1 / num2);
if (operator = test )
JOptionPane.showMessageDialog(null, "Your results are " + results + ".", "Your results are...", JOptionPane.INFORMATION_MESSAGE);
/*
else if (test == '-')
JOptionPane.showMessageDialog(null, "Your results are " + results2 + ".", "Your results are...", JOptionPane.INFORMATION_MESSAGE);
else if (test == '*')
JOptionPane.showMessageDialog(null, "Your results are " + results3 + ".", "Your results are...", JOptionPane.INFORMATION_MESSAGE);
else if (test == '/')
JOptionPane.showMessageDialog(null, "Your results are " + results4 + ".", "Your results are...", JOptionPane.INFORMATION_MESSAGE);
switch (operator)
{
case '+' : JOptionPane.showMessageDialog(null, "Your results are " + results + ".", "Your results are...", JOptionPane.INFORMATION_MESSAGE);
}
*/
System.exit(0);
}
}
|
|
|
|  |
 |
|
Asher
|
 |
Calgary, Alberta
Nov 1999 time: 22:31
|
|
Try this
code: import java.util.StringTokenizer;
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
import java.io.*;
public class Calculator
{
public static void main(String [] arg) throws IOException
{
BufferedReader keyboard = new
BufferedReader(new InputStreamReader(System.in));
StringTokenizer tokenizer;
int results, results2, results3, results4, num1, num2/*, operator*/, test;
String operator;//, num1, num2;
String pane1, pane2;
pane1 = JOptionPane.showInputDialog("The purpose of this program is to mimic a calculator."
+ "\n\n\nPlease enter the data in this format | 1 + 2 | .");
tokenizer = new StringTokenizer (pane1);
num1 = Integer.parseInt(tokenizer.nextToken());
operator = tokenizer.nextToken();
num2 = Integer.parseInt(tokenizer.nextToken());
//test = (char) '+';
results = (num1 + num2);
results2 = (num1 - num2);
results3 = (num1 * num2);
results4 = (num1 / num2);
if (operator.equals("+") )
JOptionPane.showMessageDialog(null, "Your results are " + results + ".", "Your results are...", JOptionPane.INFORMATION_MESSAGE);
/*
else if (operator.equals("-"))
JOptionPane.showMessageDialog(null, "Your results are " + results2 + ".", "Your results are...", JOptionPane.INFORMATION_MESSAGE);
else if (operator.equals("*"))
JOptionPane.showMessageDialog(null, "Your results are " + results3 + ".", "Your results are...", JOptionPane.INFORMATION_MESSAGE);
else if (operator.equals("/"))
JOptionPane.showMessageDialog(null, "Your results are " + results4 + ".", "Your results are...", JOptionPane.INFORMATION_MESSAGE);
*/
System.exit(0);
}
}
|
|
|  |
 |
|
Jack_www
|
|
Was he suppost to write a program that acts like a caculator. I had to do the same thing in my java intro class. Asher program is a better way going about it.
|
|
|  |
 |
|  |
 |
|
Asher
|
 |
Calgary, Alberta
Nov 1999 time: 22:31
|
|
Because I'm anal and bored at work: a clean version of that program.
code: import java.util.StringTokenizer;
import javax.swing.JOptionPane;
public class Calculator
{
public static void main(String[] arg)
{
StringTokenizer tokenizer;
int num1, num2, result;
String operator, pane1, pane2;
pane1 = JOptionPane.showInputDialog("The purpose of this program is to mimic a calculator."
+ "\n\n\nPlease enter the data in this format | 1 + 2 | .");
tokenizer = new StringTokenizer(pane1);
num1 = Integer.parseInt(tokenizer.nextToken());
operator = tokenizer.nextToken();
num2 = Integer.parseInt(tokenizer.nextToken());
if (operator.equals("+"))
result = num1 + num2;
else if (operator.equals("-"))
result = num1 - num2;
else if (operator.equals("*"))
result = num1 * num2;
else
result = num1 / num2;
JOptionPane.showMessageDialog(null,
"Your results are " + result + ".",
"Your results are...",
JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
Last edited by Asher on 06-10-2003 at 03:57
|
|
|  |
All times are GMT. The time now is 05:31. Apolyton Time is 00:31. |
top of page
|
| archivepost |
|
Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is ON
vB code is ON
Smilies are ON
[IMG] code is ON
|
|
|
|
|
|