2013-10-24 2 views
0

Вот мой код:Java: Как объединить строку в методе, получить ее в основном методе и использовать ее в основном методе?

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) 

Любая помощь очень ценится!

+0

Вы не делаете никакой конкатенации здесь. (кроме 'Enter string" + x + ":" ') –

+5

Что не работает для вас? Что вы пробовали? Что вы ожидаете от этого кода и что он на самом деле делает? –

+2

Ха-ха, это похоже на домашнюю работу – Andy

ответ

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 
Смежные вопросы