I am making this code that displays a series of asterisks in rows that get smaller by one each time you go down a row. It's supposed to start at 10 and stop at 1 on the very bottom row but for some reason my code starts with 1 at the top and grows bigger as it goes down. I was wondering how I could go about flipping the output so that it looks like the example. Thanks in advance
Example:
**********
*********
********
*******
******
*****
****
***
**
*
My code:
public class RowsOfAsterisks {
public static void main(String[] args) {
// TODO Auto-generated method stub
for (int i = 1; i < 11; i++)
{
for (int x = 0; x < i; x++)
{
System.out.print("*");
}
System.out.println();
}
}
}
You can create another method in order to avoid two for statment
Check if it can help you:
public class RowsOfAsterisks
{
public static String repeat(String str, int times)
{
return new String(new char[times]).replace("\0", str);
}
public static void main(String[] args)
{
for(int i = 10; i > 0; i--)
{
System.out.println(repeat("*", i));
}
}
}
You just have to start the outer loop from 10 to achieve this.
I hope the below code helps you:
public class RowsOfAsterisks {
public static void main(String[] args) {
for (int i = 10; i > 0; i--)
{
for (int x = 0; x < i; x++)
{
System.out.print("*");
}
System.out.println();
}
}
}
int rows = 10;
int columns = 10;
for(int i = 0; i<rows; i++)
{
for(int j = 0; int j < columns; j++)
{
System.out.print("*");
}
System.out.println();
--columns;
}
You're on the right track you just need to fix up the nested loop like so:
public static void main(String[] args) {
for (int i = 1; i < 11; i++)
{
for (int x = 11; x > i; x--)
{
System.out.print("*");
}
System.out.println();
}
}
Related
I can make this:
for (int i=0; i<6; i++){
for (int j=0; j<i; j++){
System.out.print("*");
}
System.out.println("");
}
but I cannot find out how to write a static void method that receives a positive integer and uses nested for-loops to display a right triangle made up of integers 1 to the number received
you can create static private method with size parameter and call from main method like below -
public static void main(String[] args) {
makeTriangle(6);
}
private static void makeTriangle(int size){
for (int i=0; i<size; i++){
for (int j=0; j<i; j++){
System.out.print("*");
}
System.out.println("");
}
}
Just define static method like this:
public class Test {
public static void printRightTriangle(int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
System.out.print("*");
}
System.out.println("");
}
}
public static void main(String[] args) {
printRightTriangle(12);
}
}
Hope it can help.
I think you're looking to print a right-angled triangle made up of integers in a method that takes a positive int.
public static void printTriangle(int maxVal) {
for (int i=1; i<=maxVal; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
System.out.println("");
}
Try this way:
public static void main(String[] args) {
printRightTriangle(6);
}
public static void printRightTriangle(int rows) {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < i; ++j) {
System.out.print("* ");
}
System.out.println();
}
}
so I want to create a numerical right triangle in java using a nested loop and I created this:
public class nestedloop
{
public nestedloop()
{
loop();
}
public static void main(String [] args)
{
new nestedloop();
}
public static void loop()
{
System.out.println(" Enter a number ");
int n= IBIO.inputInt();
for (int i = 1; i <=n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
However, as you can see, it creates a right triangle formed out of asterisks and I want it to create it through increasing numbers like this:
1
23
456
78910
I'm confused as to what I need to replace the Asterix with. I was thinking it could be another loop but everything I have tried failed miserably.
Just create a counter:
public static void loop(){
System.out.println(" Enter a number ");
int n= IBIO.inputInt();
int counter = 0;
for (int i = 1; i <=n; i++) {
for (int j = 1; j <= i; j++) {
System.out.println(++counter);
}
}
}
I am trying to write code using a for loop to print a pyramid of asterisks. The height of the pyramid should be determined by the user input and should look like:
ex: input of 3
*
**
***
**
*
import java.util.Scanner;
public class Homework6_Project2 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int triangleHeight = keyboard.nextInt();
int i;
int j;
for (i = triangleHeight; i >= 1; i--) {
for (j = i; j >= 1; j--) {
System.out.print("*");
}
System.out.println();
}
}
}
edit: I was forgetting the code for the bottom half where I needed to use the ++ increment operator.
You need another looping to print the upper pyramids.
Something like:
for(i = 1; i < triangleHeight; i++) {
for (j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
You can check this code which gave me the following output for input 3:
*
**
***
**
*
public class PyramidOfAsterisk {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
java.util.Scanner keyboard = new java.util.Scanner(System.in);
int pyramidHeight = keyboard.nextInt();
for(int i = 1; i <= pyramidHeight; i++){
for(int j=1; j <= i; j++){
System.out.print('*');
}
System.out.println();
}
for(int i = pyramidHeight - 1; i > 0; i--){
for(int j=i; j > 0; j--){
System.out.print('*');
}
System.out.println();
}
}
I'm working on Murach's Java Programming textbook exercise 11-4, and I've got the first two methods to work properly; however my shuffle method isn't working at all. Nothing happens.
public static String[] suits = {"C", "S", "H", "D"};
public static int[][] cards = new int[4][13];
public static int used[] = new int[13];
public static void loadCards() {
for(int i = 0; i < cards.length; i++) {
for(int j = 0; j < cards[i].length; j++){
cards[i][j] = j+1;
}
}
}
public static void writeCards() {
for(int i = 0; i < cards.length; i++) {
System.out.print(suits[i] +" ");
for (int j = 0; j < cards[i].length; j++) {
System.out.print(cards[i][j]+ " ");
}
System.out.println("");
}
}
public static void shuffle() {
for (int i = 0; i < cards.length; i++) {
shuffle:
for (int j = 0; j < cards[i].length; j++) {
double d = Math.random() * 13;
int random = (int) d;
for(int test = 0; test<used.length;test++){
if(random == used[test]) {
break shuffle;
}
}
cards[i][j] = random;
}
}
}
public static void main(String[] args) {
loadCards();
writeCards();
System.out.println("");
shuffle();
writeCards();
}
I feel like I'm starting to dig myself into a hole of wrong with the shuffle() method. Is there an easy fix I'm not seeing/am I trying something that's just absolutely wrong?
Can't write the shuffle code for you, but you can use something like bogo sort to shuffle how you want. The algorithm is used both for scrambling or shuffling, and for educational purposes to show why the efficiency of sorting algorithms is so important.
Solved it, thanks to Fred Larson for pointing me in the right direction.
public static void shuffle() {
for (int i = 0; i < cards.length; i++) {
for (int j = cards[i].length - 1; j > 0; j--) {
double d = Math.random() * 13;
int random = (int) d;
int a = cards[i][random];
cards[i][random] = cards[i][j];
cards[i][j] = a;
}
}
}
I'm trying to write a program that prints the output:
**
****
******
********
**********
What I came up with:
public class Loops {
public static void main(String[] args) {
for(int i=1; i<=5; i++) {
for(int j=0; j<i; j++) {
System.out.print("*" + "*");
}
System.out.println(" ");
}
}
This gives me the desired output, but despite my searching I still don't know how I can modify this code to use multiplication instead of just tacking on another * to the print statement.
public static void main(String[] args)
{
StringBuilder s = new StringBuilder();
for (int i = 1; i <= 5; i++)
{
s.append("**");
System.out.println(s.toString());
}
}
Is this what you want?
public class Loops {
public static void main(String[] args) {
for(int i = 1; i <= 5; i++) {
for(int j = 0; j < (2 * i); j++) {
System.out.print("*");
}
System.out.println();
}
}