【问题标题】:How do I make my program loop back to a certain point until a certain answer is reached [duplicate]如何让我的程序循环回到某个点,直到达到某个答案[重复]
【发布时间】:2018-09-14 16:49:22
【问题描述】:

我正在学习高级编程课程,目前正在学习 Java。我们应该设计一家商店。我的部分代码遇到了一些麻烦。我决定开一家卖不同种类苹果的商店。如果您运行我的代码并输入苹果的名称以了解更多信息,程序将结束。我希望它循环回到询问您是否想了解任何一种苹果的部分。这是我目前所拥有的。

System.out.println("Bradley's Apple Store");
System.out.println(" ");
System.out.println("Today we have eight different kinds of apples.  They are Alderman apples, Akane apples, Akero apples, Alkeme apples, Ambrosia Apples, Allington Pippin apples, and American Summer Pearmain apples");
System.out.println(" ");
System.out.println("If you would like to hear about any of the kinds of apples, type in the name of the apple.  If you would just like to purchase apples, then type purchase.");

String input = scan.nextLine();                                                     //For information on the apples
if (input.equalsIgnoreCase("Alderman"))
    System.out.println("An English (possible Scottish) culinary apple, thought to have originated in the 1920s. $4.00/lb");
if (input.equalsIgnoreCase("Akane"))
    System.out.println("One of the best early-season apples, better known in the USA than Europe, but would appeal to European tastes too. $6.00/lb");
if (input.equalsIgnoreCase("Akero"))
    System.out.println("An heirloom apple variety from Sweden, quite widely grown in Scandanavia, very handsome fruit. $2.00/lb");
if (input.equalsIgnoreCase("Alkeme"))
    System.out.println("A very attractive early Cox-style apple, slightly sharper than Cox, sometimes known as Early Windsor. $9.00/lb");
if (input.equalsIgnoreCase("Ambrosia"))
    System.out.println("A sweet modern apple variety from western Canada, quite similar to Golden Delicious. Discovered as a chance seedling in an orchard in British Columbia. $7.00/lb");
if (input.equalsIgnoreCase("Allington Pippin"))
    System.out.println("A versatile English apple, with a strong pineapple-like flavour, useful for both cooking and eating. $3.00/lb");
if (input.equalsIgnoreCase("American Summer Pearmain"))
    System.out.println("An excellent early eating apple, also good for cooking. Medium size, yellow-green fruit is flushed and streaked red to purple red. Sweet, very juicy flesh. $25.00/lb");
if (input.equals("purchase"))
    System.out.println("What would you like to buy?: ");


}

}

感谢您的帮助。

【问题讨论】:

  • while 循环应该可以解决问题
  • 使用while循环

标签: java loops


【解决方案1】:

您可以使用 while 循环检查直到用户输入“退出”

    while (true) {
        String input = scan.nextLine(); // For information on the apples

        /*
         *  Rest of your code
         */

        // break the loop if the user enters exit
        if (input.equalsIgnoreCase("exit"))
            break;
    }

【讨论】:

    【解决方案2】:

    您可以使用whiledo...while 循环。

    boolean exitLoop = false;
    do {
     String input = scan.nextLine(); //For information on the apples
     if (input.equalsIgnoreCase("Alderman")) {
      System.out.println("An English (possible Scottish) culinary apple, thought to have originated in the 1920s. $4.00/lb");
      exitLoop = true;
     }
     if (input.equalsIgnoreCase("Akane")) {
      System.out.println("One of the best early-season apples, better known in the USA than Europe, but would appeal to European tastes too. $6.00/lb");
      exitLoop = true;
     }
     if (input.equalsIgnoreCase("Akero")) {
      System.out.println("An heirloom apple variety from Sweden, quite widely grown in Scandanavia, very handsome fruit. $2.00/lb");
      exitLoop = true;
     }
     if (input.equalsIgnoreCase("Alkeme")) {
      System.out.println("A very attractive early Cox-style apple, slightly sharper than Cox, sometimes known as Early Windsor. $9.00/lb");
      exitLoop = true;
     }
     if (input.equalsIgnoreCase("Ambrosia")) {
      System.out.println("A sweet modern apple variety from western Canada, quite similar to Golden Delicious. Discovered as a chance seedling in an orchard in British Columbia. $7.00/lb");
      exitLoop = true;
     }
     if (input.equalsIgnoreCase("Allington Pippin")) {
      System.out.println("A versatile English apple, with a strong pineapple-like flavour, useful for both cooking and eating. $3.00/lb");
      exitLoop = true;
     }
     if (input.equalsIgnoreCase("American Summer Pearmain")) {
      System.out.println("An excellent early eating apple, also good for cooking. Medium size, yellow-green fruit is flushed and streaked red to purple red. Sweet, very juicy flesh. $25.00/lb");
      exitLoop = true;
     }
     if (input.equals("purchase")) {
      System.out.println("What would you like to buy?: ");
      exitLoop = true;
     }
    }
    while (!exitLoop);
    

    【讨论】:

      猜你喜欢
      • 2020-12-24
      • 1970-01-01
      • 2020-08-13
      • 1970-01-01
      • 2015-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多