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);
}
{
}
}}}
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.
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.
Code :
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);
}