在Java中,如何在需要输入时使用for循环?

时间:2018-12-13 00:19:24

标签: java

我只需要有关此程序的帮助。我相信一开始我会因为for循环的应用而陷入困境。当我运行该程序时,它第一次起作用。但是,我可以报错。

上面写着Scanner.nextLine() line: not availiable
它还显示Biggest.main(String[])line:11

我认为第二个是由于断点所致,我曾尝试禁用该断点,但由于某种原因而返回。我在下面粘贴了我的代码。感谢您的帮助!

import java.util.Scanner;

public class Biggest {

    public static void main(String[] args) {


        for (int i=1; i<=5; i++) { 
            System.out.println("Enter input:");
            Scanner input = new Scanner(System.in);
            String value = input.nextLine();
            System.out.println("Enter target:");
            int target = input.nextInt();
            input.close();

            while (value.length() != 4)
                value = ("0" + value);

            int a = value.charAt(0)-'0';
            int b = value.charAt(1)-'0';
            int c = value.charAt(2)-'0';
            int d = value.charAt(3)-'0';

            int combo1 = a + b + c + d;
            int combo2 = (a*1000) + (b*100) + (c*10) + d;
            int combo3 = a + (b*100) + (c*10) + d;
            int combo4 = ((a*10) + b) + ((c*10) + d);
            int combo5 = ((a*100) + (b*10) + c + d);
            int combo6 = a + (b*10 + c) + d;
            int combo7 = ((a*10) + b) + c + d;
            int combo8 = a + b + ((c*10)) + d;     

            int best = 0;
            if (combo1 < target) {
                best = combo1;
            }

            if (combo2 < target && combo2 > best) {
                best = combo2;
            }

            if (combo3 < target && combo3 > best) {
                best = combo3;
            }

            if (combo4 < target && combo4 > best) {
                best = combo4;
            }

            if (combo5 < target && combo5 > best) {
                best = combo5;
            }

            if (combo6 < target && combo6 > best) {
                best = combo6;
            }

            if (combo7 < target && combo7 > best) {
                best = combo7;
            }

            if (combo8 < target && combo8 > best) {
                best = combo8;
            }



            System.out.println(best);

        }

    }
}

0 个答案:

没有答案