【问题标题】:How should I structure this while loop? (JAVA)我应该如何构建这个while循环? (JAVA)
【发布时间】:2019-10-07 02:54:28
【问题描述】:

Java 新手。当用户输入为'Y'时,我应该如何构造这个while循环以重新进入循环? while 应该在一开始就去吗?输出示例如下代码。

import java.util.Scanner;

public class RamosSLE33 {


public static void main(String[] args) { 


char cont = 'Y';
String anniversaryGift = " ";
int year = 0;
Scanner input = new Scanner(System.in);

System.out.printf("ANNIVERSARY YEAR%n%n1. 50%n2. 55%n3. 60%n4. None of the above."
                  + "%n%nSelect the anniversary year: ");
year = input.nextInt();

if (year == 1) 
  System.out.printf("The anniversary gift is gold.");
  if (year == 2) 
    System.out.printf("The anniversary gift is emerald.");
    if (year == 3) 
      System.out.printf("The anniversary gift is diamond.");
      if (year == 4) 
        System.out.printf("Go to www.bernardine.com/jewelry-anniv.htm#traditional for more gift choices.");

        cont = 'N';

while(Character.toUpperCase(cont) == 'Y') {
       System.out.printf("%nSearch for another anniversary gift? Enter 'Y' or 'N': ");
       cont = input.nextLine().charAt(0);
} // End while == Y    

} //End main()

} //End class RamosSLE33

SAMPLE OUTPUT

【问题讨论】:

    标签: java if-statement while-loop nested-loops


    【解决方案1】:

    你在程序中几乎没有错误。

    1. 您的 while 循环不会重复运行整个程序

    2. 扫描器输入可能是资源泄漏,因此您尚未关闭它。

    请参考以下更正后的程序

    public class RamosSLE33 {
    
        public static void main(String[] args) {
    
            char cont = 'Y';
            int year = 0;
            Scanner input = new Scanner(System.in);
    
            while (Character.toUpperCase(cont) == 'Y') {
                System.out.printf("ANNIVERSARY YEAR%n%n1. 50%n2. 55%n3. 60%n4. None of the above."
                        + "%n%nSelect the anniversary year: ");
                year = input.nextInt();
    
                if (year == 1) {
                    System.out.printf("The anniversary gift is gold.");
                } else if (year == 2) {
                    System.out.printf("The anniversary gift is emerald.");
                } else if (year == 3) {
                    System.out.printf("The anniversary gift is diamond.");
                } else if (year == 4) {
                    System.out.printf("Go to www.bernardine.com/jewelry-anniv.htm#traditional for more gift choices.");
                } else {
                    System.out.printf("An invalid Input Number");
                }
                cont = 'N';
    
                System.out.printf("%nSearch for another anniversary gift? Enter 'Y' or 'N': ");
                cont = input.next(".").charAt(0);
            } // End while == Y
            input.close();
            System.out.printf("%n The progrm ends: ");
        } // End main()
    
    }
    

    【讨论】:

    • 请参考以上内容并评论@Sean
    【解决方案2】:

    不确定您是否快捷地复制了代码,但速记 ifs 只做一行,您需要大括号{} 来表示 2。 这部分

      if (year == 4) 
        System.out.printf("Go to www.bernardine.com/jewelry-anniv.htm#traditional for more gift choices.");
    
        cont = 'N';
    

    因此,在该循环之前 cont 始终等于 'N',因此它永远不会进入。这可能就是你的原因,剩下的只是我在做轻量级的代码批评。

    您也可以考虑使用 'Y'.equals(cont.touppercase()) 来避免在 cont 为 null 时出现 nullpointer 异常,尽管我认为您的字符工厂可能会回避先做常量是一个好习惯。

    也许也使用 do while 代替,所以它总是在评估它是否为 Y 并决定继续之前进入获取 input.nextline() 的东西。

    如果您希望它在最后重新输入整个方法,请查看递归。您可能需要将 cont 变量设置为类级别或将其分解为另一个以 cont 作为参数的方法;如果你走那条路,记得有一个退出条件。

    【讨论】:

    • cont = 'N' 和下面的 while 循环是我不确定的。我不知道我是否应该在 if 之前或在第一个用户输入之前使用这些行? @squishy
    • 程序显示礼物类型后我的输出没有显示while循环。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-18
    • 2021-05-25
    • 2011-10-13
    • 1970-01-01
    相关资源
    最近更新 更多