for my second program, I need to produce a triangle when the user inputs a number.
Here are some sample input and corresponding output:
Welcome to the equilateral triangle drawing program.
Enter the length of the side:
4
Here you go:
* * * *
* * *
* *
*
Here is different run:
Welcome to the equilateral triangle drawing program.
Enter the length of the side:
7
Here you go:
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
I have no idea where to begin/what methods to use. it's my 2nd program which I think is a big jump from my last one, which was to calculate euclidean distance.
also, for my third lab, I have to create a program that takes the input of a user and spits out the pig latin version. I'm already past the due date on that, but I still want to understand what my errors were.
import java.util.*;
import java.io.*;
class Pig{
public static void main (String[] args)
{
System.out.print("Welcome to the Pig, please enter a");
System.out.println("word/sentence/phrase.");
Scanner scan = new Scanner(System.in);
int x;
String goagain, sentence, end, transfer;
String vowel = "way"; //suffix for words beginning with vowel
String consonant = "ay"; //suffix for words beginning with consonant
do{
while(scan.hasNext())
{
sentence = scan.next(); //input to be oinkified
sentence = sentence.toLowerCase();
for( x = 0; x < sentence.length(); x++)
{
if(sentence.charAt(0)=='a'||sentence.charAt(0)=='e'||
sentence.charAt(0)=='i'||sentence.charAt(0)=='o'||
sentence.charAt(0)=='u')
{
System.out.print(sentence+vowel+" ");
break;
}
else if(sentence.charAt(x)=='a'||sentence.charAt(x)=='e'||
sentence.charAt(x)=='i'||sentence.charAt(x)=='o'||
sentence.charAt(x)=='u')
{
transfer = sentence.substring(0, x);
end = sentence.substring(x, sentence.length());
System.out.println(end+transfer+consonant+" ");
break;
}
}
}
System.out.println("Do you want to translate more?");
goagain = scan.next();
} while(goagain == "yes");
System.out.println("Goodbye!");
}
}
writing programs like "hello world!" and area and distance calculators was fun but now it's getting really hard X_X, at least for me as I've got no previous experience.
thanks in advance
This post has been edited by macosxnerd101: 29 January 2011 - 11:43 PM
Reason for edit:: Added code tags around the triangles

New Topic/Question
Reply



MultiQuote







|