How to create simple triagle pattern in Java - java

I am trying to output a triangle like this one:
19283765
2837465
37465
465
5
However, my actual output looks like this instead:
98765
8765
765
65
5
public class JavaNumber2 {
public static void main(String[] args) {
int r = 9;
for (int g = 9; g <= r; g--) {
for (int j = g; j >= 5; j--) {
System.out.print(j);
}
System.out.println();
}
}
}
How can I modify this so that I get the first output instead?

The condition in the first for loop is wrong g<=r should be g>=0, and one if condition to print in reverse order.
public static void main(String[] args) {
int r = 9;
for (int g = 9; g >=0; g--) {
for (int j = g; j >= 5; j--) {
if(g==9) {
if(j>6) {
System.out.print(r+1-j);
}
}else {
if(j>5) {
System.out.print(r+1-j);
}
}
System.out.print(j);
}
System.out.println();
}
}

I have this answer:
public class T2Tree {
public static void main(String[] args) {
for (int i = 5; i >0; i--) {
for (int j = 0; j < i; j++) {
System.out.print(j+1+5-i);
if (j<i-1) {
System.out.print(9-j+i-5);
}
}
System.out.println();
}
}
}

Or you can use a recursion (works with any start and end as well):
class Ideone
{
public static void main (String[] args)
{
int left = 1;
int right = 9;
while(right >= left){
r(left, right);
System.out.println();
left++;
right--;
}
}
private static void r(int left, int right){
System.out.print(left);
if(left != right){
System.out.print(right);
}
if(right > left){
r(left + 1, right - 1);
}
}
}

Related

Nested loop in while

I created a nested loop using For, here is the program code and the ouput, then I tried the while loop and get the different result:
For:
public class ForBersarang {
public static void main(String[] args){
int a = 5;
for(int i = 0; i<=a; i++){
for(int j = 0; j<=i; j++){
System.out.print("*");
}
System.out.println("");
}
}
While
public class WhileBersarang {
public static void main(String[] args){
int i = 0;
int a = 5;
int j = 0;
while (i<=a) {
while (j <= i) {
System.out.print("*");
j++;
}
i++;
System.out.println("");
}
}
Your problem is where you define j:
public class MyClass {
public static void main(String args[]) {
int i = 0;
int a = 5;
while (i<=a) {
//here
int j = 0;
while (j <= i) {
System.out.print("*");
j++;
}
i++;
System.out.println("");
}
}
}

(Java) I am making a horizontal Ruler with for loops

I'm trying to create a horizontal ruler as shown below:
The code I have right now is:
public class JavaRuler {
public static final int COUNT = 19;
public static void main(String[] args) {
Design();
Inches();
Number();
}
public static void Design() {
for (int j = 1; j <= COUNT * 2 + 2; j++) {
System.out.print("__");
}
System.out.println();
}
public static void Inches() {
for (int i = 1; i <= COUNT * 2 + 2; i++) {
System.out.print("'|");
}
System.out.println();
}
public static void Number() {
for (int k = 1; k <= COUNT; k++) {
System.out.print("___" + k);
}
}
}
The ruler has to be re-sizable in inches and if statements are not allowed (only for loops) my current output is really close but I cant seem to figure out how to decrease the "_" in the third row once the numbers go beyond 9. Any tips?
I think my example here will do the trick
public static void Number() {
for (int k = 1; k <= COUNT; k++) {
System.out.print(("____" + k).substring(("" + k).length()));
}
}
Result
'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|
___1___2___3___4___5___6___7___8___9__10__11__12__13__14__15__16__17__18__19
You can refer to this, but the result will be a mess if the COUNT is a big number. I have also updated some of your codes so that the inches and design will not overlap the last number.
public class JavaRuler {
public static final int COUNT = 20;
public static void main(String[] args) {
Design(" ");
Inches();
Number();
Design("|");
}
public static void Design(String s) { // Added parameter
System.out.print(s + "_"); // Added
for (int j = 0; j < COUNT * 2; j++) { // Updated
System.out.print("__");
}
System.out.print(s); // Added
System.out.println();
}
public static void Inches() {
System.out.print("|"); // Added
for (int i = 0; i < COUNT * 2; i++) { // Updated
System.out.print("'|");
}
System.out.print("'|"); // Added
System.out.println();
}
public static void Number() {
System.out.print("|"); // Added
for (int k = 1; k <= COUNT; k++) {
String underScore = " "; // Added
int length = (int) (Math.log10(k - 1) + 1);
for (int i = 1; i < length; i++) {
underScore = underScore.replaceFirst(" ", ""); // Added
}
System.out.print(underScore + k); // Updated
}
System.out.print("|"); // Added
System.out.println();
}
}
Result:
_________________________________________________________________________________
|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|'|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20|
|_________________________________________________________________________________|
I came back to it later today and it hit me! I figured it out!
public class Ruler {
public static final int COUNT = 19;
public static void main(String[] args) {
Design();
Inches();
Numbers();
}
public static void Design() {
for (int j = 1; j <= COUNT * 2 + 2; j++) {
System.out.print("__");
}
System.out.println();
}
public static void Inches() {
System.out.print("|");
for (int i = 1; i <= COUNT * 2 + 2; i++) {
System.out.print("'|");
}
System.out.println();
System.out.print("");
}
public static void Numbers() {
System.out.print("|");
for (int k = 1; k <= 9; k++) {
System.out.print("___" + k);
}
System.out.print("___");
for (int m = 10; m <= COUNT; m++){
System.out.print(m + "__");
}
System.out.print("|");
}
}

optimize for loop in java

I am trying to print a square using for loop.
can the below code be improvised to print the square only using for loop instead of swing
public class TestProgram {
public static void main(String[] args) {
TestProgram test = new TestProgram();
test.draw(15, 4);
}
public void draw(int x, int y) {
// System.out.println(".");
System.out.println();
int i = 0;
int j = 0;
for (i = 0; i < x; i++) {
System.out.print(".");
// continue;
}
for (j = x; j > y; j--) {
System.out.println(".");
System.out.print("\t\t");
System.out.println(".");
}
for (int k = 0; k <= x; k++) {
System.out.print(".");
}
}
}
How about:
public class TestProgram {
public static void main(String[] args) {
TestProgram test = new TestProgram();
test.draw(15, 4);
}
public void draw(int x, int y) {
System.out.println(generateLine(x, '.'));
for (int i = 1; i< y-1; i++) {
System.out.println("." + generateLine(x-2, ' ') + ".");
}
System.out.println(generateLine(x, '.'));
}
private String generateLine(int x, char character) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < x; i++) {
stringBuilder.append(character);
}
return stringBuilder.toString();
}
}
I am not sure i understood currectly but you're trying to print squares without swing? But you're using system.out.print. Its a bit confusing but if its for learning programming homework from school here you go to make a square with for loops:
for (int i = 0; i < width; i++) {
System.out.print("*");
}
System.out.println();
for (int i = 0; i < height - 2; i++) {
System.out.print("*");
for (int j = 0; j < width - 2; j++) {
System.out.print(" ");
}
System.out.println("*");
}
for (int i = 0; i < width; i++) {
System.out.print("*");
}
System.out.println();
}
But if you want to make rectangles (Because you mentioned swing) you should check java.awt.Rectangle2D and Rectangle:
https://docs.oracle.com/javase/7/docs/api/java/awt/Rectangle.html
You can do it in this way:
void rectangleDraw(int x, int y) {
String line1 = new String(new byte[x]).replaceAll("", ".");
String line2 = "." + new String(new byte[x - 2]).replaceAll("", " ") + ".";
System.out.println(line1);
for (int i = x; i > y; i--) {
System.out.println(line2);
}
System.out.println(line1);
}

generating different distinct permutation of fixed length(L)

I am writing a program to generate different permutation of an array with fixed length of 3.
The main problem I am facing is that it always generate dublicate permutation, How can i fix it without using Java set<>.
public class generatingCombination {
public static void main(String[] args) {
String s="ABCDEF";
printArray(s,0,new char[3], new boolean[s.length()]);
}
static void printArray(String s,int x,char []arr, boolean [] used){
if(x==3){
System.out.println(Arrays.toString(arr));
return;
}
else
{
for( int i=0;i<s.length();i++){
if(used[i]) continue;
arr[x]=s.charAt(i);
used[i]=true;
printArray(s, x+1, arr,used);
used[i]=false;
printArray(s, x+1, arr,used);
}
}
}
}
If you're only interested in permutations of length 3, then why not just:
public static void main(String[] args) {
String s = "ABCDEF";
for (int i = 0; i < s.length(); i++) {
for (int j = 0; j < s.length(); j++) {
if (j == i) {
continue;
}
for (int k = 0; k < s.length(); k++) {
if (k == i || k == j) {
continue;
}
System.out.println(Arrays.toString(new char[] { s.charAt(i), s.charAt(j), s.charAt(k)}));
}
}
}
}

java program to print number in integer as shuffle

Sorry if my question is not clear, lets say I have int a = 1234;.
How can I print as follows, for a number of any length?
123
132
213
231
321
312
Thanks in advance.
public class Test {
private static void swap(int[] p, int i, int j) {
int t= p[i];
p[i]= p[j];
p[j]= t;
return;
}
private static boolean nextPerm(int[] p) { // need p.length > 1
int n= p.length;
int i= n;
if (i-- < 1) return false;
for(;;) {
int ii= i--;
if (p[i] < p[ii]) {
int j= n;
while (!(p[i] < p[--j]));
swap(p, i, j);
for (j= n; j > ii; swap(p, --j, ii++));
return true;
}
if (i == 0) {
for (int j= n; j > i; swap(p, --j, i++));
return false;
}
}
}
public static void main(String[] args) {
int x = 123;
String s = "" + x;
int n = s.length();
int[] p = new int[n];
for (int i = 0; i < n; i++){
p[i] = i;
}
do {
for (int i = 0; i < n; i++){
System.out.print(s.charAt(p[i]));
}
System.out.println();
}
while (nextPerm(p));
}
}
finally I found, below is the code,
class ShuffleNumber {
private static int[] a = {1,2,3};
private static void print(int[] a) {
for (int i = 0; i < a.length; i++)
System.out.print(" " + a[i]);
System.out.println();
}
private static void shuffle(){
int[] b = (int[])a.clone();
for (int i = b.length - 1; i > 0; i--) {
int j = (int)Math.floor(Math.random() * (i+1));
int temp = b[j];
b[j] = b[i];
b[i] = temp;
}
print(b);
}
public static void main(String[] args) {
for (int i = 0; i < 10; i++)
shuffle();
}
}

Categories