首页 > 解决方案 > 即使没有语法错误,Eclipse 程序也不会运行

问题描述

所以我需要为用户创建一个程序来输入匹配分数并在提示时输入“退出”来退出。但是,即使没有错误,此代码也不会运行

package Main;

import java.util.Scanner;

public class Assignment {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);

        String[] names = new String[10];


        int index = 0;
        while (index<10) {

            index++;
            System.out.print("Home team name: ");
            names[index] = keyboard.nextLine();


            System.out.print("Away team name: ");
            names[index] = keyboard.nextLine();

            System.out.print("Enter home score: ");
            names[index] = keyboard.nextLine();

            System.out.print("Enter away score: ");
            names[index] = keyboard.nextLine();

            System.out.print("If you would liketo quit type exit: ");
            if ("exit".equalsIgnoreCase(keyboard.nextLine()));

            keyboard.close();
        }
    }
}

在这一点上,我对 Java 和编码知之甚少,只有非常基本的命令,所以我不知道出了什么问题。

标签: javaarrayseclipse

解决方案


一些调整和你的程序可以工作。我没有考虑这是否是一个好的解决方案,但我认为这就是你想要做的。所以现在你可以改进你的解决方案,你将如何退出循环,你想如何使用扫描仪等等。

package Main;
import java.util.Scanner;

public class Assignment {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);

        String[] names = new String[10];

        int index = 0;
        while (index<10) {
            index++;
            System.out.print("Home team name: ");
            names[index] = keyboard.nextLine();

            System.out.print("Away team name: ");
            names[index] = keyboard.nextLine();

            System.out.print("Enter home score: ");
            names[index] = keyboard.nextLine();

            System.out.print("Enter away score: ");
            names[index] = keyboard.nextLine();

            System.out.print("If you would like to quit type exit: ");
            if ("exit".equalsIgnoreCase(keyboard.nextLine())) {
                index = 10;
                keyboard.close();
            }
        }
    }
}

推荐阅读