-1

当用户输入“Y”值时,我试图将这个程序作为循环运行。我希望输入忽略大小写,以便用户也可以输入“y”或“n”。我会使用 equalsIgnoreCase() 方法吗?如果是这样,我是否想将我的 char 更改为布尔值?大约一个月后,我对 java 还是很陌生,所以任何帮助都将不胜感激。我已经玩了一段时间了,如果没有至少一个错误,我无法弄清楚。我让它循环的事实在这一点上是一个奇迹:)

        char Again = 'Y';
        while(Again == 'Y')
        {
            long product = 1, count = 0;
            System.out.println("This program will generate a table of powers of a number.");
            System.out.println("You just have to tell me what number: \n\n");
            System.out.print("Enter an integer please: ");
            int MyNum = Fred.nextInt();
            while(count<5)
            { product = product * MyNum;
              System.out.print(product + ",");
              count = count + 1;
            }
         System.out.println("\nBye for now.....");
         System.out.print("\n\n\n Try another number (y/n)?");
         String Word = Fred.next();
         Again = Word.charAt(0);
        }
4

1 回答 1

3

char是原始数据类型,它没有成员函数。因此,如果您坚持使用 char,则需要同时检查小写和大写:

 while(Again == 'Y' || Again == 'y')

或者,您可以将 Again 声明为String:Again = Word.toUpperCase();然后使用while("Y".equals(Again))

或者,只是,Again = Word;那么while("Y".equalsIgnoreCase(Again))

于 2015-11-09T04:12:42.147 回答