Good day! Newbie in Java Programming. Would like to ask for some help on understanding the looping. The program's aim is to print a right-aligned triangle. I've created method for printing "*" and another one for " " (whitespace). I'm having trouble understanding on how can I implement the whitespace in my main method. Thank you!
Expected output:
"printTriangle(4);"
*
**
***
****
Here is my code:
public class PrintingLikeBoss {
public static void printStars(int amount) {
int i = 1;
while (i <= amount) {
System.out.print("*");
i++;
}
System.out.println("");
}
public static void printWhitespaces(int amount) {
int i = 1;
while (i <= amount) {
System.out.print(" ");
i++;
}
System.out.println("");
}
public static void printTriangle(int size) {
int i = 1;
int j = 1;
while (i >= 0) {
printStars(size);
i++;
}
}
printTriangle(4);
}
}
The printTriangle() methode will never end due to i always being greater then 0.
Also there is no main method in your code, therefore you will not be able to run it.
Now for the answer to your question:
public static void printTriangle(int size){
int i = size;
int j = 1;
while(j<=i){
printWhite(i-j);
printStar(j);
j++;
System.out.println("");
}
}
public static void printWhite(int size){
int i = size;
for(int j = 0; j<i; j++){
System.out.print(" ");
}
}
public static void printStar(int size){
int i = size;
for(int j = 0; j<i; j++){
System.out.print("*");
}
}
public static void main(String[] args){
printTriangle(4);
}
This should provide you an output like this:
*
**
***
****
Related
I'm working on a Tower of Hanoi project for school which needs to ask the user how many disks there are and then it needs to create and then solve the tower with a visual included. How I decided to do it is by using 2D arrays and for the most part its working, my only problem is that I don't know how to move the disks while keeping it modular. Here is my code.
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter number of disks");
int num = scan.nextInt();
int temp = num-(num-1);
int measure = num;
//initializing the towers
int[][] towers = new int[num][num];
for(int i = 0 ; i < num; i++)
{
for(int j=0; j <3; j++)
{
}
}
createRings(towers, num, temp);
moveDisk(towers,num);
}
// creating the rings
private static void createRings (int[][]towers, int num, int temp)
{
for(int i = 0; i<num; i++)
{
for(int j=0; j<3;j++)
{
towers[i][0] = temp;
}
temp = temp+1;
}
displayTower(towers, num);
}
// prints the array for display purposes
private static void displayTower (int[][] towers, int num)
{
for(int i = 0; i<num; i++)
{
for(int j = 0; j<3; j++)
{
System.out.print(towers[i][j]+"\t");
}
System.out.println();
}
}
//moves the numbers in the array that represents disks
private static void moveDisk(int[][]towers, int num)
{
System.out.println();
displayTower(towers, num);
}
Does anyone have any suggestions on what I could do?
I've changed your code a bit to make it more readable for me.
I changed the towers array. Now each tower is its own array inside the towers array (makes more sense IMHO).
Arrays in Java know their size. So you don't have to pass the length of the array as a parameter to every method.
I added a method getHighestIdx() which returns the index before the first 0 value in the array
I don't know, how the function moveDisk() was intended to work. I changed the declaration so that it makes sense to me. It now moves a disk from tower i to tower j
This should help you to implement the algorithm from the linked question.
Here is the changed code:
public static void main(String[] args) {
int numberOfRings = 6;
int[][] towers = new int[3][numberOfRings];
createRings(towers);
displayTowers(towers);
moveDisk(towers, 0, 2);
displayTowers(towers);
}
private static void createRings(int[][] towers) {
for (int j = 0; j < towers[0].length; j++) {
towers[0][j] = j + 1;
}
}
private static void displayTowers(int[][] towers) {
for (int i = 0; i < towers[0].length; i++) {
for (int j = 0; j < towers.length; j++) {
System.out.print(towers[j][i] + " ");
}
System.out.println("");
}
}
private static void moveDisk(int[][] towers, int fromIdx, int toIdx) {
int valToMove = towers[fromIdx][getHighestIdx(towers[fromIdx])];
towers[fromIdx][getHighestIdx(towers[fromIdx])] = 0;
towers[toIdx][getHighestIdx(towers[toIdx]) + 1] = valToMove;
}
private static int getHighestIdx(int[] tower) {
int i = 0;
while (i < tower.length && tower[i] != 0) {
i++;
}
return i - 1;
}
My program is as below:
package simplemirror;
public class simple {
public static void main (String arg[]){
for( int i = 1; i <= 3; i++ ){
for( int j = 0; j < i; j++ ){
System.out.print(i+"*");
}
System.out.println("");
}
}
}
Above program output is as below:
1*
2*2*
3*3*3*
I need output as below:
1
2*2
3*3*3
2*2
1
this code will work try this.............:)
public class simple {
public static void main(String[] args) {
// TODO Auto-generated method stub
int n;
n=3;
for(int i=1;i<2*n;i++){
if(i<=n){
for(int j=1;j<i;j++){
System.out.print(i+"*");
}
System.out.println(i);
}
else{
for(int j=i+1;j<2*n;j++){
System.out.print(2*n-i+"*");
}
System.out.println(2*n-i);
}
}
}
}
try
public static void main(String[] args) {
int limit=5;
int rows=limit+(limit-1);
int center=(int) Math.ceil(((double)rows/2));
boolean centerReached=false;
for(int i=1;i<=limit&&i>0;){
for(int j=1;j<=i;j++){
System.out.print(i);
if(i==j)
continue;
System.out.print("*");
}
if(center==i)
centerReached=true;
if(centerReached)
i--;
else
i++;
System.out.println();
}
}
ouptut
1
2*2
3*3*3
4*4*4*4
5*5*5*5*5
4*4*4*4
3*3*3
2*2
1
EDIT:
It's been a while, and I have learned a few things.
You can use streams!
private static void print(int num) {
IntStream.concat(
IntStream.iterate(1, (i) -> ++i).limit(num),
IntStream.iterate(num - 1, (i) -> --i).limit(num).limit(num - 1))
.boxed()
.map(i -> Stream.generate(i::toString).limit(i).collect(Collectors.joining("*")))
.forEach(System.out::println);
}
Please don't ask such question here. Ans to your question may be looks like follows but once again don't ask such questions here.
public class simple {
public static void main (String arg[])
{
for( int i = 1; i <= 3; i++ ){
for( int j = 0; j < i; j++ ){
if(j== i-1)
{
System.out.print(i);
}
else
{
System.out.print(i+"*");
}
}
System.out.println("");
}
}
}
The task of exercise is to make a method that will work like in Example.( We must use for loop. But if you know how to do it in another way, it will be also very interesting.) Input number can be any.
Example:
Input: 3
Output:
**1**
*121*
12321
*121*
**1**
My example:
public static void main(String[] args) {
printMatrix(5);
}
public static void printMatrix (int n) {
int d = n +(n-1);
for (int i = 0; i < n ; i++)
{
for(int j = 1; j <=d; j++){
int abs = Math.abs(j-n);
System.out.print(abs>i ? "*" : i-abs+1);
}
System.out.println("");
}
My output:
****1****
***121***
**12321**
*1234321*
123454321
I can't make the next step, to turn it upside down. Dose anybody hava an ideas?
I have solved this exercise, but I think it is not a perfect solution. Does anybody have another way to solve this task?
public static void main(String[] args) {
printMatrix(5);
}
public static void printMatrix (int n) {
int d = n +(n-1);
int k = n*2;
int g = -1;
for (int i = 0; i < d ; i++)
{
if(i<n){
g++;
}
else{k=n*2;
g--;}
for(int j = 1; j <=d; j++){
if (i < n){
int abs = Math.abs(j-n);
System.out.print(abs>i ? "*" : i-abs+1);
}
else{
k--;
int abs = Math.abs(k-n);
System.out.print(abs>g ? "*" : g-abs+1);
}
}
System.out.println("");
}
}
}
Output:
****1****
***121***
**12321**
*1234321*
123454321
*1234321*
**12321**
***121***
****1****
I am taking a programming class, and things just aren't clicking for me. I have an assignment that asks to:
Write a program to assign the integer values 1 through 25 to a 25
element integer array. Then, print the array as five separate lines
each containing five elements separated by commas. The last element
on each line should be followed by a newline instead of a comma. The
output of your program should appear exactly as follows:
1,2,3,4,5
6,7,8,9,10
11,12,13,14,15
16,17,18,19,20
21,22,23,24,25
Hints:
One way to determine every 5th element is to use the modules operator (%). If you divide the subscript by 5 and the remainder is
0, it is the 5th number.
You can use System.out.print() to print a value without a newline following it. This will allow you to print multiple things on the same
line.
I have a little bit of code but I don't know where to go from here:
public class Program4
{
public static int[] array;
public static void main(String[] args);
{
int[] numbers = new int [25]
for(int i=0; i<25; i++)
array[i] = i + 1;}
public static void printArray()
{
for(int i=1; i<=25; i++);
{
System.out.print(array[i - 1]);
if (i % 5 == 0)
System.out.printIn();
}
}
I just have a mental block about programming-can anyone help point me to some helpful examples?
Try this,
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static int[] array;
public static void main(String[] args)
{
array = new int[25];
for(int i=0; i<25; i++)
array[i] = i + 1;
printArray();
}
public static void printArray()
{
int i;
for(i=1; i<=25; i++){
if (i % 5 != 0)
System.out.print(array[i-1]+",");
else
System.out.println(array[i-1]);
}
}
}
public class Foo {
public static int[] nums;
public static void main(String[] args) {
nums = new int[25];
for (int i = 0; i < nums.length; i++) {
nums[i] = i + 1;
}
printArray(nums);
}
public static void printArray(int[] myArray) {
for (int i = 0; i < myArray.length; i++) {
System.out.print(String.valueOf(myArray[i]);
if (i % 5 == 0) {
System.out.println();
} else if (i % 5 != 4){
System.out.println(", ");
}
}
}
public class Test {
public static void main(String[] args) {
int[] array = new int [25];
for(int i=0; i<25; i++) {
array[i] = i + 1;
}
for (int i=1; i<=25; i++) {
System.out.print(array[i - 1]);
if (i % 5 == 0) {
System.out.println();
} else {
System.out.print(", ");
}
}
}
}
And try to learn Java syntax first of all.
Here is an enhanced version of your code.
public class Program4
{
public static int[] array = new int[25];//instantiate the array to its default values
public static void main(String[] args)
{
//calling the methods from main
addToArray();
printArray();
}
//add the numbers to the array
public static void addToArray(){
for(int i=0; i<25; i++)
array[i] = i + 1;
}
//print the numbers from the array
public static void printArray()
{
for(int i = 1; i <= 25; i++){
if(i % 5 == 0){
System.out.print(i);
System.out.println();
}
else{
System.out.print(i + ",");
}
}
}
}
is it possible to override the last System.out.println output, so i can for example visualize changes in a array or create a progessbar?
For example if i have this class:
class Main{
public static void main(String[] args){
for(int i = 0; i < 10; i++){
for(int j = 0; j < i; j++){
System.out.print("#");
}
System.out.println("");
}
}
}
What do i have to do to create this simple progressbar which is shown in a single line and not in 10 seperate lines?
This works on my particular console (Windows) but it's not terribly portable...
public class Test {
public static void main(String[] args) throws Exception {
for (int i = 0; i < 100; i++) {
System.out.print("#");
if (i % 20 == 0) {
System.out.print("\r \r");
}
System.out.flush();
Thread.sleep(100);
}
}
}
There's also the Console class, but that doesn't actually buy you very much as far as I can see...