-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunWithLooping
More file actions
112 lines (81 loc) · 2.49 KB
/
FunWithLooping
File metadata and controls
112 lines (81 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/***********************************************************************
Program Name: Diamond.java
Programmer's Name: Robert Theriault
Program Description: Make a diamond with stars
***********************************************************************/
import java.util.Arrays;
import javax.swing.*;
public class Diamond {
/**
* @param args
*/
public static void main(String[] args)
{
String input = "";
int number = 0;
boolean true1 = true;
while (true1)
{
//Ask for radius and convert to integer
input = JOptionPane.showInputDialog("Enter an Odd Number between 3-15");
try//test in integer
{
number =(int) Double.parseDouble(input);
if (number < 3 || number > 15)//test if inbetween 3-15
{
JOptionPane.showMessageDialog(null, "You need to enter a number between 3-15");
}
else if (number % 2 == 0)//test if odd
{
JOptionPane.showMessageDialog(null, "You need to enter an Odd number");
}
else
{
true1 = false;
}
}
catch (NumberFormatException nfe)
{
JOptionPane.showMessageDialog(null, "You need to enter an Integer");
}
}
//find halfway point and create holderString to add *'s to String array
int half = number/2+1;
int halfHolder = half;
String holderString = "";
//create string array to hold *'s
String[] diamondString = new String[number];
Arrays.fill(diamondString, "");
int a = 0;//array holder
//Top half of the diamond
while (halfHolder != 0)
{
//add * to string at a
diamondString[a] = "*" + holderString;
holderString = holderString + "**";
halfHolder = halfHolder -1;
a = a + 1;//increment array spot
}
holderString = "*";//reset holderString gives odd number
//bottom half of diamond
while (a < number)
{
for (int s = a; s <= number-2; s++ )
{
holderString += "**" ;
}
diamondString[a] = holderString;
holderString = "*";//reset holderString gives odd number
a = a + 1;//increment array spot
}
//Create html string in order to display centered in JLabel
String message = "<html><body><div width='50px' align='center'>";
for (int i = 0; i < number; i++)
{
message = message + "<br>" + diamondString[i];
}
message = message + "</div></body></html>";
JLabel messageLabel = new JLabel(message);
JOptionPane.showMessageDialog(null, messageLabel);
}
}