0

这是我的代码:

package labassignment6;

//Imports:
import java.util.Scanner;

class LabAssignment6
{
    //Method for asking the strings:
    public static String mStrings(int m, Scanner keyboard2)
    {
        String input = "", st = "";
        for (int x = 1; x <= m; x++)
        {
            System.out.println("Enter string " + x + ":");
            input = keyboard2.nextLine();
            keyboard2.next();
        }
    return input;
    } //End of mStrings.

    public static void main(String[] args)
    {
        //Declare a scanner object:
        Scanner keyboard = new Scanner(System.in);

        //Declare variables:
        int m;

        //Ask the user the number of strings they want to read:
        System.out.print("Enter the number of strings you want to read: \n");
        m = keyboard.nextInt();

        //Call the method, and print the concatenated string:
        //mStrings(m, keyboard);
        String stReturned = mStrings(m, keyboard); //Put the returned string into a variable.
        System.out.println(stReturned); //Print the concatenated string.

        //Convert the string to lowercase:
        //String stLower = stReturned.toLowerCase();

    } //End of main.

} //End of class LabAssignment6.

这是输出:

Enter the number of strings you want to read: 
3
Enter string 1:
I
Enter string 2:
need
Enter string 3:
help

BUILD SUCCESSFUL (total time: 10 seconds)

任何帮助深表感谢!

4

1 回答 1

0

我想你想实现这个:

import java.util.Scanner;

public class LabAssignment6 {
    //Method for asking the strings:
    public static String mStrings(int m, Scanner keyboard2)
    {
        String input = "", st = "";
        for (int x = 1; x <= m; x++)
        {
            System.out.println("Enter string " + x + ":");
            st = keyboard2.next();
            input = input + st;
//            result = result+" "+input;
        }
    return input;
    } //End of mStrings.

    public static void main(String[] args)
    {
        //Declare a scanner object:
        Scanner keyboard = new Scanner(System.in);

        //Declare variables:
        int m;

        //Ask the user the number of strings they want to read:
        System.out.print("Enter the number of strings you want to read: \n");
        m = keyboard.nextInt();

        //Call the method, and print the concatenated string:
        //mStrings(m, keyboard);
        String stReturned = mStrings(m, keyboard); //Put the returned string into a variable.
        System.out.println(stReturned); //Print the concatenated string.

        //Convert the string to lowercase:
        //String stLower = stReturned.toLowerCase();

    } //End of main.
}

输出:

Enter the number of strings you want to read: 
3
Enter string 1:
I
Enter string 2:
Need
Enter string 3:
Help

INeedHelp
于 2013-10-24T21:07:11.027 回答