我是Java的新手,并且一直试图弄清楚为什么javac在尝试运行时一直给我一个错误。我真的很难过,不理解我的错误。这是出现在错误消息中的代码,它是游戏修改的一部分。
public static LOTRFaction forName(String name)
{
for (LOTRFaction f : ) {
if (f.codeName().equals(name)) {
return f;
}
}
return null;
}
public static LOTRFaction forID(int ID)
{
for (LOTRFaction f : ) {
if (f.ordinal() == ID) {
return f;
}
}
return null;
}
答案 0 :(得分:0)
您收到语法错误的原因是您错过了Enhanced for loop
的一部分。增强的for循环需要以下语法:
for (Object my_object : my_object_iterable) {
// Do stuff here
}
my_object
分配给my_object_iterable
中每个值的in
。它与使用my_object_iterable
关键字的循环很像python。 import java.util.Random;
import java.io.*;
import java.util.*;
/**
Courtney Fox
Professor Yao
Midterm Part 1
10/10/17
Purpose:
The purpose of this program is to develop a Nim game that consists of a
pile of stones ranging from 10-16. From that pile, both the player and
computer have to pick up to 3 stones and whoever gets the last stone
loses.
Logic:
**/
public class FoxMidQ1
{
public static void main(String[] args)
{
//Variables
int user = 0;
//int computer;
//int loser;
int gamenum = 0;
//Scanner
Scanner input = new Scanner(System.in);
//Welcome Output
System.out.println("Welcome to Nim Game!");
//Get pile size: Randomly generate 10-16
int[] pile = {10, 11, 12, 13, 14, 15 , 16};
int stones = pile[(int)(Math.random() * pile.length)];
System.out.println("Game #"+ (gamenum + 1) +": There are "+ stones + "
stones in the pile.");
System.out.println("You can remove up to 3 stones from pile at a
time.");
//User takes stones
System.out.println("How many stones would you like to remove? ");
user = input.nextInt();
必须是可迭代对象(Array,List,ArrayList等)并包含相同类型的my_object。你有一个没有可迭代的for循环! java编译器不知道你想要循环什么,因为你没有提供任何东西。