Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import java.util.*; public class Program { public static void main(String[] args

ID: 3926302 • Letter: I

Question

import java.util.*; public class Program { public static void main(String[] args) { Scanner input = new Scanner(System.in); String x="X"; int amount; System.out.println("Enter amount"); amount=input.nextInt(); System.out.println("Amount is: "+amount); for(int i=1; i<=amount; i++) { System.out.println(x); for(int j=1; j<=i; j++) { System.out.print(x); } } } }

Why does It print out the last set of X's twice? I am doing basic Java so please try to explain using the most basic codes. import java.util.*; public class Program { public static void main(String[] args) { Scanner input = new Scanner(System.in); String x="X"; int amount; System.out.println("Enter amount"); amount=input.nextInt(); System.out.println("Amount is: "+amount); for(int i=1; i<=amount; i++) { System.out.println(x); for(int j=1; j<=i; j++) { System.out.print(x); } } } }

Why does It print out the last set of X's twice? I am doing basic Java so please try to explain using the most basic codes. import java.util.*; public class Program { public static void main(String[] args) { Scanner input = new Scanner(System.in); String x="X"; int amount; System.out.println("Enter amount"); amount=input.nextInt(); System.out.println("Amount is: "+amount); for(int i=1; i<=amount; i++) { System.out.println(x); for(int j=1; j<=i; j++) { System.out.print(x); } } } }

Why does It print out the last set of X's twice? I am doing basic Java so please try to explain using the most basic codes.

Explanation / Answer

Hi,

I have modified the code. it is working as expected now. Highlighted the code changes below.

Issue here is with you have written this statement twice System.out.println(x); which causes the issue.

Program.java

import java.util.*;
public class Program
{
   public static void main(String[] args) {
   Scanner input = new Scanner(System.in);
   String x="X";
   int amount;
  
   System.out.println("Enter amount");
  
   amount=input.nextInt();
   System.out.println("Amount is: "+amount);
  
   for(int i=1; i<=amount; i++)
   {
      
   System.out.println();
  
  
   for(int j=1; j<=i; j++)
   {
  
   System.out.print(x);
      
      
  
      
   }
   }
      
   }
}

Output:

Enter amount
10
Amount is: 10

X
XX
XXX
XXXX
XXXXX
XXXXXX
XXXXXXX
XXXXXXXX
XXXXXXXXX
XXXXXXXXXX