java菜鸟级问题高分求助

来源:百度知道 编辑:UC知道 时间:2024/06/15 01:31:16
小弟刚刚开始学JAVA 下面是我自己做的一个练习题 编译不成功 具体问题我注释在里面了 用MyEclipse7.0编的
------------------------------------------------------------------
//编写程序以产生一个随机整数,判断其能否被2、3、5、6、10、15、30整除。(At page37 exercise 8);
package Chapter_1_Java基础语法;

import java.util.Random;
public class task_1
{
static int two=2; int three=3; int five=5; int six=6; int ten=10; int fifteen=15; int thirty=30;
static String ShowIt="";
static Random x=new Random();
public static void main(String[]args)
{
if(x%two==0)//出错了~硬是搞不来了...我这里的错误提示是:
//The operator % is undefined for the argument type(s) Random, int
//在网上查了下说是%没有定义,但是具体如何解决还是不知道...请老师指点下.
{
ShowIt+="该随机数能被2整除";
}
if(x%three==0)
{
ShowIt+="该随机数能被3整除";
}
if(x%five==0)
{
ShowIt+="该随机数能被5整除";
}
if(x%six==0)
{
ShowIt+="该随机数能被6整除";
}

我先纠正你一个错误, 类名的第一个字母要用大写,而作为函数名的第一个子母要用小写:) 是书写规范。

第二是如果你还不了解类的构造,容器的含义,你就将所有函数定义在main里。(我想你暂时还不需要定义类 : )

第三也就是最重要的,你将Random类的用法用错了,他用new创造的实例是一个类,而不是一个int类型的整数,需要用他的nextInt()方法获得一个整数。

最后加上一个额外的结果,不能被以上任何数正处,并输出showIt这个结果。得知能被多少整除:)

你看我修改的一下代码;

import java.util.Random;
public class Task1
{
public static void main(String[]args)
{
int two=2, three=3, five=5, six=6, ten=10, fifteen=15, thirty=30;
String showIt = "" ;
Random gen = new Random();
int x = gen.nextInt(); //这里是这样子用的,new Random()得到的是一个产生伪随机数的类,
//然后使用他的.nextInt()方法才能得到一个int值
if(x%two==0)
{
showIt+="该随机数能被2整除";
}
if(x%three==0)
{
showIt+="该随机数能被3整除";
}
if(x%five==0)
{
showIt+="该随机数能被5整除";
}
if(x%six==0)
{
showIt+="该随机数能被6整除";
}
if(x%ten==0)