java - 即使没有语法错误,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 和编码知之甚少,只有非常基本的命令,所以我不知道出了什么问题。
解决方案
一些调整和你的程序可以工作。我没有考虑这是否是一个好的解决方案,但我认为这就是你想要做的。所以现在你可以改进你的解决方案,你将如何退出循环,你想如何使用扫描仪等等。
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();
}
}
}
}
推荐阅读
- r - 如何打印取决于矩阵长度的字符串?
- java - 删除字符串的尾随字符
- python - 用字符串和整数从列表中识别整数
- javascript - Toggle multiple with the same id
- laravel - Laravel 在关系中搜索
- python - 具有齐次坐标的 MVP 变换
- ios-app-group - 是否可以将数据从现有 iOS 应用程序发送到应用程序组的 Watch OS 6 独立应用程序成员?
- python - 将 Python 代码称为基于策略的设计的措施是什么?
- node.js - 将数据从服务器实时发送到前端的最佳方式(MEAN)
- asp-classic - MSXML2.ServerXMLHTTP 不在一台服务器上发送(返回?),但在所有其他服务器上发送