I would like for my program to be in the shape of a triangle with space in between like the photo below.
Heres my code so far.
import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String space= " ";
space.replaceAll("", " ");
int i = input.nextInt();
while (i > 0) {
for (int j = 0; j <1; j++)
System.out.print("*"+space.substring(0,0)+"*");
System.out.println();
i--;
}
}
when i run this code the output is this
**
**
**
**
**
**
**
**
**
I would like the output to look like this:
First of all your inner for loop doesn't serve any purpose. It runs only once. The logic would be same even if you remove it.
for (int j = 0; j <1; j++)
Secondly, I feel you wanted to do something like this :
System.out.print("*"+space.substring(i)+"*");
Make sure your string is big enough than the index i
String space= " ";
Scanner input = new Scanner(System.in);
int i=input.nextInt();
for (int j=0; j<(i-1); j++)
System.out.print(" ");
System.out.println("*");
for (int j=1; j<(i-1); j++)
{
for (int k=0;k<(i-1-j); k++)
System.out.print(" ");
System.out.print("*");
for (int k=0;k<(j*2)-1; k++)
System.out.print(" ");
System.out.println("*");
}
for (int j=0; j<(i*2-1); j++)
System.out.print("*");
System.out.println("");
Using something like String.format("%3s, "*") you can define the width (here: 3) and therefore the white-space of the output. If you add some clever code to define the width for each line, you can save yourself a lot of headaches.
In addition it is important to know that only a font with a fixed letter spacing will do the trick (like Currier New).
Related
This is a question I got in an assignment of my Java class.
Q: Write a java program to print the following output using single System.out.print(“*”)
stars
I achieved above using a single printline command.
class Example{
public static void main(String[] args) {
String star = "*";
for (int a=0; a<5; a++) {
System.out.println(star);
star = star + " *";
}
}
}
But my proffesor wants to strictly follow the instructions in the question, hence can only use a single System.out.print(“*”). Thanks in advance.
class Example{
public static void main(String[] args) {
for (int a=0; a<6; a++) {
for (int b=0; b<a; b++) {
System.out.print("* ");
}
System.out.print("\n");
}
}
}
You can do this without using println but you have to use print("\n") instead to indicate line break.
You can use the print method instead of println but you have somehow to add an escape sequence (\n). In case that you can't add the new line character at the same print, you can use one of the following examples:
Using 2 for loops:
for (int i = 1; i <= 5; ++i) {
for (int j = 1; j <= i; ++j)
System.out.print("* ");
System.out.print("\n");
}
Using 1 for loop with the String#repeat as a helper:
for (int i = 1; i <= 5; i++) {
System.out.print("* ".repeat(i));
System.out.print("\n");
}
And the last one, using IntStream with the repeat as above (which i don't recommend it)
IntStream.rangeClosed(1, 5).forEach(c -> {
System.out.print("* ".repeat(c));
System.out.print("\n");
});
I'm doing a matrix solving console software on java.
What I am trying to do is that each "enter" key do a space instead of break in other to create a row and so on.
When the user types an INT then press "ENTER" it does a break so it breaks the matrix design.
This is my code so far:
Scanner scanner = new Scanner(System.in);//.useDelimiter("\\s+");
for(int i = 0; i < matrix.length; i++) {
System.out.print("| ");
for(int j = 0; j < matrix[i].length; j++) {
matrix[i][j] = scanner.nextInt();
}
System.out.print("|");
System.out.println("");
}
scanner.close();
System.out.println("Done");
Any ideas?
Thanks!
I don't think this possible, so i will suggest another way:
Instead to use Enter and return to the line why you don't write all your values and separate them with a space or a specific delimiter, so when you finish press enter to return to the next line to scan the next row like this:
1 2 3 4 5
5 6 7 8 9
Then you can get your values like this:
String line = scan.nextLine();
String[] row = line.split(" ");
Then use the String[] to get your int values?
This can help you.
In console mode, enter is enter : that is a breakline.
To solve your problem, you could do things differently :
You take a user input without consideration about formatting.
After each input you re-display the actual result of the matrix object.
In this way, you format it as you want, you can even display a special character the position of the next number to fill by the user.
With your actual code it could look like :
Scanner scanner = new Scanner(System.in);//.useDelimiter("\\s+");
for(int i = 0; i < matrix.length; i++) {
for(int j = 0; j < matrix[i].length; j++) {
matrix[i][j] = scanner.nextInt();
displayActualMatrix(matrix)
}
}
scanner.close();
System.out.println("Done");
...
private void displayActualMatrix(int[][] matrix){
for(int i = 0; i < matrix.length; i++) {
for(int j = 0; j < matrix[i].length; j++) {
// rendering the matrix
}
}
...
}
Here is a screenshot of my Output and the desired output is located below as wellI have to create program that outputs a star and the letter o. I have gotten it working for the most part but the output is not 100% correct, minor things really. Here is my code so far:
for(int i = 1; i <=numRows; i++){
System.out.print("\n");
for(int j = 0; j<=numRows; j++){
if(i+j >= numRows){
System.out.print('*');
System.out.print("o");
}else {
System.out.print(" ");
}
}
System.out.println();
And here is a screenshot of what the output is supposed to look like
This is similar to what I had to do for class a long time ago. Here is the code:
for(int i = 0; i <= userInput/2; i++)
{
for(int j = 0; j < (userInput/2 - i); j++)
{
printDiamond = printDiamond + " ";
}
for(int k = 0; k <= (i*2); k++)
{
printDiamond = printDiamond + "*";
}
printDiamond = printDiamond + "\n";
}
**Reason it is userInput/2 is because the other half of the code prints the diamond in reverse, thus actually making a diamond and not a triangle. **
So what do we have: First inner loop is responsible for printing the white spaces that help give our diamond its shape. The next inner loop prints the top of the diamond; *2 because we want it to print the whole top of the diamond, and not just half of the diamond if we were to split it top to bottom.
I will leave the addition of the extra o's to you.
I want to make the program print for any integer that I input an asterisk in such way that every new line there is an increasing number of two more asterisks, always starting from one asterisk.
This code will print for any integer the same number of lines that I entered, with one asterisk in it, but how do I increase the number of the asterisks in each line?
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("enter a number:");
int num = sc.nextInt();
int j=0;
int star=1;
int space= num;
System.out.println ("* ");
if (num>0) {
for (j=1; j<num; j=j+1) {
System.out.println ("* " );
}
for( j=0; j<star; j++) {
}
}
}
public static void main(String[] args) {
String line = "* "; // let's use a variable for the next line we want to print
Scanner sc = new Scanner(System.in);
System.out.println("enter a number:");
int num = sc.nextInt();
if (num > 0) {
System.out.println(line);
for (int j = 1; j < num; j++) { // j++ does the same as j = j + 1
line = "*" + line; // add a * at the start of the line
System.out.println(line);
}
}
}
System.out is a PrintStream. PrintStream has a second method, print that prints out what you put but without printing out the end of line character after it.
So, you probably want to use that instead.
Now, having said that, you'll still need to print out the end of line character when you've finished with the other characters. You can do that by calling the no argument version of println (which looks like System.out.println();)
Assuming you're looking for something like this:
INPUT: 4
*
**
***
****
Then you can do something like this:
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
for (int i = 0; i < num; i++) {
for (int j = 0; j < i; j++) {
System.out.Print("*");
}
System.out.Println();
}
Note that this prints out only one star at a time, and will be fairly slow. Fun to watch though, if toss a sleep statement in there.
You should use two for loops for that, one for lines and another for stars.
for (int i = 0; i < 5; i++) {
for (int j = 0; j < i; j++) {
System.out.Print("*");
}
System.out.Println();
}
Output:
=======
*
**
***
****
*****
So the task is to make the system output a triangle with spaces that increment in between x's like this(dashes added in place of space for readability):
xx
x-x
x--x
x---x
x----x
x-----x
x------x
x-------x
So, I've done this before and it seems easy enough, but the issue I'm having is getting the initial amount of spaces correct. I would like an example of how to do this and why it works as plainly stated as possible, thank you. Here's the code I have so far, along with the output:
Scanner in = new Scanner(System.in);
System.out.println("How many columns");
col = in.nextInt();
for (int i = 0; i < col; i++)
{
System.out.print("#");
for(int j = 0; j < (i+ 1); j++)
{
System.out.print(" ");
}
System.out.print("#");
System.out.println();
}
Output(when cols = 4):
x-x
x--x
x---x
x----x
All help is truly appreciated :)
public static void main(String[]args){
System.out.println("How many columns?");
int columns = new Scanner(System.in).nextInt();
for(int i=0; i<=columns; i++){
String toPrint = "x";
for(int cols=0; cols<i; cols++){
toPrint+=" ";
}
System.out.println(toPrint+"x");
}
}
change condition for j like below. also you have declared variable as col and used it like cols. so make corrections first.
Scanner in = new Scanner;
System.out.println("How many columns");
col = in.nextInt();
for (int i = 0; i < col; i++)
{
System.out.print("#");
for(int j = 0; j < i; j++)
{
System.out.print(" ");
}
System.out.print("#");
System.out.println();
}
I think The problem is with the r value set initially. There is no need of new variable r to be set.
If i is less than j, the loop doesn't get executed first time, and the loop executes 1 step more than each previous iteration of outer for loop