3 Replies - 218 Views - Last Post: 02 October 2012 - 04:34 AM Rate Topic: -----

#1 dulvinrivindu  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 51
  • Joined: 25-September 12

The loop doesnt work. Can I please know what is wrong in it?

Posted 02 October 2012 - 03:03 AM

import java.util.Scanner;
public class Calculator {
	float add(float num1,float num2){
		return num1+num2;
	}
	float devide(float num1,float num2){
		return num1/num2;
	}
	float multip(float num1, float num2){
		return num1*num2;
	}
	float sub(float num1,float num2){
		return num1-num2;
	}
	public static void main (String args[]){
		float num1=0,num2=0,fin=0;
		int choice=0;
		boolean result;
		Scanner scn = new Scanner(System.in);
		Calculator calc = new Calculator();
		do{
		System.out.println("Enter Your Choice");
		System.out.println("1-Add \n"+"2-Substract \n"+"3-Devide \n"+"4-Multiply");
		choice = scn.nextInt();
		System.out.println("Enter Number 1");
		num1= scn.nextFloat();
		System.out.println("Enter number 2");
		num2= scn.nextFloat();
		try{
			switch(choice){
			case 1:fin = calc.add(num1, num2);
			break;
			case 2:fin= calc.sub(num1, num2);
			break;
			case 3:fin = calc.devide(num1, num2);
			break;
			case 4:fin = calc.multip(num1, num2);
			break;
			default: System.out.println("IO Error");
			break;
			}
		}
		catch (Exception e){
			System.out.println("Enter a Valid number");
		}
		System.out.println(fin);
		System.out.println("Do you want to continue");
		result = scn.hasNextBoolean();
		}while(result = true);
		scn.close();
	}
		
}



Is This A Good Question/Topic? 0
  • +

Replies To: The loop doesnt work. Can I please know what is wrong in it?

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: The loop doesnt work. Can I please know what is wrong in it?

Posted 02 October 2012 - 03:23 AM

Please give details other than the thread title and code. Describe "doesn't work." What are you expecting? What's happening instead? Provide a sample run (copied and pasted from your console) and a describe how it's different than desired. If you're getting errors, copy and paste those too.
Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: The loop doesnt work. Can I please know what is wrong in it?

Posted 02 October 2012 - 04:28 AM

while(result = true);

is equivalent to
   result = true;
   } while(result);

you probably want

   } while(result == true);   // with 2 ==


but after having check if hasNextBolean() you will have to read it :)
Was This Post Helpful? 1
  • +
  • -

#4 dulvinrivindu  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 51
  • Joined: 25-September 12

Re: The loop doesnt work. Can I please know what is wrong in it?

Posted 02 October 2012 - 04:34 AM

Thanks everybody! Thanks alot!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1