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[?]
Related
I am new to java, I want to print a reversed star pattern based on the coordinate. After I set the coordinate, it will then print the star pattern
import java.util.Scanner;
public class coorTest {
public static void main(String[] args) {
int max = 5;
for (int y = 0; y < max; y += 2) {
int left_spacing = (int) Math.floor(y * 1.0 / 2.0);
for (int space = 0; space < left_spacing; space++) {
System.out.print(" ");
}
for (int x = 0; x < (max - y); x++) {
System.out.print("x");
}
System.out.println();
}
}
}
Try this. I am using (0,0) as a first coordinate.
public static void printStar(int x, int y) {
int starCount = 5;
int space = 0;
int count = 0;
// y-axis line move
for(int i=0; i<y; i++) {
System.out.println();
}
for (int i = 0; i < starCount; i++) {
// x-axis space move
for(int xAxis=0; xAxis<x; xAxis++) {
System.out.print(" ");
}
for (int j = 0; j < starCount; j++) {
if (count < space) {
System.out.print(" ");
count++;
continue;
} else if (j >= starCount - count) {
System.out.print(" ");
} else {
System.out.print("*");
}
}
System.out.println();
space++;
count = 0;
}
}
import java.util.Random;
import java.util.Scanner;
public class BarChart {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
Random rgen = new Random();
int number;
int ast;
int count1 = 0;
int count2 = 0;
int count3 = 0;
System.out.print("How many integers should be generated? ");
number = keyboard.nextInt();
for (int i = 1; i <= number; i++){
ast = rgen.nextInt(3);
count1++;
System.out.print("*");
}
for (int i = 1; i <= number; i++) {
ast = rgen.nextInt(2);
count2++;
System.out.print("*");
}
for (int i = 1; i <= number; i++) {
ast = rgen.nextInt(1);
count3++;
System.out.print("*");
}
number = count1 + count2 + count3;
}
}
I am new to this and I need to make a program using for loops that asks the user for a positive number, then 3 bars of asterisks get printed out that all equal the number the user types. For example:
How many integers should be generated? 32
************** 14
****** 6
************ 12
You can try this:
count1 = rgen.nextInt(number);
for(int x=0; x<count1; x++) {
system.out.print("*");
}
system.out.println(count1);
count2 = rgen.nextInt(number-count1);
for(int x=0; x<count2; x++) {
system.out.print("*");
}
system.out.println(count2);
count3 = number-(count1+count2);
for(int x=0; x<count3; x++) {
system.out.print("*");
}
system.out.println(count3);
Hope this works.
So yeah, I'm now working on nested loops but I think I'm stuck on somewhere because my desired output is:
*
**
***
**
*
And here's my code:
//intro here
int x = 0;
System.out.print("Enter: ");
x = var.nextInt();
for(int a = 1; a <= x; a++){
for(int b = 0; b <= x - a; b++){
System.out.print("");
}
for(int c = 0; c < a; c++){
System.out.print("*");
}
System.out.println();
}
}
}
but the output happening was:
*
**
***
I don't know now what to do, does it need another for with a d-- or something?
Yes. You need another for loop for print decreasing number of stars. This should work well!
import java.util.Scanner;
public class Nested {
public static void main(String[] args) {
Scanner var = new Scanner(System.in);
int x = 0;
System.out.print("Enter: ");
x = var.nextInt();
//create star rows from 1 to 6
for (int a = 1; a <= x; a++) {
for (int b = 1; b <= a; b++) {
System.out.print("*");
}
System.out.println();
}
//create start rows from 5 to 1
for (int a = x - 1; a > 0; a--) {
for (int b = a; b > 0; b--) {
System.out.print("*");
}
System.out.println();
}
}
}
int x = 0;
System.out.print("Enter: ");
x = var.nextInt();
for(int a = 1; a <= x; a++){
for(int b = 0; b <= x - a; b++){
System.out.print(""); //this code block do nothing in ur case
}
for(int c = 0; c < a; c++){
System.out.print("*");
}
System.out.println();
}
}
you can achieve the desired output by:
int x = 0;
System.out.print("Enter: ");
x = var.nextInt();
for(int a = 1; a <= x; a++){
for(int c = 0; c < a; c++){
System.out.print("*");
}
System.out.println();
}
for (int a = x - 1; a > 0; a--){
for(int c = a; c > 0; c--)
System.out.print("*");
System.out.print(""\n");
}
Ok guys I have this problem how do I code this
but without empty line between
x
xy
xxy
xxyy
xxxyy
xxxyyy
Here is my code so far
public static void main(String[] args) {
System.out.print("x");
for(int i = 0;i<6;i++){
for(int j = 0;j<i;j++){
System.out.print("x");
}
System.out.println();
}
}
The pattern is as follows:
1x, 0y
1x, 1y
2x, 1y
2x, 2y...
So your loop should look something like this:
int xCount = 0;
int yCount = 0;
int total = 3;
do {
if (xCount == yCount) xCount++;
else yCount++;
for (int x = 0; x < xCount; x++) System.out.print("x");
for (int y = 0; y < yCount; y++) System.out.print("y");
System.out.println();
} while (yCount < total);
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();
}