Skip to content

Instantly share code, notes, and snippets.

@SoPat712
Created June 18, 2021 00:12
Show Gist options
  • Save SoPat712/bf4e713b8c44be43c6cf85f395f63acb to your computer and use it in GitHub Desktop.
Save SoPat712/bf4e713b8c44be43c6cf85f395f63acb to your computer and use it in GitHub Desktop.
Java Intermediate: 6/17/2021
//Replace the package with whatever it says in your Intellij New project
//package com.javafx.test;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
reverseString();
arrExample();
asteriskPyramid();
rectangle();
pyramid();
nestLoop();
fourfigurea();
fourfigureb();
fourfigurec();
fourfigured();
}
public static void reverseString(){
Scanner scanner = new Scanner(System.in);
System.out.print("Input a string: ");
char[] letters = scanner.nextLine().toCharArray();
System.out.print("Reverse string: ");
for (int i = letters.length - 1; i >= 0; i--) {
System.out.print(letters[i]);
}
System.out.print("\n");
}
public static void arrExample(){
String[][] arr = {{"Victor", "Peter", "Doug"}, {"Tall", "Short", "Tall"}};
for (int i = 0; i< arr.length; i++){
for(int v = 0; v < arr[i].length; v++){
System.out.print(arr[i][v]+" ");
}
System.out.println();
}
}
public static void asteriskPyramid(){
String[][] array = new String[3][3];
String asterisks = "*";
int numberOfAsterisks = 0;
// iterate through the 3x3 array
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
// store the asterisk(s) in the array
array[row][col] = asterisks;
// print the current element
System.out.print(array[row][col] + "\t");
// update total number of asterisks in array
numberOfAsterisks += row + 1;
}
System.out.println(); // move to next row
asterisks += "*"; // each row has 1 more asterisk than the last one
}
// print the total number of asterisks in the array
System.out.println("Number of asterisks: " + numberOfAsterisks);
}
public static void rectangle(){
for (int row = 1; row <= 10; row++)
{
for (int col = 1; col <= 8; col++)
{
System.out.print("*");
}
System.out.println();
}
}
public static void pyramid(){
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the # of lines");
int n = scanner.nextInt();
for(int i=1;i<=n;i++)
{
for(int j=i;j<=n;j++)
{
System.out.print(" ");
}
for(int j=1;j<=i;j++)
{
System.out.print("* ");
}
System.out.println("");
}
}
public static void nestLoop(){
for (int line = 1; line <= 5; line++) {
for (int j = 1; j <= (-1 * line + 5); j++) {
System.out.print(".");
}
System.out.print(line);
for (int j = 1; j <= (line - 1); j++) {
System.out.print(".");
}
System.out.println();
}
}
public static void fourfigurea(){
for (int count =0; count < 10; count++)
{
for (int j=0; j < count+1; j++)
System.out.print("*");
System.out.println();
}
}
public static void fourfigureb(){
for (int count =11; count >= 0; count--)
{
for (int j=0; j < count-1; j++)
System.out.print("*");
System.out.println();
}
}
public static void fourfigurec(){
for(int count = 0; count < 10; count++)
{
for(int index=1; index < count+1; index++)
System.out.print(" ");
for(int star=10; star > count; star--)
System.out.print("*");
System.out.println();
}
}
public static void fourfigured(){
for(int count = 10; count > 0; count--)
{
for(int index=0; index < count-1; index++)
System.out.print(" ");
for(int star=10; star > count-1; star--)
System.out.print("*");
System.out.println();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment