gpt4 book ai didi

java - 表达式开始非法 public int getInput

转载 作者:太空宇宙 更新时间:2023-11-04 11:41:21 26 4
gpt4 key购买 nike

很抱歉这个新手问题,但是我不明白。在此代码的开头,我收到“非法表达式开始”错误,但我不明白为什么。如有任何帮助,我们将不胜感激。

public int getInput(int length){
System.out.println("Enter an index of your choice:");
Scanner input = new Scanner(System.in);
choice = input.nextInt();
if(choice > length){
System.out.println("The number is out of the array bound");
}
return choice;
}

这是完整的代码

import java.util.Random;
import java.util.Scanner;


public class shoutboxclasswithmethods {

//Tell the system about varibles and activate
int a = 0, i = 0, choice;


/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here

public int getInput(int length){
System.out.println("Enter an index of your choice:");
Scanner input = new Scanner(System.in);
choice = input.nextInt();


if(choice > length){
System.out.println("The number is out of the array bound");
}
return choice;
}

/*
* display all the array content
* @returns a string as per user slected index
*/
public String ShoutOutCannedMessage() {
//declare and intialize the array with ten messages
String[] cannedMessage = {"Hello", "this", "is","a test","for","my",
"homework","Hello World","I love Java","Welcome","Thank you"};
//get the lenght of the array
i = cannedMessage.length;
//display using the loop limiting the loop to only the number of array index+1
for (; a < i; a++) {
System.out.println("Index " + a + " : " +cannedMessage[a]);
}
//get the user input of an index using the getInput() predefined user method
int y = getInput(i);
//select and assign the message at the index to a string variable
String selectedMessage = cannedMessage[y];
//return the string varibale object
return selectedMessage;
}
/* generate a random number within a limit
* returns a string from five string arrays choosen depending on the random number as an index
*/

public String ShoutOutRandomMessage(){
//declare and intialize the arrays
String [] subject={"You","She","It","They","We","He","Who","Which","Is","I"};
String [] verb={"read","breathe","run","draw","study","shake","eat","talk","write","show"};
String [] adjective={"funny", "prickly","hard","peaceful","confident","loud","qualified","wealthy","wrong","academic"};
String [] object={"Java","Jane","course","homework","paper","Fish","Key", "phone","prince","laptop"};
String [] adverb={"daily","equally","easily","beautifully","calmly","closely","thankfully","regularly","always","quickly"};

//generate a rando number with upper bound of 10
Random rand=new Random();
choice=rand.nextInt(10);
//select and assign the message content to a single string
String r= subject[choice] +" "+verb[choice]+" "+adjective[choice]+" "+object[choice]+" "+adverb[choice]+".";
//return the string
return r;

}

}

最佳答案

看起来 main() 没有关闭

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here

}

将其括起来后,我就没有看到“非法表达式开始”错误。

以下链接将提供一些解决此类错误的提示:

http://www.java67.com/2016/08/how-to-fix-illegal-start-of-expression-error-in-java.html

关于java - 表达式开始非法 public int getInput,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42740635/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com