d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Building A Very Simple Calculator, Need Help
Add Reply New Topic New Poll
Member
Posts: 31,388
Joined: Mar 25 2009
Gold: 99.00
Jun 11 2019 06:56pm
FOR the time being, i modified code to make it much simpler as i couldn't figure out what i wanted to do lol

so here's the temporary working code:







Code
package com.example.test;

import java.util.Scanner;

public class Main {

public static Scanner scanner = new Scanner(System.in);


public static void main(String[] args) {

System.out.println("Calculator program starting....");

System.out.println("Choose which numbers to operate on: ");
int num1 = scanner.nextInt();
int num2 = scanner.nextInt();
scanner.nextLine();


printOptions();


System.out.println("Choose your option: ");
int option = scanner.nextInt();

switch (option) {
case 1:
System.out.println("You have chosen addition, " + num1 + " will be added to " + num2);
System.out.println("Your answer is: " + (num1 + num2));
break;
case 2:
System.out.println("You have chosen subtraction, " + num1 + " will be subtracted from " + num2);
System.out.println("Your answer is " + (num1 - num2));
break;
case 3:
System.out.println("You have chosen multiplication, " + num1 + " will be multiplied by " + num2);
System.out.println("Your answer is " + (num1 * num2));
break;
case 4:
System.out.println("You have chosen division, " + num1 + " will be divided by " + num2);
System.out.println("Your answer is " + (num1 / num2));
break;

}


}




public static void printOptions() {
System.out.println("List of options: \n" +
"1: for addition \n" +
"2: for subtraction \n" +
"3: for multiplication \n" +
"4: for division \n");
}


}













BUT, what i'd like to do, is add a feature to operate on multiple numbers.....(more then 2)
and going off user input to determine how many elements to operate on... not sure how to go about this though.
If anyone has any hints, that would be awesome :)

This post was edited by ferf on Jun 11 2019 07:23pm
Member
Posts: 31,388
Joined: Mar 25 2009
Gold: 99.00
Jun 11 2019 07:33pm
/EDIT, also not sure why but i can't use double datatype, gives me an exception
/EDIT nvm i'm dumb

double num1 = scanner.nextDouble();
double num2 = scanner.nextDouble();


works now










/EDIT

Trying to make it a loop, so you can re-do the program if that makes sense.... but for some reason it's looping the answer rather than the process, if that makse sense i hope


Code

package com.example.test;

import java.util.Scanner;

public class Main {

public static Scanner scanner = new Scanner(System.in);


public static void main(String[] args) {

System.out.println("Calculator program starting....");

System.out.println("Choose which numbers to operate on: ");
double num1 = scanner.nextDouble();
double num2 = scanner.nextDouble();
scanner.nextLine();


printOptions();


System.out.println("Choose your option: ");
int option = scanner.nextInt();

boolean quit = false;

while(!quit) {
switch (option) {
case 1:
System.out.println("You have chosen addition, " + num1 + " will be added to " + num2);
System.out.println("Your answer is: " + (num1 + num2));
break;
case 2:
System.out.println("You have chosen subtraction, " + num1 + " will be subtracted from " + num2);
System.out.println("Your answer is " + (num1 - num2));
break;
case 3:
System.out.println("You have chosen multiplication, " + num1 + " will be multiplied by " + num2);
System.out.println("Your answer is " + (num1 * num2));
break;
case 4:
System.out.println("You have chosen division, " + num1 + " will be divided by " + num2);
System.out.println("Your answer is " + (num1 / num2));
break;
case 5:
quit = true;
break;

}
}


}




public static void printOptions() {
System.out.println("List of options: \n" +
"1: for addition \n" +
"2: for subtraction \n" +
"3: for multiplication \n" +
"4: for division \n" +
"5: to quit");
}


}



This post was edited by ferf on Jun 11 2019 08:00pm
Member
Posts: 31,388
Joined: Mar 25 2009
Gold: 99.00
Jun 11 2019 10:04pm
Code
package com.example.test;

import java.util.Scanner;

public class Main {

public static Scanner scanner = new Scanner(System.in);


public static void main(String[] args) {

System.out.println("Calculator program starting....");


boolean quit = false;

while(!quit) {

printOptions();
System.out.println("Choose which numbers to operate on: ");
double num1 = scanner.nextDouble();
scanner.nextLine();
double num2 = scanner.nextDouble();
scanner.nextLine();

System.out.println("Choose your option: ");
int option = scanner.nextInt();


switch (option) {
case 1:
System.out.println("You have chosen addition, " + num1 + " will be added to " + num2);
System.out.println("Your answer is: " + (num1 + num2));
break;
case 2:
System.out.println("You have chosen subtraction, " + num1 + " will be subtracted from " + num2);
System.out.println("Your answer is " + (num1 - num2));
break;
case 3:
System.out.println("You have chosen multiplication, " + num1 + " will be multiplied by " + num2);
System.out.println("Your answer is " + (num1 * num2));
break;
case 4:
System.out.println("You have chosen division, " + num1 + " will be divided by " + num2);
System.out.println("Your answer is " + (num1 / num2));
break;
case 5:
quit = true;
break;

}
}

}




public static void printOptions() {
System.out.println("List of options: \n" +
"1: for addition \n" +
"2: for subtraction \n" +
"3: for multiplication \n" +
"4: for division \n" +
"5: to quit");
}


}




Fixed things up a bit, now only have 1 minor bug:
'after 1st operation, if i wanna quit program, i gotta hit enter once or twice, before i can use case 5 to exit
Member
Posts: 308
Joined: Jun 7 2019
Gold: 13.37
Jun 12 2019 10:48am
can you clarify what you want in the last post?
Member
Posts: 8,992
Joined: Mar 24 2013
Gold: 18,115.00
Jun 12 2019 06:30pm
try

Code
int option = scanner.nextInt();
scanner.nextLine();


or one-liner:

Code
int option = Integer.parseInt(scanner.nextLine());


both of them are bad smell though
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll