JAVA:使用While循环打印出与读入数字一样多的字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12763699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JAVA: Use While Loop to print out character as many times as the number read in
提问by Leroy
Hello I'm having issues on my intro to Computer Science Lab Work:
你好,我在介绍计算机科学实验室工作时遇到了问题:
CharacterLine.java: Write a program that prints a line of characters. Prompt the user to enter a character and then ask for a number. If the number is lies than 1 or greater than 80, tell the user then to exit the program. Use a while Loop to print out the character as many times as the number read in. Example: Please enter a character: & Please enter a number: 15 Your line: &&&&&&&&&&&&&&&
CharacterLine.java:编写一个打印一行字符的程序。提示用户输入一个字符,然后要求输入一个数字。如果数字大于 1 或大于 80,则告诉用户退出程序。使用while循环打印出与读入数字一样多的字符。 示例:请输入一个字符:& 请输入一个数字:15 您的行:&&&&&&&&&&&&&
Here is my code:
这是我的代码:
import java.util.Scanner;
public class CharacterLine
{
public static void main(String[] args)
{
Scanner kb = new Scanner (System.in);
int number;
System.out.print("Please enter a character: ");
String character = kb.next();
int charact = character.length();
System.out.print("Please enter a number: ");
number = kb.nextInt();
while ( number <= 80 && number >= 1 ){
if ( number <= 80 && number >= 1 ) {
int bills = (charact * number);
System.out.println("Your line: " + charact++);
}
else {
System.out.println("error.");
}
System.out.println();
System.out.print("Please enter a number: ");
number = kb.nextInt();
}
if ( number > 80 ){
System.out.println("That number is too large");
}
else if ( number < 1 ){
System.out.println("That number is too small");
}
else{
System.out.println("error");
}
}
}
I am having issues in knowing how I can multiply the number that the user inputted and make the output the number times the one letter that the user inputted.
我在知道如何乘以用户输入的数字并使输出乘以用户输入的一个字母时遇到问题。
Thanks, William
谢谢,威廉
回答by Rohit Jain
You can use loop to iterate for given
number of times.. and print the character..
您可以使用循环迭代given
次数..并打印字符..
if ( number <= 80 && number >= 1 ) {
char myChar = '*';
// This while loop will run 15 times if value of number is 15..
while(number > 0) {
System.out.println(myChar);
number--; // Decrement the value of `number` by 1.
}
}
The above while loop says:
上面的while循环说:
While the value of number is greater than 0, execute the loop, and print the statement inside it.. After printing, decrement the value of number by 1, and check the condition once again... Continue this process while
number > 0
当 number 的值大于 0 时,执行循环,并打印其中的语句.. 打印后,将 number 的值减 1,并再次检查条件... 继续此过程,而
number > 0
回答by Robert P.
while(number>0){
System.out.print(character);
number--;
}
回答by Fildor
You misunderstood the task. You are notsupposed to multiply. You are supposed to print a certain character given by the user as many timeshe tells you, using a while
loop. So Inputs "a" and "4" would result in output "aaaa".
你误解了任务。你不应该繁殖。您应该使用循环打印用户给出的特定字符,他告诉您多少次while
。所以输入“a”和“4”将导致输出“aaaa”。