Welcome to OStack Knowledge Sharing 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 Answer

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...