Jump to content

Java squaring not giving correct result

vectorx5000
Go to solution Solved by Enderman,

I am learning java and am working on this calculator. When I give the input "1",  "12",  "sq" it gives the result 14.0 when it should be 144. Can anyone help me? Also, I would like tips about my code in general. I am very new, so I am expecting lots of criticism. I called my scanner krimz just so it is easy to remember.

package calc;
import java.util.Scanner;
import java.lang.Math;

public class Calculator {
	public static void main(String args[]){
		System.out.println("Press '1' for single number operations or '2' for 2 number operation.");
		
		Scanner krimz = new Scanner(System.in);
		int type;
		type = krimz.nextInt();
		
		switch(type){
			case 1:
				int num;
				double answer1;
				String operation1;
				System.out.println("Single number operation calculator: ");
				
				System.out.println("Enter number: ");
				num = krimz.nextInt();
				
				System.out.println("Enter operation: [sqrt, sq]");
				operation1 = krimz.next();
				
				switch(operation1){
					case "sqrt":
						answer1 = Math.sqrt(num);
						System.out.println("Your answer is " + answer1);
					break;
					case "sq":
						answer1 = num ^ 2;
						System.out.println("Your answer is " + answer1);
					break;
				}
			break;
			case 2:
				int first, second, answer2;
				String operation;
				System.out.println("Two number calculation calculator: ");
				System.out.println("Enter first number: ");
				first = krimz.nextInt();
				System.out.println("Enter second number: ");
				second = krimz.nextInt();
				System.out.println("Press the operation you want: ");
				operation = krimz.next();
				switch(operation){
					case "+":
						answer2 = first + second;
						System.out.print("Your answer is " + answer2);
					break;
					case "-":
						answer2 = first - second;
						System.out.print("Your answer is " + answer2);
					break;
					case "*":
						answer2 = first * second;
						System.out.print("Your answer is " + answer2);
					break;
					case "/":
						answer2 = first / second;
						System.out.print("Your answer is " + answer2);
					break;
				}
			break;
		}
	}
}

 

My Rig:

CPU: Intel i7 5820K @4.3 GHz; Mobo: MSI X99A SLI Plus; Cooler: Cryorig H5 Ultimate; RAM: 16GB HyperX Fury Black; GPU: MSI R9 380 4GB; Case: Fractal Design Define R5 Black w/ Window; SSD: Samsung 850 EVO 256GB and 500GB; HDD: WD Blue 1TB; PSU: EVGA 750W P2

Peripherals:

Monitor: LG 29UM57; Headset: HyperX Cloud II; Keyboard: Gamdias Hermes Mechanical; Mouse: Zowie EC2-A

 

Link to comment
Share on other sites

Link to post
Share on other sites

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
Share on other sites

Link to post
Share on other sites

you're doing binary XOR for 12 and 2 which is indeed 14

you should instead use

 answer1=Math.Power(num,2);
	//or do a multiplication
	answer1=num*num; 

CPU: Intel i7 5820K @ 4.20 GHz | MotherboardMSI X99S SLI PLUS | RAM: Corsair LPX 16GB DDR4 @ 2666MHz | GPU: Sapphire R9 Fury (x2 CrossFire)
Storage: Samsung 950Pro 512GB // OCZ Vector150 240GB // Seagate 1TB | PSU: Seasonic 1050 Snow Silent | Case: NZXT H440 | Cooling: Nepton 240M
FireStrike // Extreme // Ultra // 8K // 16K

 

Link to comment
Share on other sites

Link to post
Share on other sites

37 minutes ago, Enderman said:

Thank you. I forgot how to raise to the power of in java.

My Rig:

CPU: Intel i7 5820K @4.3 GHz; Mobo: MSI X99A SLI Plus; Cooler: Cryorig H5 Ultimate; RAM: 16GB HyperX Fury Black; GPU: MSI R9 380 4GB; Case: Fractal Design Define R5 Black w/ Window; SSD: Samsung 850 EVO 256GB and 500GB; HDD: WD Blue 1TB; PSU: EVGA 750W P2

Peripherals:

Monitor: LG 29UM57; Headset: HyperX Cloud II; Keyboard: Gamdias Hermes Mechanical; Mouse: Zowie EC2-A

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×