im relatively new to java, i know the basics and can program competently for a beginner, i think. I can use GUIs but am not 100% on them. ive been working on a very basic calculator by using if statements to change the operation. is there a way to let the user input the operator.
here's my current script.(long i know):
import java.util.Scanner;
public class apples{
public static void main(String args[]){
Scanner x = new Scanner(System.in);
System.out.println("please select the operation you want to be done, for addition press 1. for subtraction press 2.for multiplication press 3. for division press 4. for squaring press 5");
int num1, decide;
double num2,answer;
decide=x.nextInt();
if(decide<2){
System.out.println("you have selected addition");
System.out.println("type first number");
num1=x.nextInt();
System.out.println("type second number");
num2=x.nextInt();
answer=num1+num2;
System.out.println(answer);
}if(decide<3 && decide>1){
System.out.println("you have selected subtraction");
System.out.println("type first number");
num1=x.nextInt();
System.out.println("type second number");
num2=x.nextInt();
answer=num1-num2;
System.out.println(answer);
}if(decide<4 && decide>2){
System.out.println("you have selected multiplication");
System.out.println("type first number");
num1=x.nextInt();
System.out.println("type second number");
num2=x.nextInt();
answer=num1*num2;
System.out.println(answer);
}if(decide<5 && decide>3){
System.out.println("you have selected division");
System.out.println("type first number");
num1=x.nextInt();
System.out.println("type second number");
num2=x.nextInt();
answer=num1/num2;
System.out.println(answer);
}if(decide>4){
System.out.println("you have selected squaring");
System.out.println("type the number to be squared");
num1=x.nextInt();
answer=num1*num1;
System.out.println(answer);
}
}
}
new Java guy looking for help.
Started by JavaObject, Aug 23 2010 03:09 AM
9 replies to this topic
#1
Posted 23 August 2010 - 03:09 AM
|
|
|
#2
Posted 23 August 2010 - 08:15 AM
If you ask for the operator just scan it as a string. Instead of if statements, a switch would be better in this case. like:
PS: Click the #-button to post code
String operator = scanner.nextLine();
switch( operator ){
case "+": //code here for addition
//some more code for addition
break;
case "-": //Same
break;
...
...
default: System.out.println("Wrong operator!");
}
PS: Click the #-button to post code
#3
Posted 23 August 2010 - 02:25 PM
As oxano said pls use the code tags when you want to write code [ CODE ] code here [ /CODE ] (minus spaces) second use indention a tab will do or 3-4 spaces.
yeah as oxano suggested you can use switch case if you can.
(btw oxano have you tried your code? becuase I don't think that switch excepts any other argument except for int or char)
so anyway use char and use single quotation sign not double.
if you don't know what switch-case is you can still use if-else
yeah as oxano suggested you can use switch case if you can.
(btw oxano have you tried your code? becuase I don't think that switch excepts any other argument except for int or char)
so anyway use char and use single quotation sign not double.
if you don't know what switch-case is you can still use if-else
char operator;
operator = x.nextChar();
if (operator == '+')
{
...
}
else if (operator == '-')
{
...
}
...
...
...
else
{
System.out.println("unknown operator");
}
#4
Posted 23 August 2010 - 02:31 PM
Yepp I've checked it... switch can't use a parameter that is not convertible to int or isn't enum. Probaly becuase of it uses == to see if they are the same which goes only for primitive types and in case of String == is checking if they have the same address. So in case you're using String you would need to use if-else and go with if(operator.equalsIgnoreCase("+")){...} or any other operator in which case you can use command sqrt for square.
#5
Posted 23 August 2010 - 02:49 PM
I've took the liberty of rewriting your code and making it as you want so that the user types in the operator. Here's the code you you can't understand something feel free to ask.
import java.util.Scanner;
//Convention for java to write
//name of the class with
//capital letter.
public class Apples
{
public static void main(String[] args)
{
//name variables so you understand what
//they're for
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter an operator(+, -, *, /, sq):");
String operator = keyboard.next();
if(operator.equals("+"))
{
System.out.print("Enter first number: ");
int num1 = keyboard.nextInt();
System.out.print("Enter second number: ");
int num2 = keyboard.nextInt();
System.out.println("Answer: " + (num1+num2));
}
else if(operator.equals("-"))
{
System.out.print("Enter first number: ");
int num1 = keyboard.nextInt();
System.out.print("Enter second number: ");
int num2 = keyboard.nextInt();
System.out.println("Answer: " + (num1-num2));
}
else if(operator.equals("*"))
{
System.out.print("Enter first number: ");
int num1 = keyboard.nextInt();
System.out.print("Enter second number: ");
int num2 = keyboard.nextInt();
System.out.println("Answer: " + (num1*num2));
}
else if(operator.equals("/"))
{
System.out.print("Enter first number: ");
int num1 = keyboard.nextInt();
System.out.print("Enter second number: ");
int num2 = keyboard.nextInt();
System.out.println("Answer: " + (num1/num2));
}
else if(operator.equalsIgnoreCase("sq"))
{
System.out.print("Enter the number: ");
int num1 = keyboard.nextInt();
System.out.println("Answer: " + (num1*num1));
}
else
System.out.println("unknown operator");
}
}
#6
Posted 23 August 2010 - 11:01 PM
Roman Y said:
(btw oxano have you tried your code? becuase I don't think that switch excepts any other argument except for int or char)
a workaround for the switch statement can be done like here: A Switch on String Idiom for Java
But if statements are probably easier to understand if you're new to java ;)
#7
Posted 24 August 2010 - 12:03 AM
hehe clever... making an enum that takes a string as a parameter checks if it has same value as any of the enum constants and returning the constant which has the same value.
But I don't think that as a beginner in programming one should really go in on enum class. I've been programming for some time now I even I still don't fully understand them...
But I don't think that as a beginner in programming one should really go in on enum class. I've been programming for some time now I even I still don't fully understand them...
#8
Posted 24 August 2010 - 12:35 AM
Yea i find them hard to use too but it looks pretty :love:
#9
Posted 24 August 2010 - 03:26 AM
yeah... but I've heard thet they're specially complex in Java, and they're a bit different (easier) in C++ and C#
#10
Posted 24 August 2010 - 04:56 AM
Java supports methods and constructors in enumerations, which i think is very useful, and allows to write beautiful code.
It's fairly easy to understand.
public enum Planet {
MERCURY (3.303e+23, 2.4397e6),
VENUS (4.869e+24, 6.0518e6),
EARTH (5.976e+24, 6.37814e6),
MARS (6.421e+23, 3.3972e6),
JUPITER (1.9e+27, 7.1492e7),
SATURN (5.688e+26, 6.0268e7),
URANUS (8.686e+25, 2.5559e7),
NEPTUNE (1.024e+26, 2.4746e7),
PLUTO (1.27e+22, 1.137e6);
private final double mass; // in kilograms
private final double radius; // in meters
Planet(double mass, double radius) {
this.mass = mass;
this.radius = radius;
}
public double mass() { return mass; }
public double radius() { return radius; }
// universal gravitational constant (m3 kg-1 s-2)
public static final double G = 6.67300E-11;
public double surfaceGravity() {
return G * mass / (radius * radius);
}
public double surfaceWeight(double otherMass) {
return otherMass * surfaceGravity();
}
}
It's fairly easy to understand.


Sign In
Create Account

Back to top









