Friday, June 20, 2014

How to make a java program with output a triangle of numbers?




Lindsey


Hey everyone, I'm trying to help a friend with his program as he is struggling in his java class. it has been awhile since I've used java and while I am close, the math isn't lining up.

We are both really close.

We are trying to create a triangle that looks like this if a user enters 5:
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15


So far we got this:
1
2 6
3 7 8
4 8 9 11
5 9 10 12 15

Here is the code:

public static void triangle2(int n)
{

//check the input and return an error message if erronous input
if (n<1){
System.out.println("Please enter a value greater than or equal to 1.");
return;
}

int val=1;

for (int i=1; i<=n; i++)
{
System.out.printf("%3d",val);
int c = 1;
int y = i+(n-c);
for (int j=1; j<i; j++){
System.out.printf("%3d", y);
// y= y-j;
y = y+c;
c++;
}
System.out.println();
val++;
}
}

Thank you :D



Answer
Yes, you are close ... try ...

import java.util.Scanner;

public class triangle2
{
public static void triangle2(int n)
{

//check the input and return an error message if erronous input
if (n<1){
System.out.println("Please enter a value greater than or equal to 1.");
return;
}

int val=1;

for (int i=1; i<=n; i++)
{
System.out.printf("%3d",val);
int c = 1;
int y = i+(n-c);
for (int j=1; j<i; j++){
System.out.printf("%3d", y);
// y= y-j;
y = y+c;
c++;
}
System.out.println();
val++;
}
}

public static void main(String[]args) { // main function
int nsize = 0, i;

// Check for argument 1 number rows in triangle
if (args.length > 0) {
for(i=0; i<args[0].toString().length(); i++) {
if (args[0].substring(i,1).compareTo("0") >= 0 &&
args[0].substring(i,1).compareTo("9") <= 0) {
nsize = nsize;
} else if (!args[0].substring(i,1).equals("-")) {
nsize--;
}
}
if (nsize == 0) nsize = Integer.parseInt(args[0]);
}
if (nsize <= 0) {
Scanner inputscanner = new Scanner(System.in);
System.out.print("How many rows are there in your triangle? ");
nsize = inputscanner.nextInt();
}
triangle2(nsize);
} // end of main function

}

what are computer inputs and out puts divices mention atleast 8 on each.?




ymkumba





Answer
Text input devices-
Keyboard, Typing
Computer keyboard
Handwriting recognition
Optical character recognition
Speech recognition
Chorded keyboard
GKOS keyboard
Keyer
Telegraph key
Vibroplex


Pointing devices-
Computer mouse
Trackball
Touchpad
Pointing stick
Graphics tablet (or digitizing tablet)
Stylus
Light pen
Light gun
Cyberglove
Touch screen
Head pointer
Eye gaze/eye tracking

Gaming devices -
Joystick
Gamepad (or joypad)
paddle
Power Pad

Image, Video input devices-
Image scanner
3D scanner
Digital camera
Digital camcorder
Webcam
Digital video recorder

Audio input devices -
Digital dictaphone
Microphone
Digital audio recorder


Image, Video output devices-
Computer printer
Plotter
Computer display

Audio output devices-
Computer speaker




Powered by Yahoo! Answers

Title Post: How to make a java program with output a triangle of numbers?
Rating: 100% based on 99998 ratings. 5 user reviews.
Author: Yukie

Thanks For Coming To My Blog

No comments:

Post a Comment