java中如果要连续打印相同的字符,如何书写 比如要打印8个空格 String indent=" "; System.out.println(in

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java中如果要连续打印相同的字符,如何书写 比如要打印8个空格 String indent=" "; System.out.println(in相关的知识,希望对你有一定的参考价值。

java中如果要连续打印相同的字符,如何书写 比如要打印8个空格
String indent=" ";
System.out.println(indent+indent+indent+indent+indent+indent+indent+indent);
是否有简便写法
循环是好方法,可否告诉我有没有8*indent这样的简便写法,直接打印出8个空格

参考技术A import java.util.Scanner;

public class Printer
static String strAmount;
static String strEnterChar;

public static void init()
Scanner amount1 = new Scanner(System.in);
String strAmount1 = amount1.nextLine();
if (!strAmount1.matches("[0-9]+"))
System.out.println("请输入正确的次数:");
init();


if (strAmount1.matches("[0-9]+"))
int number = Integer.parseInt(strAmount1);
for (int i = 0; i < number; i++)
System.out.print(strEnterChar + "\t");

init();



public static void main(String[] args)
System.out.println("请输入要打印的字符:");
Scanner enterChar = new Scanner(System.in);
strEnterChar = enterChar.nextLine();
System.out.println("请输入要打印的次数:");
Scanner amount = new Scanner(System.in);
strAmount = amount.nextLine();
if (!strAmount.matches("[0-9]+"))
System.out.println("请输入正确的次数:");
init();
return;

int number = Integer.parseInt(strAmount);
for (int i = 0; i < number; i++)
System.out.print(strEnterChar + "\t");



以上是一段,可以输入要打印的字符,并输入打印个数(还可以更换打印个数)的代码。希望对你有帮助。
参考技术B 做个for循环不就好了
for(int i=0 ;i<8;i++)
System.out.print(" ");

貌似没有8*indent这样的

不过你可以自己写一个这样的方法
比如:
public String addString(int num,String s)

for(int i=0;i<num;i++)

s+=s;


num出现次数 s出现的字符串
这种方法也比较不错
参考技术C 可以使用 apache 的 common.lang jar 包
System.out.println(StringUtils.repeat(indent,8));
参考技术D 你是要字符串连在一起的效果? StringBuffer. a = new StringBuffer();
for(打印次数)
a.append(indent);

Out.print(a.toString());本回答被提问者和网友采纳

oracle中,查找存在任意三个相同且连续的字母的字符串,请问各位大神如何解决?

比如某一个字段,有'tttest','test','ttttest','teeeest','tesst'等值,查出'tttest','ttttest','teeeest'即可。
假设您并不知道是某一字符串,而需要您在某一字段中查找字符串存在任意三个相同且连续的字母的字符串。

这个正则表达式还真不会写,期待高手回答。
不过如果急,可以变通下,用个存储过程实现:
创建一个临时字典表,插入筛选规则数据:‘aaa’,'bbb','ccc',,,,,'zzz'
然后通过select语句筛选出包含这些字符串的数据:
select 字段名 from 表名 a where exists(select * from 临时表 where a.字段名 like '%'+临时字段+'%')
或者这样写:
select 字段名 from 表名 where regexp_like(字段名,'(a3|b3|c3||d3|e3|f3|g3|h3|i3|j3|k3|l3|m3|n3|o3|p3|q3|r3|s3|t3|u3|v3)|w3|x3|y3|z3')追问

我就是想用正则表达式解决这个问题。

参考技术A 自己写个函数判断某个字符串中最大连续字数然后在where语句中判断不就可以了 参考技术B select 字段名 from 表名 where 字段名 like '%ttt%' or '%eee%'

以上是关于java中如果要连续打印相同的字符,如何书写 比如要打印8个空格 String indent=" "; System.out.println(in的主要内容,如果未能解决你的问题,请参考以下文章

请问斑马888-TT的打印机如何连续打印?

oracle中,查找存在任意三个相同且连续的字母的字符串,请问各位大神如何解决?

java或者正则表达式如何判断字符串不是相同数字或者字母?不能全是连续数字?(最好是java)

java 编程基础

JAVA字符串中,只要有连续的空格就去掉且但要保留一个空格;如果只是一个空格就不用处理;怎么实现?

面试:字符串字典序最大的子序列