Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

string - java repeat character

I am a java beginner, please keep that in mind. I have to make a program that reads a number, and then displays that amount of exclamation mark "!".

This is what i have:

import java.util.Scanner;
import java.io.PrintStream;

class E_HerhaalKarakter1 {

 PrintStream out;

 E_HerhaalKarakter1 () {
  out = new PrintStream(System.out);
 }

 String printUitroeptekens (int aantal) {
  String output = "!"

  for (int i = 0; i <= aantal; i++) {
   output.concat("!");
  }
  return output;
 }

 void start () {
  Scanner in = new Scanner(System.in);

  out.printf("Hoeveel uitroeptekens wilt u weergeven?
");

  if(in.hasNext()) {
   out.printf("baldbla");
   printUitroeptekens(in.nextInt());
   out.printf("%s",output);
  }
 }

 public static void main (String[] argv) {
  new E_HerhaalKarakter1().start();
 }
}

Thank you

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you do actually have the requirement to create a string which contains X number of exclamation marks, then here's a way to do it without repeated concatenation:

char[] exmarks = new char[aantal];
Arrays.fill(exmarks, '!');
String exmarksString = new String(exmarks);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...