【问题标题】:my loop keeps looping also its looping the wrong thing?我的循环一直在循环,它也在循环错误的东西?
【发布时间】:2016-01-17 19:28:24
【问题描述】:

boolean test = true;// 我的循环从这里开始,我希望能够在未选择任何案例时循环 //switch 语句,我希望循环在显示错误后返回到案例 1消息。

while (test)

Proceed = Next.nextInt();
switch (Proceed) {

    case 1:// Proceed 
        System.out.println("Please enter your 5 digit pin below.");
        Scanner Pin = new Scanner(System.in);
        int Pincode = Pin.nextInt();
        if (Pincode > 9999 && Pincode < 99999) {
            System.out.println("1)Display Balance");
            System.out.println("2)Withdraw Cash");
            System.out.println("3)Other services");

        } else {
         System.err
                    .println("Sorry,the pin you enterd was incorrect\nyour card is being ejected\nPlease wait...");
        }
        test=false;
        break;

    case 2:// Return Card
        System.err.println("Your card is being ejected.\n Please Wait..");
        test=false;
        break;

    default:  // i want to display this message and send it back to case 1. when i do the method i'm doing it just keeps spamming this message.
        System.err.println("Sorry your request could not be processed.Please re-try");
        test=true;
}

【问题讨论】:

  • proceed的值是多少?
  • 继续 = Next.nextInt();

标签: eclipse loops while-loop switch-statement boolean


【解决方案1】:

如果您想将其发送回案例 1,那么您需要将继续设置为 1。我猜 Next.nextInt() 会给您 1、2,然后是 3,然后是 4,等等。 2会转到默认情况

【讨论】:

  • 不明白,所以我完全删除循环并将继续设置为 1 ?Proceed = Next.nextInt();
  • 尝试放置一个调试语句,以便您可以看到设置的过程
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-30
相关资源
最近更新 更多