java programming help needed

April 6, 2010 at 11:08:56
Specs: Windows Vista
I am completely new to java, and programming in general. I have to write a java program where the user inputs 2 numbers, and decides whether they want to add, subtract, mutliply or divide those two numbers. The program will output the results, and ask for the next number until the user exits. I have to use Repeat/Until loop logic. Any examples or suggestions would be greatly appreciated.


See More: java programming help needed

Report •


#1
April 12, 2010 at 20:19:06
If you are new to java I recommend doing something like this:

Create a instance field or class scope variable type string

Create a while loop that breaks when the
string.equalsIgnoreCase("q"). You must use the equals
method because strings are not native types.

Within the loop ask the user for a number, another number,
and either * / + -. If you don't know how to use the scanner
class for input tell me and I will help you.

Then convert the string to a number for the numbers using
double one = Double.parseDouble(What ever you called it)
double two = Double.parseDouble(What ever you called it)
String sign = What ever you called it

Then use a switch statement that checks the sign

if it equals + than add etc...

At the end ask if they want to continue, so if they enter a q it
will quit the loop.

I will be glad to write a sample program for you, just tell me if
you need it. :-)


Report •

#2
April 12, 2010 at 20:35:00
Here I change it around a little, made it simpler

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package spiderlink;

/**
*
* @author test
*/
import java.util.Scanner;

class CalcApp {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
String quit = "y";
while (!quit.equalsIgnoreCase("q")) {

double ans = 0.0;
System.out.println("Please enter first number: ");
double one = sc.nextDouble();
System.out.println("Please enter second number: ");
double two = sc.nextDouble();
sc.nextLine();
System.out.println("Please enter symbol: ");
String symbol = sc.nextLine();

if (symbol.equals("*")) {
ans = one * two;
} else if (symbol.equals("/")) {
ans = one / two;
} else if (symbol.equals("+")) {
ans = one + two;
} else if (symbol.equals("-")) {
ans = one - two;
}

System.out.println("The answer is: " + ans);

System.out.println("Enter q to quit y to continue ");
quit = sc.nextLine();

}

}
}

If you need any more help please post


Report •

#3
April 12, 2010 at 20:36:28
Sry bout that I was typing in a file I was working on here's just
the code you need (didn't feel like opening up a blank file lol)

import java.util.Scanner;

class CalcApp {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
String quit = "y";
while (!quit.equalsIgnoreCase("q")) {

double ans = 0.0;
System.out.println("Please enter first number: ");
double one = sc.nextDouble();
System.out.println("Please enter second number: ");
double two = sc.nextDouble();
sc.nextLine();
System.out.println("Please enter symbol: ");
String symbol = sc.nextLine();

if (symbol.equals("*")) {
ans = one * two;
} else if (symbol.equals("/")) {
ans = one / two;
} else if (symbol.equals("+")) {
ans = one + two;
} else if (symbol.equals("-")) {
ans = one - two;
}

System.out.println("The answer is: " + ans);

System.out.println("Enter q to quit y to continue ");
quit = sc.nextLine();

}

}
}


Report •

Related Solutions

#4
April 13, 2010 at 12:42:30
Thanks so much for your help!

Report •


Ask Question