Question

java: can anyone explain. why does my code print out ##### and ***** instead of just...

java: can anyone explain. why does my code print out ##### and ***** instead of just #####?

int a=1, b=2, c=3;
  
if(a<b)
System.out.println("####");
else
System.out.println("&&&&");
System.out.println("****");

Homework Answers

Answer #1

Answer :

int a=1, b=2, c=3;
  

if(a<b) // STATEMENT 1
System.out.println("####");    // STATEMENT 2
else     // STATEMENT 3
System.out.println("&&&&");    // STATEMENT 4
System.out.println("****");    // STATEMENT 5

EXPLANATION :

Here, STATEMENT 1,  holds true ie. ( 1 < 2 ) , then the  STATEMENT 2 executes . So, it will print #####

And , STATEMENT 3 and STATEMENT 4 , will not executed as " if " condition holds true. But STATEMENT 5 will be displayed , because there is no braces around the else statement , hence by default only the STATEMENT 4 will be considered as a part of else statement  and not the STATEMENT 5.

Hence, it will also print *****

Hence, it will print both ##### and ***** instead of just #####.

BUT , IF WE WANT TO PRINT ONLY #####. ( We have to put curly braces around else statment ).

THE CODE changes to :

int a = 1, b = 2, c = 3;

if (a < b)
   System.out.println("####");
else {
   System.out.println("&&&&");
   System.out.println("****");
}

OUPTUT OF UPDATED CODE :

Know the answer?
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
Similar Questions
In Java programming language 11.Create the code for a for loop to print the numbers 1  through...
In Java programming language 11.Create the code for a for loop to print the numbers 1  through 5 inclusive vertically so the output is 1 2 3 4 5 11b What is the output of the following               OUTPUT int i=3; while(int i >0) { System.out.println(i); i--; } 11c What is the output of the following               OUTPUT for(int i=3;i<10;i++) System.out.println(3*i); 11d Create the line of code to print the numbers 10 through 1 decreasing by 1 each time 11e Create the line of code...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
This is Java programing.Modify this Java code,so that each franchise can assign and print their own...
This is Java programing.Modify this Java code,so that each franchise can assign and print their own burger price.The programing have at least three franchises. (Use abstract) class newBurger { public newBurger() { } public void salmonBurger(){    System.out.println("salmonBurger $5.99");    System.out.println("Kcal: 294"); } public void clamBurger(){    System.out.println("clamBurger $4.99");    System.out.println("Kcal: 200"); } public void oysterBurger(){    System.out.println("oysterBurger $3.50");    System.out.println("Kcal: 125"); } } class franchise1 extends newBurger { String name = "franchise #1"; public franchise1() { } } public...
In Java 1. What will be the value of x after the following section of code...
In Java 1. What will be the value of x after the following section of code executes: int x = 3; if (x > 3)     x = x – 2; else     x = x + 2; A. 1 B.3    C.5              D.7 2. What will be the value of y after the following section of code executes: int y = 5, z = 3; if (y > 4){      z = 2;      y = y – 1;...
This is the java code that I have, but i cannot get the output that I...
This is the java code that I have, but i cannot get the output that I want out of it. i want my output to print the full String Form i stead of just the first letter, and and also print what character is at the specific index instead of leaving it empty. and at the end for Replaced String i want to print both string form one and two with the replaced letters instead if just printing the first...
How can i make this lunix code print 3 numbers in reverse it must be in...
How can i make this lunix code print 3 numbers in reverse it must be in printStars format and no loops Here is the code i have but can figure out how to reverse the numbers #include<stdio.h> void printStars(int n) { if (n>0){ printf(""); printStars(n-1); } } int main() { int n; int b; int c; printf("Enter 3 numbers to reverse "); scanf("%d",&n,&b,&c); printf("your reversed numbers are %d",n); printStars(n); return 0;
Java, I get a index out of bounds exception with this method, can you spot where...
Java, I get a index out of bounds exception with this method, can you spot where the method goes out of bounds from this code? public void showEarliestAttack(){ String earliestAttack; for (int i = 0; i < attackArray.size(); i++) { earliestAttack= attackArray.get(i).toString(); if(attackArray.get(i+1).getDate().compareTo(attackArray.get(i).getDate())<0){ earliestAttack = attackArray.get(i+1).toString(); } System.out.println(earliestAttack); } }
I wrote the following java code with the eclipse ide, which is supposed to report the...
I wrote the following java code with the eclipse ide, which is supposed to report the number of guesses it took to guess the right number between one and five. Can some repost the corrected code where the variable "guess" is incremented such that the correct number of guesses is displayed? please test run the code, not just look at it in case there are other bugs that exist. import java.util.*; public class GuessingGame {    public static final int...
question : Take the recursive Java program Levenshtein.java and convert it to the equivalent C program....
question : Take the recursive Java program Levenshtein.java and convert it to the equivalent C program. Tip: You have explicit permission to copy/paste the main method for this question, replacing instances of System.out.println with printf, where appropriate. You can get the assert function from assert.h. Try running the Java program on the CS macOS machines. You can compile and run the program following the instructions discussed in class. Assertions are disabled by default. You can enable assertions by running java...
This is in java and you are not allowed to use Java API classes for queues,...
This is in java and you are not allowed to use Java API classes for queues, stacks, arrays, arraylists and linkedlists. You have to write your own implementations for them. You should construct a BST by inserting node values starting with a null tree. You can re-use the code for the insert method given in the sample code from the textbook. -insert method is provided below Your code should have a menu driven user interface at the command line with...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT