Question

What is wrong with the following code fragment? AND Rewrite it so that it produces correct...

What is wrong with the following code fragment? AND Rewrite it so that it produces correct output.

Java Code:

if (total == MAX)

if (total < sum)

System.out.println(“total == MAX and < sum.”);

else

System.out.println(“total is not equal to MAX”);

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

Issue here is with curly braces. I have modified the code. It wil work as expected.

when you dont mention the curly braces if statement will treat like single statement. In our case, we did not mention the curly braces, so it treats like with in if condition there is if-else statement. That is the issue here.

if (total == MAX){

if (total < sum)

System.out.println(“total == MAX and < sum.”);

}

else

System.out.println(“total is not equal to MAX”);

Add a comment
Know the answer?
Add Answer to:
What is wrong with the following code fragment? AND Rewrite it so that it produces correct...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • use java and write in text a. Rewrite the following code segment using if statements. Assume...

    use java and write in text a. Rewrite the following code segment using if statements. Assume that grade has been declared as of type char. char grade= 'B';; switch (grade) { case 'A': System.out.println("Excellent"); break case 'B': System.out.println("Good"); default: System.out.println("you can do better”); } - b. write Java code that inputs an integer and prints each of its digit followed by ** e.gif the integer is 1234 then it should print 1**2**3**4 e.g. if the integer is 85 then it...

  • Something is wrong with each of the following pieces of code. Use A or B to...

    Something is wrong with each of the following pieces of code. Use A or B to label whether the error is from: A. Compiler ErrorB. Logical or Runtime Error Use minor edits to correct the code. int x System.out.println("value of x is:x) if (x820) System.out.println("Your number was even!") System.out.println("Your number is odd!") else double avg numl+num2+num3/3.0 if (x > y) f double max-x; else max-y: 5. double answer Math.sqrt (/25;

  • Given the following Java code fragment, what is output? int a, b; String c, d, e;...

    Given the following Java code fragment, what is output? int a, b; String c, d, e; String x = new String(“I LOVE”); String y = “java!”; a = x.length( ); System.out.println(“1) “ + a); b = y.length( ); System.out.println(“2) “ + b); c = y.toUpperCase( ); System.out.println(“3) “ + c); d = x.toLowerCase( ); System.out.println(“4) “ + d); e = x.concat(y); System.out.println(“5) “ + e);

  • What is the output of the following code fragment? int i = 1; while( i <=...

    What is the output of the following code fragment? int i = 1; while( i <= 5 ) if(i == 2 || i == 4) System.out.println(i + ":" + " is an even index) System.out.println("i: " + i); i++;

  • JAVA: Rewrite the following code to where the inputs are from a file. The file name...

    JAVA: Rewrite the following code to where the inputs are from a file. The file name will be from the input from the user scanner. CODE: import java.util.*; public class Q1 { public static int sumOD (int k) { int sumOD = 0; int in = k; while (in != 0) { int digitON = in % 10; sumOD += digitON; in /= 10; } return sumOD; }    public static void main(String[] args) { int n, i, k; System.out.println("Enter...

  • What is wrong with the following code fragment? char *src = "hello"; char *dst = (char...

    What is wrong with the following code fragment? char *src = "hello"; char *dst = (char *) malloc(strlen(src)); //too small! strcpy (dst, src); //work properly

  • Rewrite the C++ code below so the the following conditions are met: You may create any...

    Rewrite the C++ code below so the the following conditions are met: You may create any number of classes. You may edit the main() function. Remove the variable string type completely from the entire program. -- Note: Entire program includes all classes, all functions, and main(). Get rid of every if and if else statement completely from the entire program. -- Note: Entire program includes all classes, all functions, and main(). Hint: Use polymorphism. #include <iostream> #include <vector> #include <algorithm>...

  • (a)How many times does the code snippet given below display "Hello"? int x = 1; while...

    (a)How many times does the code snippet given below display "Hello"? int x = 1; while (x != 15) {    System.out.println ("Hello");    x++; } (b)What is the output of the following code fragment? int i = 1; int sum = 0; while (i <= 5) {    sum = sum + i;    i++; } System.out.println("The value of sum is " + sum); Quie 2 What is the output of the following snipped code? public class Test {...

  • Question 28 (1 point) ✓ Saved What does the code fragment print? double x = Math.sqrt...

    Question 28 (1 point) ✓ Saved What does the code fragment print? double x = Math.sqrt (3); double y = 4; if (x + x == y) { System.out.println(x); } else { System.out.println(y); } 4 1

  • java programming!!! Write the code fragment that will do the following: • Ask the user how...

    java programming!!! Write the code fragment that will do the following: • Ask the user how many numbers they want to enter • Declare and instantiate an array of doubles with as many elements as the number entered by the user • Write one 'for' loop to fill the array with data from the user • Write a second 'for' loop to calculate the sum of all of the numbers in the array • Print the calculated sum Note: Write...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT