【问题标题】:Need help declaring a variable within a for loop (Java)需要帮助在 for 循环中声明变量(Java)
【发布时间】:2013-10-17 06:56:29
【问题描述】:

我目前正在编写一个程序,但在尝试执行 for 循环时遇到了错误。我想在for循环中声明一个变量,然后一旦该变量获得某个值就中断,但是它返回错误“无法解析为变量”。

这是我的代码

int i = -1;
for (; i == -1; i = index)     
{ 
    Scanner scan = new Scanner(System.in);
    System.out.println("Please enter your first and last name");
    String name = scan.nextLine();
    System.out.println("Please enter the cost of your car,"
                     + "\nthe down payment, annual interest rate,"
                     + "\nand the number of years the car is being"
                     + "\nfinanced, in that order.");
    DecimalFormat usd = new DecimalFormat("'$'0.00");
    double cost = scan.nextDouble();
    double rate = scan.nextDouble();
    int years = scan.nextInt();
    System.out.println(name + ","
                   +  "\nyour car costs " + usd.format(cost) + ","
                   +  "\nwith an interest rate of " + usd.format(rate) + ","
                   +  "\nand will be financed annually for " + years + " years."
                   +  "\nIs this correct?");
    String input = scan.nextLine();
    int index = (input.indexOf('y'));
}

我想运行程序的输出段,直到用户输入是,然后循环中断。

【问题讨论】:

    标签: java variables loops for-loop declare


    【解决方案1】:

    在这里你想使用一个while循环。通常你可以通过大声说出你的逻辑来决定使用哪个循环,而这个变量是(不是)(值)这样做。

    对于你的问题,在循环外初始化变量,然后在里面设置值。

    String userInput = null;
    while(!userInput.equals("exit"){
      System.out.println("Type exit to quit");
      userInput = scan.nextLine();
    } 
    

    【讨论】:

      【解决方案2】:

      你不能这样做。如果变量是在循环内声明的,那么每次运行都会重新创建它。为了成为退出循环的条件的一部分,它必须在它之外声明。

      或者,您可以使用break 键来结束循环:

      // Should we exit?
      if(input.indexOf('y') != -1)
          break;
      

      【讨论】:

        【解决方案3】:

        您可以使用 do-while 代替 for 循环(它更适合这种情况。

        boolean exitLoop= true;
        do
        {
            //your code here
            exitLoop=  input.equalsIgnoreCase("y");
        } while(exitLoop);
        

        【讨论】:

          【解决方案4】:

          对于无限循环,我更喜欢while。

          boolean isYes = false;
          while (!isYes){ 
          Scanner scan = new Scanner(System.in);
          System.out.println("Please enter your first and last name");
          String name = scan.nextLine();
          System.out.println("Please enter the cost of your car,"
                               + "\nthe down payment, annual interest rate,"
                               + "\nand the number of years the car is being"
                               + "\nfinanced, in that order.");
          DecimalFormat usd = new DecimalFormat("'$'0.00");
          double cost = scan.nextDouble();
          double rate = scan.nextDouble();
          int years = scan.nextInt();
          System.out.println(name + ","
                             +  "\nyour car costs " + usd.format(cost) + ","
                             +  "\nwith an interest rate of " + usd.format(rate) + ","
                             +  "\nand will be financed annually for " + years + " years."
                             +  "\nIs this correct?");
          String input = scan.nextLine();
          isYes = input.equalsIgnoreCase("yes");
          }
          

          【讨论】:

            【解决方案5】:

            变量index的作用域是for循环块的局部变量,而不是for循环本身,所以你不能在for循环中说i = index

            反正你不需要index。这样做:

            for (; i == -1;)
            

            甚至

            while (i == -1)
            

            最后……

                i = (input.indexOf('y'));
            }
            

            顺便说一句,我不确定你想要input.indexOf('y')"blatherskyte" 的输入将触发此逻辑,而不仅仅是 "yes",因为输入中有 y

            【讨论】:

            • 添加到上面的答案也处理大写Y,如果用户输入“yay”或类似的东西,也应该处理
            • @KaushikSivakumar 是的,出于“blatherskyte”、“yay”和“Y”的原因,应该改变打破循环的机制。
            猜你喜欢
            • 2015-03-25
            • 2018-04-25
            • 2011-05-28
            • 2011-09-02
            • 2018-12-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多