首页 文章

如何使循环运行与用户输入一样多次?

提问于
浏览
0

我有一个for循环,调用它的方法是printMe(),我希望用户能够确定它与输入运行的次数 . 这与我认为应该是的一致:

System.out.println("Enter a number: ");
  loop = input.nextInt();

4 回答

  • 2

    是的,如果您的变量输入来自类 Scanner ,那么它是正确的 . 你可以像这样声明变量 sc ,如果你想从用户那里读取更多的输入,可以多次调用它 .

    Scanner sc = new Scanner(System.in); 
    System.out.println("Enter a number: ");
    loopNum = sc.nextInt();
    
  • 1

    借助扫描仪读取用户输入

    Scanner sc = new Scanner(System.in);
    int loopNum = sc.nextInt();
    for (int i=); i<loopNum; i++){
        printMe();
    }
    
  • 2

    这应该工作:

    Scanner input = new Scanner(System.in);
    int loop = input.nextInt();
    for(int i = 0; i<loop;i++){
        //do the thing
    }
    
  • 1

    应该是这样的:

    for (int i=0; i<loopNum; i++)
      printMe();
    

    干杯

相关问题