Java- Count from 1 - 5000 and put it in 5 columns - java

Can't figure it out. Need to use a for/while statement. First month learning Java so please keep it simple. Thanks
This is what I have so far:
public class prog6c
{
public static void main (String [] args)
{
int x = 1;
while (x<5000){
for (x=1; x<5; ++x)
System.out.print(" "+x);
}
}
}

Your problem is that after going through the for loop for the first time x<=5. But the next time you are setting it back to 1.
I would suggest a loop until 5000 which prints a line break when x can be divide by 5
for (x = 1; x < 5000; ++x)
{
System.out.print(" ");
System.out.print(x);
if (x % 5 == 0)
{
System.out.println();
}
}

If I am understanding the purpose of what you are trying to achieve, check out the following code sample. I haven't tested it out. But it should work on most part:
public class Prog6c {
public static void main(String[] args) {
int x = 1;
int y = 1;
while (x<5000){
for (y=1; y<=5; ++y)
System.out.print(x++ + " ");
System.out.print("\n");
}
}
}

Try doing:
int x = 1;
int columns = 1;
while(x < 5000){
for(x = 1; x < 5; x++){
while(columns < 5){
if(columns ==){
columns == 0
System.out.print("\n")
}
columns++;
System.out.print(" " + x);
}
}
}
This code count by 0 to 5000 aligning in 5 columns per row.
Frank.

Related

(Java Number Pyramid) How do I get my numbers to have more spaces between them while lining up accurately when it is an integer is > 9?

I am having difficulty getting the desired output. I know there are problems that are similar to mine that is already posted, but I find it hard to relate my code to their solutions without a massive overhaul.
The solution for my class assignment:
Supposed to continue until every direction of pyramid equals 1
My second method "spaces" is redundant and I am not sure why. Any help would be appreciated.
Blockq
public static void main(String[] args) {
numPar();
spaces();
}
private static void spaces() {
int x = 0;
if(x > 0 && x < 10) {
System.out.print(" ");
} else if (x > 10 && x < 99) {
System.out.print(" ");
} else if (x > 99) {
System.out.print(" ");
}
}
private static void numPar() {
int spaces = 14;
for(int i = 0; i<= 7; i++) {
for(int u = 0; u<spaces; u++) {
System.out.print(" ");
}
spaces--;
spaces--;
for(int j = 0 ; j <i ; j++) {
System.out.print(""+ (int) Math.pow(2,j)+" ");
}
for(int k = i ; k >=0 ; k--) {
System.out.print(""+ (int) Math.pow(2,k)+" ");
}
System.out.println("");
}
}
}
I made every number take 3 places using String.format("%3s", (int) Math.pow(2, j)). You can make it dynamic by replacing the number 3 here with the length of the largest number you'll print. I also changed the number of spaces in your print statements. Here is the full code that prints an evenly spaced pyramid:-
public static void main(String[] args) {
numPar();
spaces();
}
private static void spaces() {
int x = 0;
if (x > 0 && x < 10) {
System.out.print(" ");
} else if (x > 10 && x < 99) {
System.out.print(" ");
} else if (x > 99) {
System.out.print(" ");
}
}
private static void numPar() {
int spaces = 14;
for (int i = 0; i <= 7; i++) {
for (int u = 0; u < spaces; u++) {
System.out.print(" ");
}
spaces--;
spaces--;
for (int j = 0; j < i; j++) {
System.out.print("" + String.format("%3s", (int) Math.pow(2, j)) + " ");
}
for (int k = i; k >= 0; k--) {
System.out.print("" + String.format("%3s", (int) Math.pow(2, k)) + " ");
}
System.out.println("");
}
}
String.format explanation:-
String.format("%3s", str) will print the string str, padding it with spaces, to make the total length 3 if it's less than 3. Note that you can write anything instead of 3 - I used 3 because your biggest number was of length 3.
So "A" will be printed as "_ _ A" (2 spaces), and "Ab" will be printed as "_ Ab" (1 space).
I just replaced str with your Math.pow(2, j).

writiting the code with the appropriate variable values

I should Write a program Square.java that declares and initalizes a variable n (with n ≥ 2) and prints out a two dimensional n-by-n triangular pattern as shown below (where n=6).
public class Square {
public static void main (String[]args){
for (int Line=1; Line<= 6; Line ++){
for (int n=1; n<= (Line-1*1); n++) {
System.out.print ("# ");
}
for (int n=1; n<= (7-Line); n++){
System.out.print ("$ ");
}
System.out.println ("");
}
}
}
I can't manage to write the code with n>=6 or n=2
I am guessing you are forgetting to update the 7 when you change the 6. Put your size in a variable as shown below. Seems to work for me for 2 and any other size
public static void main(String[] args) {
int size = 6;
for (int Line = 1; Line <= size; Line++) {
for (int n = 1; n <= (Line - 1 * 1); n++) {
System.out.print("# ");
}
for (int n = 1; n <= ((size + 1) - Line); n++) {
System.out.print("$ ");
}
System.out.println("");
}
}
The size of the square must be an attribut of the class
The result :
public class Square {
public int size;
public Square(int size) {
this.size = size;
}
public void goSquare(){
for (int line=1; line<= size; line++){
for (int n=1; n<= (line-1); n++) {
System.out.print ("# ");
}
for (int n=1; n<= ((size+1)-line); n++){
System.out.print ("$ ");
}
System.out.println ("");
}
}
public static void main (String[]args){
Square square = new Square(6);
square.goSquare();
}
}
In your actual code 7-Line is what blocks values >=6 and for value =2 your code works great but it only prints one element(point).
Another thing if you use Line-1*1 you mean (Line-1)*1 which is equivalent to write Line-1.
Also in 7-Line the value 7 should be an attribute to be used in the first loop too in Line <= 7, you can see it in this working code:
int max = 2;
for (int Line = 1; Line <= max; Line++) {
for (int n = 1; n <= (Line - 1); n++) {
System.out.print("# ");
}
for (int n = 1; n <= (max - Line); n++) {
System.out.print("$ ");
}
System.out.println("");
}
And this is an Ideone Working Example of the code.

Input loop number missing loop

Whenever I input x (number of empoyee) as any number it always short in 1 loop when asking the employee salary. And if i put x as 1 it doesn't ask me.
Need help how to fix it.
class salary {
public static void main(String[] args) {
int x = 0;
int y;
System.out.println("Enter how many employee");
x = EasyIn.getInt();
for (int i = 1; i < x; ++i) {
System.out.println("Enter the salary of employee " + i);
y = EasyIn.getInt();
if (y < 20000) {
System.out.println("This employee Bonus Rate is 7%");
}
}
}
}
You have an error in your for loop. It should be:
for(int i = 0; i < x; ++i)
or:
for(int i = 1; i <= x; ++i)
What is EasyIn?
There might also be an issue with your EasyIn class.
Your problem is here
for (int i = 1; i < x; ++i)
Think about the terminating condition of the for loop.
Try something like this -
// start at 0.
for (int i = 0; i < x; ++i)
{
// add one for display.
System.out.println("Enter the salary of employee " + (1+i));
y = EasyIn.getInt();
if(y < 20000)
{
System.out.println("This employee Bonus Rate is 7%");
}
}
you are starting i from 1 instead of zero so it will always short by 1.
Simply change int i=1; to int i=0;

Spaces at the start of every other line in a for loop

I'm attempting to get spaces at the beginning of every other line of the output of this program:
import java.util.Scanner;
public class Checkerboard
{
public static void main(String[] args)
{
int num;
Scanner input = new Scanner(System.in);
System.out.println ("What is the integer?");
num = input.nextInt();
for (int x = 0; x < num; x++)
{
for (int y = 0; y < num; y++)
{
System.out.print("A ");
}
System.out.println(" ");
}
}
}
The output of this program, if the user enters "4" for example, is:
A A A A A
A A A A A
A A A A A
A A A A A
I'm trying to get it to look like this:
A A A A A
A A A A A
A A A A A
A A A A A
Print the extra space only for even values of x
for (int x = 0; x < num; x++)
{
for (int y = 0; y < num; y++)
{
System.out.print("A ");
}
System.out.println("");
if (x % 2 == 0){
System.out.print(" ");
}
}
To be more explicit, the % operator (modulus) will return the remainder of a division operation. So any even number modulu 2 will return 0. Otherwise it would return 1. 13 % 10 == 3 because 13 / 10 = 1 remainder of 3.

loops and probabtily

i have to Have the computer compute all the possible ways three dice can be thrown: 1 + 1 + 1, 1 + 1 + 2, 1 + 1 + 3, etc. Add up each of these possibilities and see how many give nine as the result and how many give ten.
public class prog209b
{
public static void main(String []args){
int sum = 0;
int count = 0;
do{
for(int i = 1; i<=6; i++){
count +=1;
for(int y=1; y<=6; y++){
count += 1;
for(int x=1; x<=6; x++ ){
sum = i + y + x;
}
}
}
}while (sum == 10 && count == 27);{
System.out.println("There are " +count +" ways to get ten");
}
}
}
Thats what i came up with but i can get it to work correctly at all. instead of giving me that theres 27 ways it gives me like 42. Obviously im not doing this correctly. Please help me before i have an aneurysm
I'm not going to do your homework for you but:
You don't need the do/while loop - why would you need to keep going once you'd found all the possibilities?
You don't need to count all the possible dice rolls - you need to count how many give 9 as a total, and how many give 10. You could either do that with two variables, or you could make a method which took the "target" as a parameter
You don't need the sum other than right in the innermost loop - all you need to do is find out whether the sum of the values is equal to one of your target values, and increment the appropriate counter...
Your count += 1s are in the wrong place and your while (sum == 10 && count == 27) makes no sense.
int nine = 0
int ten = 0;
for(int i = 1; i<=6; i++){
for(int y=1; y<=6; y++){
for(int x=1; x<=6; x++ ){
sum = i + y + x;
if (sum == 9) nine++;
if (sum == 10) ten++;
}
}
}
You should do something like this:
for(int i = 1; i<=6; i++){
for(int y=1; y<=6; y++){
for(int x=1; x<=6; x++ ){
sum = i + y + x;
if (sum == 10)
count++;
}
}
}
System.out.println("There are " + count + " ways to make 10");

Categories

Resources