2013-10-24 48 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

你是不是在這裏做任何級聯。 (除了輸入字符串「+ 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. 
} 

OUTPUT:

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

INeedHelp 
相關問題