2014-11-26 2 views
1

추가 메서드를 사용하여 사용자가 입력 한 이름의 길이를 계산하려고합니다. .? 나는 그것이 오류를 가지고 내 방식에서 사용자 입력 "ipnut에 액세스하려고하면 내 코드에 문제가 있습니다 무엇다른 방법으로 사용자 입력에 액세스하는 방법

import java.util.Scanner; 

public class LengthOfName { 
    public static void main(String[] args) { 
     Scanner reader = new Scanner(System.in); 
     System.out.println("Type your name: "); 
     String input = reader.nextLine(); 


    calculateCharecters(text); 

} 
public static int calculateCharecters(String text){ 

    int texts = input.length(); 
    System.out.println("Number of charecters: " + texts); 
    return texts; 
} 

을}

답변

1

변화는 calculateCharecters(text);

int characterLength = calculateCharacters(input); //because your method return an int 
System.out.println("Number of charecters: " + characterLength); 
text.length()

public static void main(String[] args) { 

    Scanner reader = new Scanner(System.in); 
    System.out.println("Type your name: "); 
    String input = reader.nextLine(); //"input" store the user input 

    calculateCharecters(input); //and go through this metod 

} 

public static int calculateCharecters(String text) { // now "text" store the user input 

    int texts = text.length(); //here "texts" store the "text" lenght (text.lenght()) or number 
           //of characters 

    System.out.println("Number of charecters: " + texts); //printing number of characters 
    return texts; //returning number of characters so 
} 

는이 작업을 수행 할 수 있어야한다

+0

아! 나는 그것이 쉬운 일임을 알았다. 필요한 것은 사용자 입력을 매개 변수에 넣는 것뿐입니다. –

+0

이어야합니다. 텍스트라는 변수가 없습니다. rt? – Burusothman

0
calculateCharecters(text); 

은 다음과 같아야합니다

calculateCharecters(input); 

입력에 메서드를 전달해야합니다.

텍스트는 사용자가 호출하는 메서드에 대한 매개 변수입니다. o 텍스트. calculateCharecters(text)calculateCharecters(input)
input.length()해야 calculateCharecters(input);

import java.util.Scanner; 

public class LengthOfName { 
    public static void main(String[] args) { 
     Scanner reader = new Scanner(System.in); 
     System.out.println("Type your name: "); 
     String input = reader.nextLine(); 


    calculateCharecters(input); 

} 
public static int calculateCharecters(String text){ 

    int texts = input.length(); 
    System.out.println("Number of charecters: " + texts); 
    return texts; 
} 
} 
관련 문제