gpt4 book ai didi

Java Billboard 数组列表

转载 作者:行者123 更新时间:2023-12-02 05:55:09 25 4
gpt4 key购买 nike

我已经结束了这个 Java 控制台。当我添加新文本时,我可以放置一个单词,但当我尝试添加一个句子时,我收到错误!如果有人能看到我错过的东西或问题,我将非常感激。它适用于一个单词,但不适用于两个或更多单词。

这是两个类

第一个类

 package Billboard;

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Paul
*/
import java.util.Scanner;

public class BillboardMain {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
Billboard billboard = new Billboard();

while (true) {
System.out.println();
System.out.println();
System.out.println("\t Billboard Menu");
System.out.println();
System.out.println("Please select from the following billboard texts");

for (int i = 0; i < billboard.getMessages().size(); i++) {
System.out.println((i + 1) + ": "
+ billboard.getMessages().get(i));
}
System.out.println((billboard.getMessages().size() + 1)
+ ": Add new message.");
System.out.println((billboard.getMessages().size() + 2)
+ ": Show current text.");
System.out.println((billboard.getMessages().size() + 3) + ": Exit.");

System.out.println();
System.out.print("Choice: ");
int code = console.nextInt();

if (code == billboard.getMessages().size()+1) {
System.out.print("Enter new text here: ");
String newText = console.next();
billboard.addNewText(newText);
System.out.println("The new text message has been set to billboard");
} else if (code == billboard.getMessages().size() + 2) {
System.out.println("Current text is: " + billboard.getText());
} else if (code == billboard.getMessages().size() + 3) {
System.exit(0);
} else {
billboard.setText(code);
System.out.println("The text message has been set to billboard.");
}
}
}
}


The second class

package Billboard;

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/


/**
*
* @author Paul
*/
import java.util.ArrayList;
import java.util.List;

public class Billboard {
private String text;
private List<String> messages = new ArrayList<String>();

public Billboard() {
super();
messages.add("Neutral.");
messages.add("Enemies.");
messages.add("Friends.");
}

public List<String> getMessages() {
return messages;
}

public boolean addNewText(String newText) {
text = newText;
return messages.add(newText);
}

public String getText() {
return text;
}

public void setText(int index) {
if (index <= messages.size())
this.text = messages.get(index - 1);
else
throw new IndexOutOfBoundsException("Invalid message code.");
}

public String reverse() {
if (isEmpty())
return null;
else {
char[] chars = text.toCharArray();
char[] reverse = new char[chars.length];

for (int i = chars.length - 1, j = 0; i < 0; i--, j++) {
reverse[j] = chars[i];
}
return new String(reverse);
}
}

public String replace(char oldChar, char newChar) {
return text.replace(oldChar, newChar);
}

public String substring(int begin, int end) {
if (begin >= 0 && end < text.length()) {
return text.substring(begin, end);
} else
return null;
}

public boolean isEmpty() {
return text == null || text.isEmpty();
}
}

最佳答案

定义另一个扫描仪

扫描仪控制台1 = new Scanner(System.in);

并替换: String newText = console.next();其中:String newText = console1.nextLine();

应该可以

关于Java Billboard 数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23173875/

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