import java.util.Scanner;
public class DrawTriangle
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter a height");
while (!scan.hasNextInt()) // while non-integers are present
{
scan.next();
System.out.println ("Bad input. Enter an integer.");
}
int input = scan.nextInt();
for (int x = 1; x <= input; x++)
{
for (int y = 0; y < input; y++)
{
System.out.print(" ");
for (int z = 1; z < y; z++)
{
System.out.print("x");
}
System.out.println();
}
}
}
}
I have to make a triangle of x's relating to the height specified by the user. Can't get it to work at all, any help would be appreciated.
Thanks!
Sorry should have clarified
I need it to look like this -
x
xxx
xxxxx
You don't need nested loop upto 3 levels. Just 2 levels are needed. One for traversing along columns, and one for traversing along rows.
So, change your loop to: -
for (int x = 1; x <= input; x++)
{
for (int y = 0; y < x; y++)
{
System.out.print("x ");
}
System.out.println();
}
UPDATE : -
For equilateral triangle, you would need to add one more loop to print spaces before x on the starting rows. Here's the code: -
for (int x = 1; x <= input; x++)
{
for (int y = 0; y < input - x; y++) {
System.out.print(" ");
}
for (int y = 0; y < x; y++) {
System.out.print("x ");
}
System.out.println();
}
Related
I am trying to make a array that generates random numbers and saves how many times each number was rolled. Then I am trying to create a histogram of the results but the consoles goes blank. I couldn't think of way to cleverly use a loop so I had to use a very crude solution.
import java.util.Random;
import java.util.Arrays;
public class DiceRolls {
public static void main(String[] args){
int[] diceRolls = new int [9];
Random rand = new Random();
for(int numberOfRolls = 0; numberOfRolls <= 20; numberOfRolls++){
int individualRolls = rand.nextInt(9);
diceRolls[individualRolls] ++;
}
for(int y = 0; y >= diceRolls[0]; y++){
System.out.print("*");
}
System.out.println(" ");
for(int y = 0; y >= diceRolls[1]; y++){
System.out.print("*");
}
System.out.println(" ");
for(int y = 0; y >= diceRolls[2]; y++){
System.out.print("*");
}
System.out.println(" ");
for(int y = 0; y >= diceRolls[3]; y++){
System.out.print("*");
}
System.out.println(" ");
for(int y = 0; y >= diceRolls[4]; y++){
System.out.print("*");
}
System.out.println(" ");
for(int y = 0; y >= diceRolls[5]; y++){
System.out.print("*");
}
System.out.println(" ");
for(int y = 0; y >= diceRolls[6]; y++){
System.out.print("*");
}
System.out.println(" ");
for(int y = 0; y >= diceRolls[7]; y++){
System.out.print("*");
}
System.out.println(" ");
for(int y = 0; y >= diceRolls[8]; y++){
System.out.print("*");
}
}
}
public static void main(String[] args){
int[] diceRolls = new int [9];
Random rand = new Random();
for(int numberOfRolls = 0; numberOfRolls <= 20; numberOfRolls++){
int individualRolls = rand.nextInt(9);
diceRolls[individualRolls] ++;
}
for(int j : diceRolls) {
for(int i = 0; i<j; i++) {
System.out.print("*");
}
System.out.println();
}
}
Any Questions?
You inverted you loop conditions. They should be:
y < diceRolls[?]
I am trying to write a program that outputs a Z pattern that is n number of * across the top, bottom, and connecting line using for loops.
Example:
Enter a number: 6
******
*
*
*
*
******
This is my current code, it's producing a half pyramid upside down.
import java.util.*;
public class ZShape {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = input.nextInt();
for (int x = 0; x <= n; x++) {
for (int y = n; y >= 1; y--) {
if (y > x) {
System.out.print("* ");
}
else
System.out.print(" ");
}
System.out.println();
}
}
}
This is the logic in the following code:
Loop over each row of the output (so from 0 to n excluded so that we have n rows)
Loop over each column of the output (so from 0 to n excluded so that we have n columns)
We need to print a * only when it is the first row (x == 0) or the last row (x == n - 1) or the column is in the opposite diagonal (column == n - 1 - row)
Code:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = input.nextInt();
for (int row = 0; row < n; row++) {
for (int column = 0; column < n; column++) {
if (row == 0 || row == n - 1 || column == n - 1 - row) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
Sample output for n = 6:
******
*
*
*
*
******
(Note that this output has trailing white-spaces for each row, you did not specify whether they should be included, but it is easy to remove them by adding another check).
How about using three loops instead?
for (int x = 0; x < n; x++) {
System.out.print("*");
}
System.out.println();
for (int x = n-3; x >= 0; x--) {
for (int y = x; y >= 0; y--) {
System.out.print(" ");
}
System.out.println("*");
}
for (int x = 0; x < n; x++) {
System.out.print("*");
}
public class Star {
public static void main(String[] args) {
for (int i = 0; i <=4; i++) {
for (int j = 0; j <=4; j++)
{
if (i==4 || (i+j)==4 || i==0)
{
System.out.print(" * ");
}
else
{
System.out.print(" ");
}
}
System.out.println(" ");
}
}
}
Here is a logic to print Z shape:
when i = 1, then the First line will print only "#"
when i = n, then the Last line will print in same way by "#" only
when i = j, that means it will execute one diagonal line by "#" only
Code:
public static void main(String[] args) {
Scanner scr = new Scanner(System.in);
int n = scr.nextInt();
for (int i = 1; i <= n; i++) {
for(int j = n; j >= 1; j--) {
if (i == 1 || i == n || i == j) {
System.out.print("# ");
}
else {
System.out.print(" ");
}
}
System.out.println();
}
}
i need help.
i try to make a pyramids that use two alphabet like this:
O
OO
OXO
OXXO
OXXXO
OXXXXO
OOOOOOO
well, because i still new with java
i'm stuck here
O
OX
OXX
OXXX
OXXXX
OXXXXX
OXXXXXX
Here is my code:
import java.util.Scanner;
class Xx {
public static void main(String[] args) {
int x;
Scanner Sc = new Scanner(System.in);
System.out.print("Line of Pyramid : ");
x = Sc.nextInt();
for (int y = 0; y <= x; y++) {
System.out.print("O");
for (int z = 1; z <= y; z++) {
System.out.print("X");
}
System.out.println();
}
}
}
I would start with a method to repeat a given character n times, something like
static String repeat(char ch, int n) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
sb.append(ch);
}
return sb.toString();
}
then you can call it to build your pyramid. Something like
int x = Sc.nextInt();
System.out.println("O");
System.out.println("OO");
for (int y = 1; y <= x; y++) {
System.out.print("O");
char ch = 'X';
if (y == x) {
ch = 'O';
}
System.out.print(repeat(ch, y));
System.out.println("O");
}
Which (when I run it with 5) generates (the requested),
O
OO
OXO
OXXO
OXXXO
OXXXXO
OOOOOOO
int x;
Scanner Sc = new Scanner(System.in);
System.out.print("X : ");
x = Sc.nextInt();
for (int y = 0; y <= x; y++)
{
System.out.print("O");
for (int z = 1; z <= y; z++)
{
System.out.print("X");
if(z == y)
System.out.print("O");
}
System.out.println();
}
for(int i=0; i<= x+2; i++)
{
System.out.print("O");
}
Output:
X : 5
O
OXO
OXXO
OXXXO
OXXXXO
OXXXXXO
OOOOOOOO
import java.util.Scanner;
class Xx {
public static void main(String[] args) {
int x;
Scanner Sc = new Scanner(System.in);
System.out.print("X : ");
x = Sc.nextInt();
for (int y = 0; y <= x; y++) {
System.out.print("O");
for (int z = 1; z <= y; z++) {
if(z==y||y==x)
System.out.print("O");
else
System.out.print("X");
}
System.out.println();
}
}
}
Explanation
you can identify the coloumn edge using z==y condition, and base coloumn should fill with 'O' , for check that y==x condition added in your code , will give your expected output
Whenever I input x (number of empoyee) as any number it always short in 1 loop when asking the employee salary. And if i put x as 1 it doesn't ask me.
Need help how to fix it.
class salary {
public static void main(String[] args) {
int x = 0;
int y;
System.out.println("Enter how many employee");
x = EasyIn.getInt();
for (int i = 1; i < x; ++i) {
System.out.println("Enter the salary of employee " + i);
y = EasyIn.getInt();
if (y < 20000) {
System.out.println("This employee Bonus Rate is 7%");
}
}
}
}
You have an error in your for loop. It should be:
for(int i = 0; i < x; ++i)
or:
for(int i = 1; i <= x; ++i)
What is EasyIn?
There might also be an issue with your EasyIn class.
Your problem is here
for (int i = 1; i < x; ++i)
Think about the terminating condition of the for loop.
Try something like this -
// start at 0.
for (int i = 0; i < x; ++i)
{
// add one for display.
System.out.println("Enter the salary of employee " + (1+i));
y = EasyIn.getInt();
if(y < 20000)
{
System.out.println("This employee Bonus Rate is 7%");
}
}
you are starting i from 1 instead of zero so it will always short by 1.
Simply change int i=1; to int i=0;
I'm attempting to get spaces at the beginning of every other line of the output of this program:
import java.util.Scanner;
public class Checkerboard
{
public static void main(String[] args)
{
int num;
Scanner input = new Scanner(System.in);
System.out.println ("What is the integer?");
num = input.nextInt();
for (int x = 0; x < num; x++)
{
for (int y = 0; y < num; y++)
{
System.out.print("A ");
}
System.out.println(" ");
}
}
}
The output of this program, if the user enters "4" for example, is:
A A A A A
A A A A A
A A A A A
A A A A A
I'm trying to get it to look like this:
A A A A A
A A A A A
A A A A A
A A A A A
Print the extra space only for even values of x
for (int x = 0; x < num; x++)
{
for (int y = 0; y < num; y++)
{
System.out.print("A ");
}
System.out.println("");
if (x % 2 == 0){
System.out.print(" ");
}
}
To be more explicit, the % operator (modulus) will return the remainder of a division operation. So any even number modulu 2 will return 0. Otherwise it would return 1. 13 % 10 == 3 because 13 / 10 = 1 remainder of 3.