在system.out.print.ln中打印到屏幕的计数器值

时间:2022-11-13 22:53:22

I am trying to enter Strings into an array Input by user - using Scanner. I am prompting user for total amount of students and saving this in a variable to determine array length. Each loop I would like to ask the question "what is the name of student" (and their position in counter) ie student #1 student# 2 etc The below code seems to be working as is.

我试图通过用户使用扫描仪将字符串输入数组输入。我提示用户输入总数并将其保存在变量中以确定数组长度。每个循环我想问一个问题“学生的名字是什么”(以及他们在柜台中的位置),即学生#1学生#2等以下代码似乎按原样运作。

System.out.println("what is first name of student")

when I add to the end of this + counter+ I get an error message "illegal start of expression"

当我添加到此+计数器的结尾+我收到错误消息“非法开始表达”

Please help, and find entire code below. Thanks

请帮忙,并在下面找到完整的代码。谢谢

public class Student
{
    public static void main(String[]args)//Main method
    {
        Scanner input=new Scanner(System.in);

        int studentNumber;// declare a variable to store # students

        System.out.println( "Please enter the number of students.");

        noStudent=input.nextInt();//take in user input for number of racers

        while (noStudent<2 || noStudent>20)// validate users 
        {
            //while users input is invalid, alert user and prompt user for a valid number
            System.out.println("Invalid input please re-enter a number between 2 and 20.");
            noStudent=input.nextInt();
        }

        String[] nameArray = new String[noStudent];

        for(int counter = 0; counter<nameArray.length; counter++)
        {
            System.out.println("please enter first name of student "  + i+);  
            nameArray[counter] = input.next();
        }
    }//end main
}//end class

2 个解决方案

#1


1  

try to do (if it's java):

尝试做(如果是java):

for(int counter = 0; counter<nameArray.length; counter++)
{
System.out.println("please enter first name of student "  + (counter+1));  
nameArray[counter] = input.next();
}

#2


1  

System.out.println("please enter first name of student "  + i+);

Should be

System.out.println("please enter first name of student "  + counter);

Also, I think you may want to declare the variable noStudent instead of studentNumber. You're using noStudent a bunch of times, but I don't see it declared. And I see studentNumber declared, but I don't see it ever used.

另外,我想你可能想要声明变量noStudent而不是studentNumber。你一直在使用noStudent,但我没有看到它声明。我看到studentNumber宣布,但我没有看到它曾经使用过。

int noStudent;
System.out.println( "Please enter the number of students.");
noStudent=input.nextInt();

#1


1  

try to do (if it's java):

尝试做(如果是java):

for(int counter = 0; counter<nameArray.length; counter++)
{
System.out.println("please enter first name of student "  + (counter+1));  
nameArray[counter] = input.next();
}

#2


1  

System.out.println("please enter first name of student "  + i+);

Should be

System.out.println("please enter first name of student "  + counter);

Also, I think you may want to declare the variable noStudent instead of studentNumber. You're using noStudent a bunch of times, but I don't see it declared. And I see studentNumber declared, but I don't see it ever used.

另外,我想你可能想要声明变量noStudent而不是studentNumber。你一直在使用noStudent,但我没有看到它声明。我看到studentNumber宣布,但我没有看到它曾经使用过。

int noStudent;
System.out.println( "Please enter the number of students.");
noStudent=input.nextInt();