Java 水平打印循环输出?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22060032/
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
Print loop output horizontally?
提问by Kar900
Hey guys I have to write a program for class that asks me to produce a bar graph based on how many cars a salesperson sold for the month. An example is this:
嘿伙计们,我必须为课堂编写一个程序,该程序要求我根据销售人员当月销售的汽车数量生成条形图。一个例子是这样的:
Pam XXXX
Leo XXXXXX
I'm almost there but not quite. As of right now I can get one X next to the salespersons name but any others are printed underneath on separate lines.
我快到了,但还没到。截至目前,我可以在销售人员姓名旁边得到一个 X,但其他任何人都打印在不同的行下方。
Pam X
X
X
If you can help me get those other X's on the same line I would really appreciate it. Thank you for your input!
如果您能帮我将其他 X 放在同一条线上,我将不胜感激。谢谢您的意见!
import java.util.Scanner;
public class BarGraph
{
public static int Pam = 0;
public static int Leo = 0;
public static int Kim = 0;
public static int Bob = 0;
public static Scanner kb = new Scanner(System.in);
public static void main(String args[])
{
System.out.println("Enter number of cars sold by Pam ");
Pam = kb.nextInt();
kb.nextLine();
System.out.println("Enter number of cars sold by Leo ");
Leo = kb.nextInt();
kb.nextLine();
System.out.println("Enter number of cars sold by Kim ");
Kim = kb.nextInt();
kb.nextLine();
System.out.println("Enter number of cars sold by Bob ");
Bob = kb.nextInt();
kb.nextLine();
System.out.print("Pam "); pamsCars();
System.out.print("Leo "); leosCars();
System.out.print("Kim "); kimsCars();
System.out.print("Bob "); bobsCars();
}
public static void leosCars()
{
for(int i=0; i < Leo; i++)
{
printX();
}
}
public static void kimsCars()
{
for(int i=0; i < Kim; i++)
{
printX();
}
}
public static void pamsCars()
{
for(int i=0; i < Pam; i++)
{
printX();
}
}
public static void bobsCars()
{
for(int i=0; i < Bob; i++)
{
printX();
}
}
public static void printX()
{
System.out.println("X");
}
}
采纳答案by Phil Schwartz
You are using System.out.println("X");
This automatically appends a newline character to the end of the string.
您正在使用System.out.println("X");
这会自动将换行符附加到字符串的末尾。
Instead use System.out.print("X");
to print the X's next to each other.
而是用于System.out.print("X");
打印彼此相邻的 X。
回答by Saher Ahwal
use print
instead of println
in your printX function to be specific.
使用print
而不是println
在您的 printX 函数中进行具体说明。
since println prints and adds break (newline)
因为 println 打印并添加 break (换行符)
回答by TheLostMind
public static void printX()
{
System.out.println("X"); // println() puts newline character.. use print()
}
回答by mok
make these changes:
进行这些更改:
System.out.print("Pam "); pamsCars();System.out.println("");
and
和
public static void printX()
{
System.out.print("X");
}
As the others have said, the "println()" method adds a "\n" to the end of the line.
正如其他人所说,“println()”方法在行尾添加了一个“\n”。
回答by anonymous
Change your printX() method not to print newline and change the printCar() signature to accept name and number of X to print.
更改您的 printX() 方法不打印换行符并更改 printCar() 签名以接受要打印的 X 的名称和数量。
public static void printX()
{
System.out.print("X");
}
public static void printCars(String name, int num)
{
System.out.print(name);
for(int i=0; i < num; i++)
{
printX();
}
System.out.println();
}
Now you can call them like
现在你可以这样称呼他们
printCars("Pam ",Pam));
printCars("Bob ",Bob));
回答by sneufeld
couldn't you use a
你不能用
System.out.printf("%s",x);
as well?
还有?
or
或者
System.out.printf("%n%s",x);
i didn't try the loop myself but i'm pretty sure one of the two would also be an alternative... the %n will give you the same result as println as long as you go with printf and %n... its the same escape sequence as \n when you're using println.
我自己没有尝试过循环,但我很确定这两者之一也将是替代方案......只要你使用 printf 和 %n ,%n 就会给你与 println 相同的结果......当您使用 println 时,它与 \n 相同的转义序列。
i just learned that a month ago in class, so i figured i'd share what basic knowledge i have... mid-terms next week so i have programming fever.
我一个月前在课堂上才知道,所以我想我会分享我所拥有的基本知识......下周期中考试,所以我有编程热。
回答by MikeT
How about the following (sorry not tested, so in principle only and more of a meansd of shjowing an alternative to loops).
下面怎么样(抱歉没有测试,所以原则上只是一种替代循环的方法)。
Of course a flaw is that a user could input more than 10.
当然一个缺陷是用户可以输入超过 10 个。
There again what if the user input 10,000? (would you really want to print 10,000 X's). Perhaps something to think about.
如果用户输入 10,000 又会怎样?(您真的想打印 10,000 个 X 吗)。也许有什么需要考虑的。
import java.util.Scanner;
public class BarGraph
{
public static int Pam = 0;
public static int Leo = 0;
public static int Kim = 0;
public static int Bob = 0;
public static Scanner kb = new Scanner(System.in);
//Assume max sales of 10
public static final String maxsales = "XXXXXXXXXX"; //+++++NEW
public static void main(String args[])
{
System.out.println("Enter number of cars sold by Pam ");
Pam = kb.nextInt();
kb.nextLine();
System.out.println("Enter number of cars sold by Leo ");
Leo = kb.nextInt();
kb.nextLine();
System.out.println("Enter number of cars sold by Kim ");
Kim = kb.nextInt();
kb.nextLine();
System.out.println("Enter number of cars sold by Bob ");
Bob = kb.nextInt();
kb.nextLine();
printCars("Pam",pam); //+++++NEW
printCars("Leo",leo); //+++++NEW
printCars("Kim",kim); //+++++NEW
printcars("Bob",bob); //+++++NEW
}
//+++++NEW All other methods/functions replaced by this one
public static void printCars(String person, int sales) {
system.out.println(person + " " + maxsales.substr(sales));
}
}
回答by sachin chaskar
int n = 20;
for(int i = 0; i <= n; i++) {
System.out.print(" " + i);
}