如何使条件重启和结束程序?

时间:2014-05-04 13:47:36

标签: java

如果输入“是”,我希望能够使我的if语句重新启动程序,如果“No”被估算则结束程序,如果“是”或“否”,则说“大写是重要的”输入。

Scanner sc = new Scanner(System.in);
    final String vowels = "aeiouAEIOU";
    System.out.println("Enter your word:");
    String word = sc.nextLine();
    while (!word.equalsIgnoreCase("done"))
    {
        String beforVowel = "";
        int cut = 0;
        while (cut < word.length() && !vowels.contains("" + word.charAt(cut)))
        {
            beforVowel += word.charAt(cut);
            cut++;
        }
        if (cut == 0)
        {
            cut = 1;
            word += word.charAt(0) + "w";
        }
        System.out.println(word.substring(cut) + beforVowel + "ay");

      Scanner keyboard = new Scanner (System.in);
      System.out.print("Do you wish to convert another setence ");
      String name = keyboard.nextLine();
      if(name = "Yes"){
          new Prog508a().launch;
      }
      if(name = "No"){
      break;
      }
      else{
      System.out.print("Capitalzation is important.");
      }

1 个答案:

答案 0 :(得分:0)

String name = keyboard.nextLine();


if(name = "Yes"){
      new Prog508a().launch;
  }
  if(name = "No"){
  break;
  }

这实际上不是一个很好的方法,我建议你改用方法返回。

另外你不应该使用

if(name = "Yes")

由于不是比较,更好的方法是

if(name.contains("y") || name.contains("Y")){

因为这允许用户输入以y或Y开头的任何内容,但是如果你真的想要使用&#39;是&#39;那你应该做

if(name.equals("Yes"))