Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How do I stop my answer from looping?

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default How do I stop my answer from looping?

    I have achieved the output I want for my program but the answer just continuously loops I have tried to stop the loop but I can't seem to figure out how to. Could someone help me out please?



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

    import java.util.Scanner;

    /**
    *
    * @author Adam
    */
    public class PowerSquare {

    public static void main(String[] args) {

    double numbers;
    double num1;
    double num2;

    Scanner scan = new Scanner(System.in);

    System.out.println("Please enter 2 numbers:");
    num1 = scan.nextDouble();


    for (double i = num1; i <= num1 {
    System.out.println("cube of" + i + "is" + i * i * i);

    num2 = scan.nextDouble();
    for (double a = num2; a <= num2{
    System.out.println("square of" + a + "is" + Math.sqrt(a));




    if (num1 > num2 && num2< num1){
    System.out.print("The largest number is" + num1);
    }
    else if (num2 > num1 && num1 < num2)
    System.out.print("The largest number is" + num2);
    }

    {
    }



    }}}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,619
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: How do I stop my answer from looping?

    Please post the output that shows the problem.

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting

    Where do you change the value of the variable that is controlling the looping?
    If you don't change it then the loop will never end.

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    18
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How do I stop my answer from looping?

    You code contains lot of weird thing i don't know why you are using the for loop and your IF condition is completely weird.
    It seems like you are trying to take two value and wants to display the cube of both the numbers and the largest of the two numbers. You can't do that without using the for loop at all.

    	public static void main(String args[]) {
     
    		double num1;
    		double num2;
     
    		Scanner scan = new Scanner(System.in);
     
    		System.out.println("Please enter 2 numbers:");
    		num1 = scan.nextDouble();
    		System.out.println("cube of" + num1 + "is" + num1 * num1 * num1);
    		num2 = scan.nextDouble();
    		System.out.println("cube of" + num2 + "is" + num2 * num2 * num2);
     
    		if (num1 > num2) {
    			System.out.print("The largest number is" + num1);
    		} else 
    			System.out.print("The largest number is" + num2);
     
    		}

Similar Threads

  1. Pls Answer...
    By sachin.garde in forum Exceptions
    Replies: 4
    Last Post: October 31st, 2011, 02:38 AM
  2. What's ur answer?
    By Tanmaysinha in forum The Cafe
    Replies: 6
    Last Post: October 12th, 2011, 06:26 AM
  3. Pls answer( urgent)
    By sar in forum Java Theory & Questions
    Replies: 1
    Last Post: September 20th, 2011, 11:28 AM
  4. [SOLVED] Make it stop looping! please. :)
    By Hallowed in forum What's Wrong With My Code?
    Replies: 11
    Last Post: May 7th, 2011, 11:20 AM
  5. need answer for these
    By satishbs in forum Java Theory & Questions
    Replies: 2
    Last Post: April 15th, 2011, 01:46 AM