在一行中显示多个变量?
Displaying multiple variables on single line?
所以我正在编写一个程序,该程序读取最长 80 个字符的消息,并使用 push、pop 和 isempty 方法将其显示回用户。唯一的问题是,一行中将打印一个变量,因此向后的消息一次一个字母地垂直向下显示在屏幕上。代码如下,有人可以告诉我正确的命令或需要修复的地方吗?
public class StackUser
{
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args)
{
int length;
int I = 0;
char currChar;
int Max = 80;
myStack Placeholder;
Placeholder = new myStack();
System.out.println("Please enter any word or sentence up to a maximum of 80 characters long.");
String userEnter = keyboard.nextLine();
length = userEnter.length();
if (length < Max)
{
while (I < length)
{
char res = userEnter.charAt(I);
Placeholder.PushChar(res);
I = I + 1;
}
}
if (Max < length)
{
while (I < Max)
{
char res = userEnter.charAt(I);
Placeholder.PushChar(res);
I = I + 1;
}
}
while (Placeholder.IsEmpty() != true)
{
currChar = Placeholder.PopChar();
System.out.println("Here is your message backwards:" + currChar);
}
}
}
只需使用 print 而不是 println:
System.out.print("Here is your message backwards: ");
while (Placeholder.IsEmpty() != true) {
currChar = Placeholder.PopChar();
System.out.print(currChar);
}
System.out.println();
所以我正在编写一个程序,该程序读取最长 80 个字符的消息,并使用 push、pop 和 isempty 方法将其显示回用户。唯一的问题是,一行中将打印一个变量,因此向后的消息一次一个字母地垂直向下显示在屏幕上。代码如下,有人可以告诉我正确的命令或需要修复的地方吗?
public class StackUser
{
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args)
{
int length;
int I = 0;
char currChar;
int Max = 80;
myStack Placeholder;
Placeholder = new myStack();
System.out.println("Please enter any word or sentence up to a maximum of 80 characters long.");
String userEnter = keyboard.nextLine();
length = userEnter.length();
if (length < Max)
{
while (I < length)
{
char res = userEnter.charAt(I);
Placeholder.PushChar(res);
I = I + 1;
}
}
if (Max < length)
{
while (I < Max)
{
char res = userEnter.charAt(I);
Placeholder.PushChar(res);
I = I + 1;
}
}
while (Placeholder.IsEmpty() != true)
{
currChar = Placeholder.PopChar();
System.out.println("Here is your message backwards:" + currChar);
}
}
}
只需使用 print 而不是 println:
System.out.print("Here is your message backwards: ");
while (Placeholder.IsEmpty() != true) {
currChar = Placeholder.PopChar();
System.out.print(currChar);
}
System.out.println();