English 中文(简体)
Java“预期”
原标题:Java ".class expected"
  • 时间:2012-04-09 22:51:25
  •  标签:
  • java
import java.util.Random;
import java.util.Scanner;

public class Lottery
{
    private int[] lotteryNumbers = new int[5];
    private int counter;
    private int[] userNumbers = new int[5];
    private Scanner keyboard = new Scanner(System.in);  
    public Lottery()
    {
        for(counter = 0; counter < 5; counter++)
        {
            lotteryNumbers[counter] = nextInt(int 10);
        }
    }

There is more code after, but there are no errors there, so I m not gonna include it. Anyway, the line that says "lotteryNumbers[counter] = nextInt(int 10);" get a ".class expected" error.

最佳回答

Java已经知道计算方法参数的类型;在你使用这种方法时,你不需要具体说明。

nextInt(int 10);

应:

nextInt(10);

当然,这假设你实际上有<代码>next 斜线/代码界定。 (在你的法典样本中看不到)

问题回答

The What sint for?

如果你重新尝试投稿,应打上<代码>(int)。

您重新发现这一错误的原因是,当 Java看到一种预期表达的类型名称时,它就认为你试图提及这种类型的类别物体,例如int.}

Java是一种面向目标的语言。 您援引<代码>nextInt(10)? 我看不到。 汇编者将暗中假定这一点。 Lottery? 我看不到。

我认为,你们需要这样的东西:

private Random random = new Random(System.currentTimeMillis());

那么,你应当这样做:

lotteryNumbers[counter] = this.random.nextInt(10);

我与你一样,还有其他问题:

  • Unnecessary "magic" numbers everywhere. It s possible to make this class far more flexible than what you ve got.
  • Mixing input into classes like this is a bad idea. Make an abstraction that you can pass values into and leave where you get them from alone. Think "single responsibility".
  • I don t see why Lottery needs a private data member for user numbers. However, I can see where it might have a method that would accept user numbers and tell whether they won or not. You ve created a poor abstraction, in my opinion.

这可能会让你们坐下来。

Without knowing the specifics of nextInt(), I d suggest the error would be from the int keyword before the parameter you re passing to it. Try:

lotteryNumbers[counter] = nextInt(10);




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签