gpt4 book ai didi

java - 在单行上显示多个变量?

转载 作者:行者123 更新时间:2023-11-30 03:34:21 25 4
gpt4 key购买 nike

因此,我正在编写一个程序,该程序读取最长 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();

关于java - 在单行上显示多个变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28331903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com