Skip to content

Instantly share code, notes, and snippets.

@gersande
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gersande/9648119 to your computer and use it in GitHub Desktop.
Save gersande/9648119 to your computer and use it in GitHub Desktop.
Weeee just some homework
//-----------------------------------------
// Assignment 3
// Patterns : Triangle/Pattern Output in Java
// Written by Gersande 9505334
//-----------------------------------------
import java.util.Scanner;
import java.util.Arrays; // Just in case
public class TriangleOutput {
// stuff will go here
//Creating some custon print functions so that I don't have to copy/paste System.out.println(); a million times
public static void writeln(String a) {
System.out.println(a);
}
public static void write(String a) {
System.out.print(a);
}
public static void writeln(int a) {
System.out.println(a);
}
public static void write(int a) {
System.out.print(a);
}
public static void writeln() {
System.out.println();
}
public static void write() {
System.out.print(" ");
}
public static void main(String[] args) {
// Create a scanner object which I will call scan
Scanner scan = new Scanner(System.in);
// Let's set up some constant values
int minimumInput = 1;
int maximumInput = 10;
//Let's make some strings !!!
String introduction = "\n============================================================\nHello my name is Gersande and\nthis is my Triangle Pattern Output Program, designed in Java.\n============================================================\n";
String input1 = "Please call which Pattern you would like \nby entering a number between 1 and 4. Entering 5 quits the program.";
String input2 = "Please call how many lines of the pattern you would like, \nby entering a number larger than " +minimumInput+ " and less than "+maximumInput+".";
String mistake = "\n====================================\nI'm sorry, but that is not a valid option.\n====================================\n";
String quitting = "Quitting...";
// Let's set up some variables.
int patternInput;
int userInput;
write(introduction);
// Start up the repeating thingies
int c = 0;
int d = 0;
while (c==0) {
writeln(input1);
patternInput = scan.nextInt();
/*
* So basically in this program, c is always going to be equal to 0 unless the user presses 5.
* As long as patternInput is 1,2,3,4,5, and nothing else, something is going to happen!
*/
if (patternInput <= 0 || patternInput > 5 ) {
// If patternInput is too large, or too small, a mistake will be printed and the user will be prompted
writeln(mistake);
}
if (patternInput == 5) {
// This shuts down everything.
c = c + 1;
}
if (patternInput == 1) {
// Remember that little d variable? This is where it comes in handy.
do {
writeln(input2); // Ask for how many lines
userInput = scan.nextInt(); // Get the user input
if (userInput <= minimumInput || userInput >= maximumInput) {
writeln(mistake); // If the lines number is not some number between 1 and 10, print message.
} else {
patternOne(userInput);
break; // this ends everything
}
} while (d==0);
}
if (patternInput == 2) {
// Mostly, this is the same as when patternInput == 1
do {
writeln(input2); // ask for how many lines
userInput = scan.nextInt(); // get the user input
if (userInput <= minimumInput || userInput >= maximumInput) {
writeln(mistake); // print mistake because no
} else {
patternTwo(userInput);
break; // this ends the things
}
} while (d==0);
}
if (patternInput == 3) {
// Mostly, this is the same as when patternInput == 1
do {
writeln(input2); // ask for how many lines
userInput = scan.nextInt(); // get the user input
if (userInput <= minimumInput || userInput >= maximumInput) {
writeln(mistake); // print mistake because no
} else {
patternThree(userInput);
break; // this ends the things
}
} while (d==0);
}
if (patternInput == 4) {
// Mostly, this is the same as when patternInput == 1
do {
writeln(input2); // ask for how many lines
userInput = scan.nextInt(); // get the user input
if (userInput <= minimumInput || userInput >= maximumInput) {
writeln(mistake); // print mistake because no
} else {
patternFour(userInput);
break; // this ends the things
}
} while (d==0);
}
}
}
// PATTERN 1
/*
* The first pattern will output something like this, depending on how many lines the user asks the machine to input
*
* 5 4 3 2 1
* 5 4 3 2
* 5 4 3
* 5 4
* 5
*
*/
public static void patternOne(int a) {
// Check if a (the UserInput from before, is even or odd)
isEven(a);
writeln("The user asked for Pattern 1 with " + a + " lines.\n");
int z = 0;
for (int n = 0; n < a; n++) {
// some thing !!!
for (int m = a; m > z; m--) {
write(m);
write();
}
z++;
writeln();
}
writeln();
}
// PATTERN 2
/*
* The second pattern will output something like this, depending on how many lines the user asks the machine to input
*
* 1
* 1 2
* 1 2 3
* 1 2 3 4
* 1 2 3 4 5
*
*/
public static void patternTwo(int a) {
// Check if a (the UserInput from before) is even or odd)
isEven(a);
writeln("The user asked for Pattern 2 with " + a + " lines.");
int k = a;
for (int n = 0; n <= a; n++) {
// This is for making the empty things
for (int m = k; m > 0; m--) {
write();
write();
}
k--;
// This is for making the actual numbers
for (int o = 0; o<n ;o++ ) {
write(o+1);
write();
}
writeln(); // Skip a line
}
writeln();
}
// PATTERN 3
/*
* The third pattern will output something like this, depending on how many lines the user asks the machine to input
*
* 1 2 3 4 5
* 2 3 4 5
* 3 4 5
* 4 5
* 5
*
*/
public static void patternThree(int a) {
// Check if a (the UserInput from before) is even or odd)
isEven(a);
writeln("The user asked for Pattern 3 with " + a + " lines.\n");
int k = a;
int z = 0;
int y = 1;
for (int n = z; n <= k; n++) {
//This is for making empty things
for (int m = z; m < n; m++) {
write();
}
// This is for making the actual numbers
for (int o = y; o <= k; o++) {
//write(); // space before the numbers
write(o);
}
y++;
writeln();
}
writeln();
}
// PATTERN 4
/*
The second pattern will output something like this, depending on how many lines the user asks the machine to input
1
123
12345
123
1
PS SPECIAL CASE
WHEN THE AMOUNT OF LINES ARE EVEN IT LOOKS LIKE THIS :
1
123
123
1
*/
public static void patternFour(int a) {
// Check if a (the UserInput from before) is even or odd)
isEven(a);
writeln("The user asked for Pattern 4 with " + a + " lines.\n");
}
/*
i had this code for pattern four
int k = a / 2;
boolean timeToGrow = false;
int b = 1;
// LET'S MAKE THE PATTERNS MUAHAHAHAHAH
for (int n = 0; n <= a; n++) {
int z = k;
// write("foo"); // write foo everytime there is a line if a = 6, it will write foo on every six lines
// I STILL NEED TO WRITE THINGS HERE OKAY OKAY
for (int m=-1; m<=k; m++) {
write();
// i need a for loop for the first batch of spaces
// i need a for loop for the numbers
// i need a for loop for the second batch of spaces
}
// when increasing
for (int o = b; o<=n; o++) {
write(o);
}
if (z>0 && timeToGrow != true) {
z--;
}
if (z==0) {
z++;
timeToGrow = true;
}
if (z>0 && timeToGrow) {
z++;
}
k--;
writeln();
}
writeln();
WHICH CREATED THE FOLLOWING
============================================================
Hello my name is Gersande and
this is my Triangle Pattern Output Program, designed in Java.
============================================================
Please call which Pattern you would like
by entering a number between 1 and 4. Entering 5 quits the program.
4
Please call how many lines of the pattern you would like,
by entering a number larger than 1 and less than 10.
5
The amount of lines (5) is odd.
The user asked for Pattern 4 with 5 lines.
1
12
123
1234
12345
*/
// check it a number is even or odd, and prints a line
public static void isEven(int a) {
String even = "The amount of lines (" + a + ") is even.";
String odd = "The amount of lines (" + a + ") is odd.";
if (a%2 == 0) {
writeln(even);
} else {
writeln(odd);
}
writeln();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment