I'm trying to create a program that depends on user input for integers between 2 and 10.
If the user entered four, this should be the output:
****
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
****
I want an input of 4 to output four stars in the first line to make a horizontal edge, and then a 4-star diagonal. Then four stars along a vertical “edge”, before repeating the diagonal and horizontal edge.
So I can draw the first line, last and middle lines right but my diagonals won't even show the spaces for some reason!
****
**
**
**
**
* *
* *
* *
* *
**
**
**
**
****
This is my code (I'm a beginner but I've been trying to fix this a lot and I really need some help):
int num = 0;
System.out.println("Enter a value between 2 and 10.");
num = keyNum.nextInt();
while (num < 2 || num > 10) {
System.out.println("Enter a valid number please.");
num = keyNum.nextInt();
}
for (int a = 0; a < num + 1; a++)
{
System.out.print(" ");
}
for (int b = 0; b < num; b++)
{
System.out.print("*");
}
for (int c = 0; c < num; c++)
{
System.out.println("");
for (int d = num; d < 1; d--)
{
System.out.print(" ");
}
System.out.print("*");
for (int e = (num * 3) - 2; e < num; e++)
{
System.out.print(" ");
}
System.out.print("*");
}
for (int f = 0; f < num; f++)
{
System.out.println("");
System.out.print("*");
for (int g = 0; g < num * 3; g++)
{
System.out.print(" ");
}
System.out.print("*");
}
for (int h = 0; h < num; h++)
{
System.out.println("");
for (int i = num; i < 1; i--)
{
System.out.print(" ");
}
System.out.print("*");
for (int j = (num * 3) - 2; j < num; j++)
{
System.out.print(" ");
}
System.out.print("*");
}
System.out.println("");
for (int k = 0; k < num + 1; k++)
{
System.out.print(" ");
}
for (int l = 0; l < num; l++)
{
System.out.print("*");
}
Any kind of help would be appreciated! Thank you.
This should work
Just changed couple of for loop conditions
while (num < 2 || num > 10) {
System.out.println("Enter a valid number please.");
num = keyNum.nextInt();
}
for (int a = 0; a < num + 1; a++)
{
System.out.print(" ");
}
for (int b = 0; b < num; b++)
{
System.out.print("*");
}
for (int c = 0; c < num; c++)
{
System.out.println("");
for (int d = num; d > c; d--)
{
System.out.print(" ");
}
System.out.print("*");
for (int e = num * 2; e < (num * 3) + (c *2); e++)
{
System.out.print(" ");
}
System.out.print("*");
}
for (int f = 0; f < num; f++)
{
System.out.println("");
System.out.print("*");
for (int g = 0; g < num * 3; g++)
{
System.out.print(" ");
}
System.out.print("*");
}
for (int h = 1; h <= num; h++)
{
System.out.println("");
for (int i = 0; i < h; i++)
{
System.out.print(" ");
}
System.out.print("*");
for (int j = 0; j < (num * 3) - (h * 2) ; j++)
{
System.out.print(" ");
}
System.out.print("*");
}
System.out.println("");
for (int k = 0; k < num + 1; k++)
{
System.out.print(" ");
}
for (int l = 0; l < num; l++)
{
System.out.print("*");
}
Related
How to make this pattern
if input N = 5
Output :
Mine is like this, it become 2N
if input N = 5
Output
Here's my code
int i,j;
for(i = 0; i <= n; i++)
{
for(j = 1; j <= n - i; j++)
System.out.print(" ");
for(j = 1; j <= 2 * i - 1; j++)
System.out.print("*");
System.out.print("\n");
}
for(i = n - 1; i >= 1; i--)
{
for(j = 1; j <= n - i; j++)
System.out.print(" ");
for(j = 1; j <= 2 * i - 1; j++)
System.out.print("*");
System.out.print("\n");
}
What should i fix??
You can check odd numbers in your loop. Please see the following example:
public static void main(String[] args) {
printPattern(5);
}
private static void printPattern(int n) {
int i, j;
for (i = 0; i <= n; i++) {
if (i % 2 != 0) {
for (j = 1; j <= (n - i)/2; j++) {
System.out.print(" ");
}
for (j = 0; j < i; j++) {
System.out.print("*");
}
System.out.println();
}
}
for (i = n - 1; i >= 1; i--) {
if (i % 2 != 0) {
for (j = 1; j <= (n - i)/2; j++) {
System.out.print(" ");
}
for (j = 0; j <i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
Instead of running these two loops from 0 to N twice. Just run half N/2 in each loop.
Example:
public static void main(String[] args) {
int n = 10;
for (int i = 0; i <= (n / 2 + 1); i++) {
for (int j = 1; j <= n - i; j++) System.out.print(" ");
for (int j = 1; j <= 2 * i - 1; j++) System.out.print("*");
System.out.print("\n");
}
// N/2
for (int i = n / 2 - 1; i >= 1; i--) {
for (int j = 1; j <= n - i; j++) System.out.print(" ");
for (int j = 1; j <= 2 * i - 1; j++) System.out.print("*");
System.out.print("\n");
}
}
This is the work that i done so far:I have to print diamond pattern which always starts with uppercase from string, which repeats,but not always starts from the beginning.
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String userInput = keyboard.next();
userInput = Character.toUpperCase(userInput.charAt(0)) + userInput.substring(1);
int i;
int j;
if (userInput.length() % 2 != 0) {
for(i = 1; i < userInput.length(); i += 2) {
for(j = 0; j < userInput.length() - 1 - i / 2; ++j) {
System.out.print(" ");
}
for(j = 0; j < i; ++j) {
System.out.print(userInput.charAt(j));
}
System.out.println("");
}
for(i = userInput.length(); i > 0; i -= 2) {
for(j = 0; j < userInput.length() - 1 - i / 2; ++j) {
System.out.print(" ");
}
for(j = 0; j < i; ++j) {
System.out.print(userInput.charAt(j));
}
System.out.print("\n");
}
} else {
for(i = 2; i < userInput.length(); i += 2) {
for(j = 0; j < userInput.length() - 1 - i / 2; ++j) {
System.out.print(" ");
}
for(j = 0; j < i; ++j) {
System.out.print(userInput.charAt(j));
}
System.out.println("");
}
for(i = userInput.length(); i > 0; i -= 2) {
for(j = 0; j < userInput.length() - 1 - i / 2; ++j) {
System.out.print(" ");
}
for(j = 0; j < i; ++j) {
System.out.print(userInput.charAt(j));
}
System.out.print("\n");
}
}
}
For example my input is "Peter".
So my output is:
P
Pet
Peter
Pet
P
but it must be:
P
Ete
Rpete
Rpe
T
I dont know what to change to make this work
Here's a shorter version of your code:
public static void main(String[] args) {
String userInput = "Peter";
int length = userInput.length();
int m, j, i, n = 0;
for (m = length % 2 > 0 ? 1 : 2; m < length * 2; m += 2) {
i = m < length ? m : length * 2 - m;
for (j = 0; j < length - 1 - i / 2; ++j) {
System.out.print(" ");
}
for(j = 0; j < i; ++j) {
char c = userInput.charAt(n++ % length);
c = j == 0 ? Character.toUpperCase(c) : Character.toLowerCase(c);
System.out.print(c);
}
System.out.println("");
}
}
You need some few changes:
Declare int n=0; after int j;
Always print userInput.charAt(n++ % userInput.length()) instead of charAt(j)
In order to get only the first character in line in uppercase:
char c = userInput.charAt(n++ % userInput.length());
c = j == 0 ? Character.toUpperCase(c) : Character.toLowerCase(c);
System.out.print(c);
Check the modulo operator.
With these changes, you'll get this output:
P
Ete
Rpete
Rpe
T
Given the fact that the input itself gets printed in a cylic manner, we can make use out of it. My proposal would be to concatenate the input string and print out the substrings which are determined by the structure of the diamond pattern.
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String userInput = keyboard.next();
String concatenated = userInput;
// build up the index array
int i, cumSum = 0;
ArrayList<Integer> helperIndex = new ArrayList<>();
for(i = 1; i < userInput.length(); i += 2) {
helperIndex.add(i);
cumSum += i;
}
for(i = userInput.length(); i > 0; i -= 2) {
helperIndex.add(i);
cumSum += i;
}
int numOfWordRepitition = cumSum / userInput.length() ;
for (i = 0; i < numOfWordRepitition; i++){
concatenated += userInput;
}
// print out diamond
String substr;
int prev = helperIndex.get(0);
int next = helperIndex.get(0);
substr = concatenated.substring(0 , helperIndex.get(0));
System.out.println(Character.toUpperCase(substr.charAt(0)) + substr.substring(1));
for(i = 1; i < userInput.length(); i++){
next += helperIndex.get(i);
substr = concatenated.substring(prev , next);
substr = Character.toUpperCase(substr.charAt(0)) + substr.substring(1);
System.out.println(substr);
prev = next;
}
}
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");
}
So for an assignment i need to make a dual-wedge figure with for loops,so far I have had no luck, can anyone help?
Here is a sample of the outcome:
*******
*** ***
** **
* *
Here's my code
int dual_wedge_length=9;
int half_length = dual_wedge_length/2;
int space=1;
int height2 = (dual_wedge_length/2) +1;
for (int line1 = 1; line1 <= dual_wedge_length; line1++)
{
System.out.print("*");
}
System.out.println();
for (int height = 1; height <= (dual_wedge_length+1)/2; height++)
{
for (int half1 = 1; half1 <= half_length; half1++)
{
System.out.print("*");
//half_length--;
space+=2;
}
for (int space_counter = 0; space_counter == space;space_counter++)
{
System.out.print(".");
}
for (int half1 = 1; half1 >= half_length; half1++)
{
System.out.print("*");
half_length--;
}
System.out.println();`
int dual_wedge_length=9;
int height2 = (dual_wedge_length+1)/2;
for(int i = 0;i < dual_wedge_length; i++)System.out.print("*");
System.out.println();
for(int i = 1; i < height2;i++){
int num = height2 - i;
for(int j = 0; j < num; j++){
System.out.print("*");
}
for(int k = 0; k < 2*i -1; k++){
System.out.print(" ");
}
for(int m = 0; m < num; m++){
System.out.print("*");
}
System.out.println();
}
I am new to learning Java and I am trying to print an unfilled diamond using asterisks. The height of the diamond has to be based on user input. My code currently prints out a filled diamond based on user input but I cannot figure out how to print an unfilled one. Every time I change one of the loops it messes something else up. All help is appreciated!
public static void diamond(){
int i = 0;
int j = 0;
int k = 0;
int height = 0;
Scanner in = new Scanner(System.in);
System.out.println("How tall do you want the diamond to be: ");
height = in.nextInt();
for (k = 1; k <= (height + 1) / 2; k++) {
for (i = 0; i < height - k; i++) {
System.out.print(" ");
}
for (j = 0; j < k; j++) {
System.out.print("* ");
}
System.out.println("");
}
for (k = ((height + 1) / 2); k < height; k++) {
for (i = 1; i < k; i++) {
System.out.print(" ");
}
for (j = 0; j < height - k; j++) {
System.out.print(" *");
}
System.out.println("");
}
}
Here is the method modified for printing an unfilled diamond. An if condition has been added to the inner loops.
public static void diamond() {
int i = 0;
int j = 0;
int k = 0;
int height = 0;
Scanner in = new Scanner(System.in);
System.out.println("How tall do you want the diamond to be: ");
height = in.nextInt();
for (k = 1; k <= (height + 1) / 2; k++) {
for (i = 0; i < height - k; i++) {
System.out.print(" ");
}
for (j = 0; j < k; j++) {
if (j == 0 || j == (k - 1)) {
System.out.print("* ");
} else {
System.out.print(" ");
}
}
System.out.println("");
}
for (k = ((height + 1) / 2); k < height; k++) {
for (i = 1; i < k; i++) {
System.out.print(" ");
}
for (j = 0; j < height - k; j++) {
if (j == 0 || j == (height - k - 1)) {
System.out.print(" *");
} else {
System.out.print(" ");
}
}
System.out.println("");
}
}