I need the solution for FixDebugSix3.java and it\'s not posted in Chegg Answers.
ID: 3759222 • Letter: I
Question
I need the solution for FixDebugSix3.java and it's not posted in Chegg Answers. The coding is from the file listed in Java Programming Joyce Farrel 7th Edition, Problem is on Page # 346, under Debugging Exercise. Here is a copy of the file:
// DebugSix3.java
// Prompt user for value to start
// Value must be between 1 and 20 inclusive
// At command line, count down to blastoff
// With a brief pause between each displayed value
import javax.swing.*;
public class DebugSix3
{
public static void main(String[] args)
{
String userNumString;
int userNum, val;
final int MIN = 1;
final int MAX = 20;
userNumString = JOptionPane.showInputDialog(null,
"Enter a number between + MIN + " and " + MAX + inclusive");
userNum = Integer.parseInt(userNumString);
while(userNum < MIN || userNum > MAX)
{
userNumString = JOptionPane.showInputDialog(null,
"Number out of range" +
" Enter a number between " + MIN + and " + MAX + " inclusive");
userNum = Integer.parseInt(userNuString);
}
for(val = userNum; val < 0; --val)
{
System.out.print(val + " ");
for(int x = 0; x < 100000; ++x)
for(int y = 0; y < 10000; ++y);
// Adjust these numbers for faster or slower performance
}
System.out.println("Blastoff!");
}
}
Explanation / Answer
Here is the solution for FixDebugSix3.java
This is the provided code by you
import javax.swing.*;
public class DebugSix3
{
public static void main(String[] args)
{
String userNumString;
int userNum, val;
final int MIN = 1;
final int MAX = 20;
userNumString = JOptionPane.showInputDialog(null,
"Enter a number between + MIN + " and " + MAX + inclusive");
userNum = Integer.parseInt(userNumString);
while(userNum < MIN || userNum > MAX)
{
userNumString = JOptionPane.showInputDialog(null,
"Number out of range" +
" Enter a number between " + MIN + and " + MAX + " inclusive");
userNum = Integer.parseInt(userNuString);
}
for(val = userNum; val < 0; --val)
{
System.out.print(val + " ");
for(int x = 0; x < 100000; ++x)
for(int y = 0; y < 10000; ++y);
// Adjust these numbers for faster or slower performance
}
System.out.println("Blastoff!");
}
}
CODE:
for(int x =0; x<10000; ++x)
for (int y=0; y <10000; ++y){
//{ //extra bracket
System.out.println("Blastoff!");
//} //extra bracket
}
This will run an insane amount of times. Are you sure that this is what you want to do?
Also, the brackets are off
I don't think you even want those two loops there, you just want it to print "Blastoff", Then
Also, here:
for(val = userNum; val <0;--val){
System.out.println (val + " ");
val++; // it works until here for now
}
Why do you set the condition to while val < 0 ? it would seem to me you would want to count down, and your --val step seems to confirm this. Make it val > 0 or possibly val >= 0 if you want to include 0.
Also, why are you incrementing val inside the loop? you increment it by 1, then immediately decrement it by 1. Infinite loop material right there. Remove the val++; line :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.