import java.util.Scanner;
public class PrintVshape {
public static void main(String [] args){
Scanner inputDevice = new Scanner(System.in);
for(int i=0;i<5;i++){
for(int k=6;k>i;k--){
System.out.print("x");
}
System.out.print("V");
for(int j=7;j>i;j--){
System.out.print("p");
}
System.out.print("V");
System.out.print("\n");
}
}
}
This prints out the following:
xxxxxxVpppppppV
xxxxxVppppppV
xxxxVpppppV
xxxVppppV
xxVpppV
This is a homework problem. It's the x's that I'm stuck on. I need to invert them so they become more instead of fewer.
The loop you have for x says that run it from 6 to i and i moves from 0 to 5;
for(int k=6;k>i;k--){
System.out.print("x");
}
Which means x will be printed 6 times , 5 times .. ..
Correct the loop for x and it will be fine.
You need to loop on k up to some increasing value, such as i, or in this case, i+2, to get the range between two xs in the first row to six xs in the last:
for (int k = 0; k < i+2; k++) {
System.out.print("x");
}
Scanner inputDevice = new Scanner(System.in);
for(int i=0;i<5;i++){
for(int k=0;k<i+2;k++){
System.out.print("x");
}
System.out.print("V");
for(int j=7;j>i;j--){
System.out.print("p");
}
System.out.print("V");
System.out.print("\n");
}
}
result:
xxVpppppppV
xxxVppppppV
xxxxVpppppV
xxxxxVppppV
xxxxxxVpppV
i+2 in k cycle, mean how many times it will print x on beginnig (if you want start with xx then i=0+2)
I am not quite sure, is this the thing you are after now?
public static void main(String[] args) {
int N = 8;
printV(N);
}
private static void printV(int N) {
for (int i = 0; i < N; i++) {
for (int k = 0; k < i; ++k) {
System.out.print("x");
}
System.out.print("V");
for (int j = 0; j < (N - i - 1) * 2; ++j) {
System.out.print("p");
}
System.out.print("V");
for (int k = 0; k < i; ++k) {
System.out.print("x");
}
System.out.print("\n");
}
}
Output:
VppppppppppppppV
xVppppppppppppVx
xxVppppppppppVxx
xxxVppppppppVxxx
xxxxVppppppVxxxx
xxxxxVppppVxxxxx
xxxxxxVppVxxxxxx
xxxxxxxVVxxxxxxx
Related
Would it be possible to produce this layout with nested loops? I'm still new to nested to Java/loops and cannot solve this issue.
*****====+
*****===++
*****==+++
*****=++++
*****+++++
====++++++
===+++++++
==++++++++
=+++++++++
I'm having trouble looping through five times with the "*" character without allowing the "+" to increment.
Here is my code:
class Main {
public static void main(String[] args) {
for (int k = 4; k > 0; k--) {
System.out.print("*****");
for (int l = 0; l < k; l++) {
System.out.print("=");
}
for (int m = 0; m < 1; m++) {
for (int n = 0; n < m; n++) {
System.out.print("+");
}
}
System.out.println();
}
System.out.print("*****+++++");
}
}
there are could be multiple approaches to this problem, here is how I would think about it:
you need to display 9 lines of "something", so lets have top level loop:
for (int i=0; i<9; i++) {...}
now, each iteration of this loop you need to display X stars, Y equal signs, Z plus signs:
for (int i=0; i<9; i++) {
for (int j=0; j< #X# ; j++) System.out.print("*");
for (int j=0; j< #Y# ; j++) System.out.print("=");
for (int j=0; j< #Z# ; j++) System.out.print("+");
System.out.println();
}
now you need to determine rules how X,Y,Z are changed, here is the logic I came up with:
if (stars > 0 && equals == 0) {
stars = 0;
equals = 5;
}
equals--;
pluses++;
so, final code will look like:
public static void main(String[] args) throws InterruptedException {
int stars = 5; // initial state
int equals = 4;
int pluses = 1;
for (int i=0; i<9; i++) {
for (int j=0; j<stars; j++) System.out.print("*");
for (int j=0; j<equals; j++) System.out.print("=");
for (int j=0; j<pluses; j++) System.out.print("+");
System.out.println();
if (stars > 0 && equals == 0) {
stars = 0;
equals = 5;
}
equals--;
pluses++;
}
}
If you notice there is a pattern.
It prints a * starting from left to right.
It prints a + for each increasing number starting from right to left.
It prints a = instead of * if you are past mid point (first the mid point of left to right, then the mid point of top to bottom).
The logic can be applied as following,
class Main {
public static void main(String[] args) {
int max = 10;
int switchPoint = max - 1;
for(int i = 1; i <= max -1; i++) {
for(int j = 1; j <= max; j++) {
if(j > switchPoint)
System.out.print("+");
else if( i > max/2 || j > max / 2)
System.out.print("=");
else
System.out.print("*");
}
switchPoint--;
System.out.println();
}
}
}
/* Output:
*****====+
*****===++
*****==+++
*****=++++
*****+++++
====++++++
===+++++++
==++++++++
=+++++++++
*/
As you may notice the behavior of the "*" and "=" is different in the first five lines than in the next five lines, so you may either divide the loop in two loops or make a single one and check wether you are printing the first five lines or the last ones, that check may be done either by an if statement or in the loops conditions.
So the code will look like:
class Main {
public static void main(String[] args) {
for (int i = 1; i < 10; i++){
for (int j = 1; i <= 5 && j <= 5; j++){
System.out.print("*");
}
for (int k = 5; (i <= 5 && k > i) || (i > 5 && k > i-5); k--){
System.out.print("=");
}
for (int l = 1; l <= i; l++){
System.out.print("+");
}
System.out.println();
}
}
}
public class A {
public static void main(String[] args) {
for (int k = 4; k > 0; k--) {
System.out.print("*****");
for (int l = 0; l < k; l++) {
System.out.print("=");
}
for (int m = 0; m < 5 - k; m++) {
System.out.print("+");
}
System.out.println();
}
System.out.print("*****+++++");
System.out.println();
for (int s = 4; s > 0; s--) {
for (int k = 0; k < s; k++) {
System.out.print("=");
}
for (int l = 0; l < 5; l++) {
System.out.print("+");
}
for (int m = 0; m < 5 - s; m++) {
System.out.print("+");
}
System.out.println();
}
}
}
I am writing the code to print a matrix on taking user input in the form of n value:
Suppose if,
n= 3
output:
3 3 3
3 0 3
3 1 3
3 2 3
3 3 3
I am getting ArrayIndexOutOfBoundException in the line: a[i][j]=n;
import java.util.*;
public class HelloWorld{
public static void main(String []args){
Scanner scan = new Scanner(System.in);
//System.out.println("Enter n");
int n = scan.nextInt();
System.out.println(n);
int a[][]= new int[n][n];
int b=0;
int mid = n/2 +1;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i+j==mid)
{
a[i][j]=n-b;
b++;
}
else
{
a[i][j]=n;
}
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(a[i][j]);
}
System.out.println();
}
}
}
If a request for a negative or an index greater than or equal to size of array is made, then the JAVA throws a ArrayIndexOutOfBounds Exception. This is unlike C/C++ where no index of bound check is done. TheArrayIndexOutOfBoundsException is aRuntime Exception thrown only at runtime.
Look at this line:
for(int j=0;i<n;j++)
you increment while "i < n" but you increment "j++".
When your j reaches value 3, the inner loop continues, but exceeds the array-length (of 3, because the hightest array-index is actually 2).
(Also on a minor sidenote, it is usually preferrable to write ++i or ++j within for-loop increments. This is not a rule, just easier to read for most oldscool c-Devs). Also consider leaving spaces to inprove readability:
import java.util.*;
public class HelloWorld {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// System.out.println("Enter n");
int n = scan.nextInt();
System.out.println(n);
int a[][] = new int[n][n];
int b = 0;
int mid = n / 2 + 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i + j == mid) {
a[i][j] = n - b;
b++;
} else {
a[i][j] = n;
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(a[i][j]);
}
System.out.println();
}
}
minor correction in a loop
import java.util.*;
public class HelloWorld{
public static void main(String []args){
Scanner scan = new Scanner(System.in);
//System.out.println("Enter n");
int n = scan.nextInt();
System.out.println(n);
int a[][]= new int[n][n];
int b=0;
int mid = n/2 +1;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++) //minor correction here
{
if(i+j==mid)
{
a[i][j]=n-b;
b++;
}
else
{
a[i][j]=n;
}
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(a[i][j]);
}
System.out.println();
}
}
}
I am working on an assignment and I understand how to do the first part of the assignment but not the second.
Problem:
Write a program that ask the user to enter the size of a triangle (1 to 50), then print the triangle by printing a series of lines consisting of asterisks. The first line will have 1 asterisk, the next two will have two and so on, with each line having one more asterisk than the previous line up to the number entered by the user. On the next line print one less asterisk and continue by decreasing the number of asterisk by 1 for each successive line until any one asterisk is printed.
I can make the program print up ward however I don't know how to make it print downward. My professor says to use for loops.
import java.util.Scanner;
public class CS123Ass5ID5189 {
public static void main(String[] args) {
int size;
System.out.println("Enter Triangle size");
Scanner key = new Scanner(System.in);
size = key.nextInt();
System.out.println(size);
for (int i = 0; i < size; i++) {
for (int f = 0; f < i; f++) {
System.out.print("*");
}
System.out.println("*");
}
for (int i = 0; i > size; i--) {
for (int f = 0; f > i; f--) {
System.out.println("*");
}
System.out.println("*");
}
}
}
Supposed to look something like
*
**
***
**
*
Your answer is almost there. You just need the second outer loop to count down from size to zero in the outer loop, and do the same thing.
for (int i = size - 1; i > 0; i--)
So the first loop goes from 0 to size, and this loop goes from (size - 1) to 0.
for (int i = size - 1; i > 0; i--) {
// This section is exactly the same as in the first loop
for (int f = 0; f < i; f++) {
System.out.print("*");
}
System.out.println("*");
}
Edit
To limit the input number to 50, check it in another loop. While the user has entered an invalid number, tell them, and ask for another one. Add this just after you get the initial number:
while (size < 0 || size > 50)
{
System.out.print("Size must be between 0 and 50. Try again: ");
size = key.nextInt();
}
Your second loop is wrong, it should be for i>0. Also, start at 1, not 0 and print like this:
for (int i = 1; i <= size; i++) {
for (int f = 0; f < i; f++) {
System.out.print("*");
}
System.out.println();
}
for (int i = size-1; i > 0; i--) {
for (int f = 0; f < i; f++) {
System.out.print("*");
}
System.out.println();
}
Your inner loop should still be counting up to the value of i. This should do what you need.
For loops can begin at any value, not just zero. This allows you to start at size and decrement all the way down to zero.
Here is an example of what you are looking for. (It links to an online executable to demonstrate the code below.)
public class Main
{
public static void main (String[] args)
{
int size = 10;
System.out.println(size);
for (int i=0; i<size-1; i++)
{
for (int f=0; f<i; f++)
{
System.out.print("*");
}
System.out.println("*");
}
for (int i=size-1; i>=0; i--)
{
for (int f=i; f>0; f--)
{
System.out.print("*");
}
System.out.println("*");
}
}
}
P.S. Your second inner for loop should have System.out.print not System.out.println to keep all the asterisks on one line.
Your first loop looks fine, but look at the second loop.
for (int i = 0; i > size; i--) {
for (int f = 0; f > i; f--) {
System.out.println("*");
If size is greater than 0, the outer for-loop won't be entered. Fix that by setting i = size - 1, then loop until i > 0 and i-- to decrease i in each loop.
The inner for loop has a similar problem, it should be the exact same and the previous inner loop. Also exact same in the usage of System.out.print instead of System.out.println. Otherwise, the triangle will print vertically for the bottom half.
FWIW - It's easier to read if you make small methods to clearly show your intention.
private static void printStarLine(int howManyStars) {
for (int i = 0; i < howManyStars; i++) {
System.out.print("*");
}
System.out.println();
}
public static void main(String[] args) {
Scanner key = new Scanner(System.in);
int size = 3; // some number in the range 1-50
do {
System.out.println("Enter Triangle size (1-50)");
size = key.nextInt();
} while (size < 1 || size > 50);
for (int stars = 1; stars < size; stars++) {
printStarLine(stars);
}
printStarLine(size);
for (int stars = size - 1; stars > 0; stars--) {
printStarLine(stars);
}
}
for(i=1; i<=n; i++)
{
for(j=1; j<=i; j++)
{
System.out.print("*");
}
System.out.println();
}
for(i=n; i>=1; i--)
{
for(j=1; j<i; j++)
{
System.out.print("*");
}
System.out.println();
}
this works
I'm not able to print this pattern:-
90
890
7890
67890
567890
4567890
34567890
234567890
1234567890
I,ve already tried this:
class loop1
{
public static void main(String args[])
{
for(int i=1;i<10;i++)
{
for(int j=9;j<10;j++)
{ System.out.print(j);
j--;
}
System.out.println("");
}
}
}
but get an output of unlimited 9, a never ending loop.
try this
public static void main(String args[]) {
for (int i = 1; i < 10; i++) {
for (int j = i; j > 0; j--) {
System.out.print(10 - j);
}
System.out.println(0);
}
}
Try this its very simple
working example is here
StringBuffer s = new StringBuffer("0");
for(int i = 9; i > 0; i--) {
s.insert(0, i);
System.out.println(s);
Result
90
890
7890
67890
567890
4567890
34567890
234567890
1234567890
for(int i=1;i<10;i++) {
for(int j=10-i;j<10;)
System.out.print(j++);
System.out.println("0");
}
Try this:
StringBuffer s = new StringBuffer("0");
for(int i = 9; i > 0; i--) {
s.append(i);
System.out.println(s);
}
Your outer loop is looping the wrong way; for a working solution, it should be going from 9 down to 1. Then, your inner for loop would be looping up to 9:
for(int i = 9; i > 0; i--) {
for(int j = i; j < 10; j++) {
System.out.print(j);
}
System.out.println(0);
}
The 0 can't be part of the loop because it doesn't fit with the 1-9 pattern. However, you could print the last digit of each number and cut the 0 part of the println statement:
for(int i = 9; i > 0; i--) {
for(int j = i; j < 10; j++) {
String number = j + "";
System.out.print(number.substring(number.length() - 1));
}
System.out.println();
}
Maybe i am late but i have to post this because of my love for loops
public class SeriesLoop {
public static void main(String a[]){
for(int i=9;i>=1;i--){
for(int j=i;j<=10;j++){
System.out.print(j%10);
}
System.out.println();
}
}
}
DEMO
Cheers
Assume that the int variables i and j have been declared, and that n has been declared and initialized.
Using for loops (you may need more than one), write code that will cause a triangle of asterisks of size n to be output to the screen.
For example, if the value of n is 4, the output should be
*
**
***
****
I won't do your homework, so here is a hint:
The first line is always 1 element long.
The last line is always N elements long.
The are a total of N lines.
Surely, with the above, you can create the necessary program.
Just for fun - single for-loop solution:
public void doIt(int n) {
String temp = String.copyValueOf(char[n]);
for (int i = 1; i <= n; i++)
System.out.println(temp.substring(n-i).replace((char) 0, 'x'));
}
And some recursion - zero for-loop solution:
public void doItAgain(int n, String display) {
if (n==0) return;
System.out.println(display);
doItAgain(n-1, display+'x');
}
(call it with doItAgain(4,"x") for your example)
My answer:
public class loop1
{
public static void main(String[] args)
{
for(int i = 0; i < 4; i++)
{
for(int j = 0; j <= i; j++)
System.out.print("*");
System.out.println();
}
}
}
In case you're in school/college and more interested in getting some, more power to you buddy:
for(int i = 0; i < n; i++)
{
for(int j = 0; j <= i; j++)
System.out.print("*");
System.out.println();
}
public class ForLoop {
public static void main(String[] args) {
for(int i = 0;i <= 9;i++){
for(int j = 1;j <= i;j++){
System.out.print("*");
}
System.out.println("\n");
}
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j <= i; j++)
System.out.print("*");
System.out.println();
}
simple, easist way to do it using main method --> public static void main (Strings [] args){
for(int i = 1; i <= max; i++){
for(int j = 1; j <= i; j++){
System.out.print("*");
}
System.out.println(" ");
}
}