如何重复输入字符串,直到用户输入关键字以停止Java

时间:2019-03-02 22:32:11

标签: java string for-loop methods java.util.scanner

我被困在字符串方法程序中,我的问题是我无法使循环停止,并且输入关键字后程序无法打印当前存储的输出。我不是要比较字符串,而是要输入多个字符串并添加一个单词,在这种情况下,在输入单词“ stop”之前,在字符串中添加“ not”。输入“停止”后。系统将输出存储的整个字符串。

这是程序的问题:  (StringConcat.java)此程序要求用户重复输入字符串。它应该将这些字符串连接在一起,但在用户输入的每对单词之间插入空格和单词“ not”。当用户输入字符串“ stop”时停止。显示最终的字符串。例如,程序输出可能类似于:

请输入一些字符串: “这样” “眼睛” “您” “有” “停止”

“没有眼睛,就是你没有”

到目前为止,这是我的代码:

import java.util.*;
public class StringConcat{

  public static void main(String [] args){

  Scanner sc = new Scanner(System.in);
  String s = new String();
     System.out.print("Please enter some Strings: ");
  for(int x=0; x<s.length(); x++){
     s = sc.nextLine();
     s = s + "not ";
     if(s == "stop"){
     System.out.println(s);
     break;
     }
     else{
     continue;
     }
   }
  }
}

2 个答案:

答案 0 :(得分:0)

使用namespace CC_Case_Maker { public partial class add_thing : Form { public string piccpath1 { get; set; } public string piccpath2 { get; set; } public string description { get; set; } public string titlee { get; set; } public add_thing() { InitializeComponent(); } private void label1_Click(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { string picpath = openFileDialog1.FileName; pictureBox1.Image = Image.FromFile(picpath); string[] extract = Regex.Split(picpath, "evidence"); string pipath2 = Regex.Replace(extract[1], "evidence", ""); piccpath1 = picpath; piccpath2 = pipath2; } } private void button3_Click(object sender, EventArgs e) { description = richTextBox1.Text; titlee = textBox1.Text; this.DialogResult = DialogResult.OK; this.Close(); } } } 循环。

为什么while循环?

通常,当我们不知道要执行的循环数时,总是必须使用while循环。在这种情况下,仅当用户输入“ while”时。

因此,您需要一个stop字段来保存用户单词。另外,我们可以使用数字字段来跟踪是第一个还是第二个单词,然后在“ not”单词后面加上一个单词。

然后,看一下这个示例:

String

这也可以使用for循环来完成,但我确实建议在这种情况下使用while循环。

编辑: 如您所见,我使用Scanner s = new Scanner(System.in); String currentAnswer = ""; String userWords = ""; int tracker = 0; while (!currentAnswer.equals("stop")){ currentAnswer = s.nextLine(); userWords += currentAnswer + " "; if (tracker % 2 != 0) { userWords += "not "; } tracker++; } System.put.println(userWords); 而不是equals()来比较两个字符串,因为我们要检查它的值,而不是它的对象相等性。

当我们使用==运算符时,我们试图检查两个对象是否以相同的内存地址为目标,但是我们只想知道两个Strings是否具有相同的值。

对于这种情况,知道我们可以使用其他方式进行比较是有效的,例如==甚至是Objects.equals()

选中this discussion,以了解有关比较字符串的更多信息。

答案 1 :(得分:0)

您的代码有几个问题:
(1)为什么您使用for循环并在s.length()(此时为s)的长度上迭代到0 )与您的问题无关?
您需要一个没有预定义迭代次数的循环,例如while (true),将以break退出。
(2)在每次迭代中,您都会获得用户的输入并将其存储在s中,因此您会丢失所有以前的值。
您需要一个单独的变量来存储用户的输入。
(3)不需要continue语句作为循环中的最后一条语句。
(4)因为在每次迭代时都在末尾添加“ not”,所以在循环结束后,您必须从s中删除最后一个“ not”
(5)在比较字符串时不要使用==。为此有一种方法equals()
这是我的解决方案:

Scanner sc = new Scanner(System.in);
String s = "";
System.out.print("Please enter some Strings: ");
while (true){
    String input = sc.nextLine();
    if(input.equalsIgnoreCase("stop"))
        break;
    s += input + " not ";
}
if (s.length() >= 5)
    s = s.substring(0, s.length() - 5);
System.out.println(s);
相关问题