Java 即使没有语法错误,Eclipse程序也不会运行

Java 即使没有语法错误,Eclipse程序也不会运行,java,arrays,eclipse,Java,Arrays,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

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

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();
        }
    }
}
packagemain;
导入java.util.Scanner;
公共课堂作业{
公共静态void main(字符串[]args){
扫描仪键盘=新扫描仪(System.in);
字符串[]名称=新字符串[10];
int指数=0;

而(index进行一些调整,您的程序就可以运行了。 我没有考虑这是否是一个好的解决方案,但我认为这是您想要做的。因此,现在您可以改进您的解决方案,如何退出循环,如何使用扫描仪等等

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();
            }
        }
    }
}
<代码>包装主; 导入java.util.Scanner; 公共课堂作业{ 公共静态void main(字符串[]args){ 扫描仪键盘=新扫描仪(System.in); 字符串[]名称=新字符串[10]; int指数=0;
而(index在
if(“exit”.equalsIgnoreCase(keyboard.nextLine())
)之后有一个半圆,所以
keyboard.close()
总是被取消

根据
if(“exit”.equalsIgnoreCase(keyboard.nextLine()))
删除


但是关闭输入不会结束while循环。而是添加一个
break;
命令退出循环,并在所有情况下关闭循环后的键盘,而不仅仅是“退出”while(在这里索引无限循环。检查语法。修复后,您将遇到
ArrayIndexOutOfBoundsException
,但简单的谷歌搜索将对您进行排序。然后您将遇到
IllegalStateException
。您可以再次在谷歌上搜索,或者只需更好地检查
扫描仪
API即可。@Mena
(index@GyroGearless不是OP想要做的;)你在Eclipse中打开了控制台窗口吗(菜单:window>Show View>Console)?我想如果(“exit.equalsIgnoreCase(keyboard.nextLine());
这样它就不会运行你的
键盘。close();
我猜。这仍然不适用于我,它不喜欢while循环。你是否复制、编译并运行了这段代码?我不知道你想做什么,但它肯定能工作。你可以在控制台中输入值并亲自查看。或者你必须给我们更多关于你真正需要的信息,因为所有答案中的提示都是正确的。在更改代码的过程中,请将代码单独设置为do循环,以避免出现这种情况,谢谢您的帮助!