package triangle;import java.util.*;
public class Triangle1 {
public static void main(String args[]) {
int n;
System.out.println("Welcome to the Game of Triangle!");
System.out.println("Please enter a number between 1-20 !");
Scanner stdin = new Scanner(System.in);
n = stdin.nextInt();
while(n>0&&n<=20){
for(int i = 0; i <= n; i++) {
for (int j = 0; j < 2 * i - 1; j++) {
System.out.print("x");
}
System.out.println();
}
System.out.println("Thanks, here is the triangle with "+ n + " rows:");
break;
}
System.out.println("Please enter a number between 1-20 !");
}
}
输出的结果是:x
xxx
xxxxx
xxxxxxx
xxxxxxxxx
Thanks, here is the triangle with 5 rows:
不过,我想要的结果是:Thanks, here is the triangle with 5 rows: x
xxx
xxxxx
xxxxxxx
xxxxxxxxx
问题是:X前面怎么添加这个空格哈,这个代码怎么样?>?另外还有个题目就是:第三个是:
输入任意一串英文句子,要求显示出
这个句子包含的字母数,
占用的string数,
最长的一个单词占了多少character,
最后显示出每个字母出现的频率,如果没有出现的字母,则不显示频率. 输出例:
Enter a line of text;
Now is the time for all good men to come to the aid of their country This line contains 53 letters.
This line contains 16 string tokens.
The longest token has 7 characters.
The frequency of letters is
A -- 2
C -- 2
D -- 2
E -- 6
F -- 2
G -- 1
H -- 3
I -- 4
L -- 2
M -- 3
N -- 2
O -- 9
R -- 3
S -- 1
T -- 7
U -- 1
W -- 1
Y -- 1
这个程序怎么实现,能否好心人能提供一段完成的程序代码,在下不胜感激...
public class Triangle1 {
public static void main(String args[]) {
int n;
System.out.println("Welcome to the Game of Triangle!");
System.out.println("Please enter a number between 1-20 !");
Scanner stdin = new Scanner(System.in);
n = stdin.nextInt();
while(n>0&&n<=20){
for(int i = 0; i <= n; i++) {
for (int j = 0; j < 2 * i - 1; j++) {
System.out.print("x");
}
System.out.println();
}
System.out.println("Thanks, here is the triangle with "+ n + " rows:");
break;
}
System.out.println("Please enter a number between 1-20 !");
}
}
输出的结果是:x
xxx
xxxxx
xxxxxxx
xxxxxxxxx
Thanks, here is the triangle with 5 rows:
不过,我想要的结果是:Thanks, here is the triangle with 5 rows: x
xxx
xxxxx
xxxxxxx
xxxxxxxxx
问题是:X前面怎么添加这个空格哈,这个代码怎么样?>?另外还有个题目就是:第三个是:
输入任意一串英文句子,要求显示出
这个句子包含的字母数,
占用的string数,
最长的一个单词占了多少character,
最后显示出每个字母出现的频率,如果没有出现的字母,则不显示频率. 输出例:
Enter a line of text;
Now is the time for all good men to come to the aid of their country This line contains 53 letters.
This line contains 16 string tokens.
The longest token has 7 characters.
The frequency of letters is
A -- 2
C -- 2
D -- 2
E -- 6
F -- 2
G -- 1
H -- 3
I -- 4
L -- 2
M -- 3
N -- 2
O -- 9
R -- 3
S -- 1
T -- 7
U -- 1
W -- 1
Y -- 1
这个程序怎么实现,能否好心人能提供一段完成的程序代码,在下不胜感激...
for(int i = 0; i <= n; i++) {
// 你在每行打印空格就可以了。
for(int k=0; k<n-i;k++) {
System.out.print(" ");
}
for (int j = 0; j < 2 * i - 1; j++) {
System.out.print("x");
}
System.out.println();
}
public static void main(String[] args){
int n=5;
for(n=5;n>0;n--){
for(int i=1;i<=n;i++){
System.out.print(' ');
}
for(int b=1;b<=2*(6-n)-1;b++){
System.out.print("*");}
System.out.println();
}
}
}
测试结果: *
***
*****
*******
*********
import java.util.Arrays;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class TriangleTest{
public static int countNum(String str){
int i=0;
Matcher m=Pattern.compile("\\w").matcher(str);
while(m.find())
i++;
return i;
}
public static int countFrequency(char c,String s){
int i=0;
Matcher m=Pattern.compile(c+"").matcher(s.toLowerCase());
while(m.find())
i++;
return i;
}
public static void main(String[] args){
Scanner sca=new Scanner(System.in);
System.out.println("Enter a line of text; ");
String str=sca.nextLine();
String s[]=str.split(" ");
str=str.replaceAll(" ", "");
System.out.println("This line contains "+str.length()+" letters");
System.out.println("This line contains "+s.length+" string tokens");
int result[]=new int[s.length];
int init=0;
for(String string:s)
result[init++]=countNum(string);
Arrays.sort(result);
System.out.println("The longest token has "+result[0]+" characters");
System.out.println("The frequency of letters is ");
for(int i='a';i<='z';i++)
System.out.println((char)(i)+"--"+countFrequency((char)i,str));
}
}
测试结果:
Enter a line of text;
Now is the time for all good men to come to the aid of their country
This line contains 53 letters
This line contains 16 string tokens
The longest token has 2 characters
The frequency of letters is
a--2
b--0
c--2
d--2
e--6
f--2
g--1
h--3
i--4
j--0
k--0
l--2
m--3
n--3
o--9
p--0
q--0
r--3
s--1
t--7
u--1
v--0
w--1
x--0
y--1
z--0
public static void main(String args[]) {
int n; System.out.println("Welcome to the Game of Triangle!");
System.out.println("Please enter a number between 1-20 !");
Scanner stdin = new Scanner(System.in);
n = stdin.nextInt(); while (n > 0 && n <= 20) {
for (int i = 0; i <= n; i++) {
for (int j = 0; j < (n-i); j++) {
System.out.print(" ");
}
for (int j = (n-i); j <=n; j++){
if(j==n)
System.out.print("*");
else
System.out.print("**");
}
System.out.println();
}
System.out.println("Thanks, here is the triangle with " + n
+ " rows:");
break;
}
System.out.println("Please enter a number between 1-20 !");
}
}
你的代码上改的,没时间细想,改的有些多