whats the error i think its syntax ior semicolon:…
- User
whats the error i think its syntax ior semicolon: //*************************************** // Dio // // Purpose: // // Made by Michael Ortola // R0: 30-Aug-2023 Ortola - Beginning //*************************************** import java.util.Scanner; public class Dio_MAO { public static void main(String[] args) { Scanner scan = new Scanner(System.in); long et = System.nanoTime(); System.out.print("Enter range for k: "); int krange = scan.nextInt();
System.out.print("Enter span for x, y, z: "); int span = scan.nextInt(); int solution = 0; for (int k = 0, k <= krange, k++) { boolean solutionFound = false; for (int x = -span, x <= span && !solutionFound, x++) { for (int y = -span, y <= span && !solutionFound, y++) { int z = (int) Math.cbrt(k - Math.pow(x,3) - Math.pow(y,3)); if (Math.pow(z,3) == k - Math.pow(x,3) - Math.pow(y,3)) { System.out.print("k: + " + k + ", x: " + x + ", y: " + y + ", z: " + z); solutionFound = true; solution++; } } } if (!solutionFound) { System.out.print("k: + " + k + " Not found!"); } } et = System.nanoTime() - et; System.out.print("Solution(s) found for " + solution + " values of k."); System.out.printf("\nElapsed time: %.2E nsecs", (double) et);} }
- Assistant
There are a few errors in your code:
-
In your for loop, the condition should use
<instead of<=. Changefor (int k = 0, k <= krange, k++)tofor (int k = 0; k < krange; k++). -
In your nested for loops, you are using the comma operator instead of the logical && operator. Change
for (int x = -span, x <= span && !solutionFound, x++)tofor (int x = -span; x <= span && !solutionFound; x++)andfor (int y = -span, y <= span && !solutionFound, y++)tofor (int y = -span; y <= span && !solutionFound; y++). -
The Math.cbrt() method returns a double, but you are trying to assign it to an int variable. Change
int z = (int) Math.cbrt(k - Math.pow(x,3) - Math.pow(y,3))todouble z = Math.cbrt(k - Math.pow(x,3) - Math.pow(y,3)). -
In your if statement, you are comparing a double to an int using the == operator. This may not give you the desired result due to floating-point precision. Instead, you can use Math.round() to round the double to the nearest integer and compare it to the int value. Change
if (Math.pow(z,3) == k - Math.pow(x,3) - Math.pow(y,3))toif (Math.round(Math.pow(z,3)) == k - Math.pow(x,3) - Math.pow(y,3)). -
In your System.out.print statements, you are concatenating the strings incorrectly. Change
System.out.print("k: + " + k + ", x: " + x + ", y: " + y + ", z: " + z)toSystem.out.println("k: " + k + ", x: " + x + ", y: " + y + ", z: " + z)andSystem.out.print("k: + " + k + " Not found!")toSystem.out.println("k: " + k + " Not found!").
After making these changes, your code should compile and run without any syntax errors.
-
- User
Dio_MAO.java:25: error: ';' expected for (int k = 0, k <= krange, k++) ^ Dio_MAO.java:25: error: illegal start of expression for (int k = 0, k <= krange, k++) ^ Dio_MAO.java:25: error: ';' expected for (int k = 0, k <= krange, k++) ^ Dio_MAO.java:25: error: illegal start of expression for (int k = 0, k <= krange, k++) ^ Dio_MAO.java:25: error: ')' expected for (int k = 0, k <= krange, k++) ^ Dio_MAO.java:25: error: illegal start of expression for (int k = 0, k <= krange, k++) ^ Dio_MAO.java:28: error: ';' expected for (int x = -span, x <= span && !solutionFound, x++) ^ Dio_MAO.java:28: error: illegal start of expression for (int x = -span, x <= span && !solutionFound, x++) ^ Dio_MAO.java:28: error: ';' expected for (int x = -span, x <= span && !solutionFound, x++) ^ Dio_MAO.java:28: error: illegal start of expression for (int x = -span, x <= span && !solutionFound, x++) ^ Dio_MAO.java:28: error: ')' expected for (int x = -span, x <= span && !solutionFound, x++) ^ Dio_MAO.java:28: error: not a statement for (int x = -span, x <= span && !solutionFound, x++) ^ Dio_MAO.java:28: error: ';' expected for (int x = -span, x <= span && !solutionFound, x++) ^ Dio_MAO.java:28: error: ';' expected for (int x = -span, x <= span && !solutionFound, x++) ^ Dio_MAO.java:30: error: ';' expected for (int y = -span, y <= span && !solutionFound, y++) ^ Dio_MAO.java:30: error: illegal start of expression for (int y = -span, y <= span && !solutionFound, y++) ^ Dio_MAO.java:30: error: ';' expected for (int y = -span, y <= span && !solutionFound, y++) ^ Dio_MAO.java:30: error: illegal start of expression for (int y = -span, y <= span && !solutionFound, y++) ^ Dio_MAO.java:30: error: ')' expected for (int y = -span, y <= span && !solutionFound, y++) ^ Dio_MAO.java:30: error: not a statement for (int y = -span, y <= span && !solutionFound, y++) ^ Dio_MAO.java:30: error: ';' expected for (int y = -span, y <= span && !solutionFound, y++) ^ Dio_MAO.java:30: error: ';' expected for (int y = -span, y <= span && !solutionFound, y++)
- Assistant
Apologies for the mistake. In the for loop, you need to use a semicolon (;) to separate the three parts of the loop statement: initialization, condition, and increment/decrement.
Here's the corrected code for the for loops:
for (int k = 0; k <= krange; k++) { boolean solutionFound = false; for (int x = -span; x <= span && !solutionFound; x++) { for (int y = -span; y <= span && !solutionFound; y++) { int z = (int)Math.cbrt(k - Math.pow(x,3) - Math.pow(y,3)); if (Math.pow(z, 3) == k - Math.pow(x, 3) - Math.pow(y, 3)) { System.out.println("k: " + k + ", x: " + x + ", y: " + y + ", z: " + z); solutionFound = true; solution++; } } } if (!solutionFound) { System.out.println("k: " + k + " Not found!"); } }Make sure to replace the previous for loops with this corrected version.