繁体   English   中英

即使没有语法错误,Eclipse 程序也无法运行

[英]Eclipse Program Does Not Run Even With no Syntax Errors

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

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 和编码知之甚少,只有非常基本的命令,所以我不知道出了什么问题。

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

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();
            }
        }
    }
}

if ("exit".equalsIgnoreCase(keyboard.nextLine()))后面有一个if ("exit".equalsIgnoreCase(keyboard.nextLine())) ,所以keyboard.close()总是被执行。

删除; 符合if ("exit".equalsIgnoreCase(keyboard.nextLine()))

但关闭输入不会结束 while 循环。 而是加一个break; 命令退出循环并在所有情况下在循环后关闭键盘,而不仅仅是 for 'exit'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM