赋值处理创建一个主类,然后是另外两个类,用于固定语句和随机语句。固定语句打印任何固定响应。那部分工作正常。然而,我的随机部分独立工作,但在这种情况下,它没有。我错过了一些将它与正确的班级联系起来的陈述。
这是主要课程:
package it511_module8_milestone4;
import java.util.Scanner;
public class IT511_Module8_Milestone4 {
//Allow for User Input
int UserInput;
Scanner input = new Scanner(System.in);
//Implement shoutOutCannedMessage
public void shoutOutCannedMessage(String[] myArray) {
for (int index = 0; index <= 3; index++) {
//Print out the Array Container
System.out.print(myArray[index] + "\n");
}
}
public void shoutOutRandomMessage(String[] Subject, String[] Verb,
String[] Adjective, String[] Object, String[] Adverb) {
//Random number generator
int num0 = [int] (Math.random()*6); ***This is the issue***
int num1 = (int) (Math.random()*3);
int num2 = (int) (Math.random()*3);
int num3 = (int) (Math.random()*3);
int num4 = (int) (Math.random()*3);
{
//Print Random Message
System.out.println(Subject[num0]+Verb[num1]+Adjective[num2]+
Object[num3]+Adverb[num4]);
}
}
}
这是公共课:
public class RandomMessage {
public static void main(String[] args) {
//String of six names to be stored in the Array
String []Subject;
Subject = new String[6];
//Subject Array Container
Subject[0] = ("Ken");
Subject[1] = ("Erica");
Subject[2] = ("Nathan");
Subject[3] = ("Alana");
Subject[4] = ("Nolan");
Subject[5] = ("Ethan");
//Verb Array
String []Verb;
Verb = new String[3];
//Verb Array Container
Verb[0] = (" threw ");
Verb[1] = (" kicked ");
Verb[2] = (" caught ");
//Adjective Array Container
String []Adjective;
Adjective = new String[3];
//Adjective Array Container
Adjective[0] = ("the red");
Adjective[1] = ("the blue");
Adjective[2] = ("the white");
//Object Array Container
String []Object;
Object = new String[3];
//Object Array Container
Object[0] = (" football ");
Object[1] = (" soccer ball ");
Object[2] = (" baseball ");
//Adverb Array Container
String []Adverb;
Adverb = new String[3];
//Verb Array Container
Adverb[0] = ("hard.");
Adverb[1] = ("far.");
Adverb[2] = ("sadly.");
String list = new RandomMessage().shoutOutRandomMessage(Subject, Verb,
Adjective, Object, Adverb);
}
public String shoutOutRandomMessage(String[] Subject, String[] Verb,
String[] Adjective, String[] Object, String[] Adverb) {
//Random number generator
int num0 = (int) (Math.random()*6);
int num1 = (int) (Math.random()*3);
int num2 = (int) (Math.random()*3);
int num3 = (int) (Math.random()*3);
int num4 = (int) (Math.random()*3);
System.out.println(Subject[num0]+Verb[num1]+Adjective[num2]+
Object[num3]+Adverb[num4]);
//Return statement
String message = ("Great Choice!");
return message;
}
}
答案 0 :(得分:2)
您正在将double转换为int,并且转换的格式使用()
并添加要在其中投射的类或基本类型,因此请将[]
替换为()
。
int num0 = (int) (Math.random()*6);