2014-11-04 4 views
-1

여기에 또 다른 기본적인 질문이 있습니다. 마지막 질문을 따르고 있습니다. 나는이 질문들이 매우 기본 적이기 때문에 아무도 괴롭히지 않기를 바란다. 먼저 이름을 입력으로 가져 와서 정수로 변경 한 다음 합계를 계산하여이 합계가 작동하지 않는 방정식에 넣어야합니다.왜 합계가 작동하지 않습니까?

시간을내어 도와 주셔서 감사합니다.

package loveindex; 
import java.util.Scanner; 
import java.lang.Math; 
public class LoveIndex { 

public static void main(String[] args) { 
    Scanner scan = new Scanner(System.in); 
    System.out.println("Testing Scanner, write something: "); 
    String testi = scan.nextLine(); 
    testi = testi.toUpperCase(); 
    char[] ascii1 = testi.toCharArray(); 
    for(char ch:ascii1){ 
     // System.out.println((int)ch+" "); 
     int[] a = {(int)ch}; 
     int all = IntStream.of(a).sum(); // >>> here it shows an error and the quick fix as always is just a bigger mess ... 
     System.out.println("The sum is " + a); 
    } 


    System.out.println("Testing Scanner, write something: "); 
    String testi2 = scan.nextLine(); 
    testi2 = testi2.toUpperCase(); 
    char[] ascii2 = testi2.toCharArray(); 
    for(char ch:ascii2){ 
     System.out.println((int)ch+" "); 
    } 
    scan.close(); 
} 
} 
+1

을 받고 어떤 오류? – WannaBeCoder

+0

자바 8을 사용하고 있습니까? – WannaBeCoder

+0

내가 코드에 대해 이미 언급 한 부분은 구문에 대한 오류를 표시 한 다음 빠른 수정을 사용하여 클래스를 만든 다음 구문 "OF"의 다른 부분에 대한 오류를 보여줍니다. –

답변

2

시도 하시겠습니까?

public static void main(String[] args) { 
    Scanner scan = new Scanner(System.in); 
    System.out.println("Testing Scanner, write something: "); 
    String testi = scan.nextLine(); 
    testi = testi.toUpperCase(); 
    char[] ascii1 = testi.toCharArray(); 
    int sum = 0; 
    for (char ch : ascii1) { 
     sum += ch; 
    } 
    System.out.println("Sum: " + sum); 
    scan.close(); 
} 

지속적인 입력이 예 요청으로 :

public static void main(String[] args) { 
    Scanner scan = new Scanner(System.in); 
    System.out.println("Testing Scanner, write something: "); 
    while (scan.hasNext()) { 
     System.out.println("Testing Scanner, write something: "); 
     String testi = scan.nextLine(); 
     testi = testi.toUpperCase(); 
     char[] ascii1 = testi.toCharArray(); 
     int sum = 0; 
     for (char ch : ascii1) { 
      sum += ch; 
     } 
     System.out.println("Sum: " + sum); 
    } 
    scan.close(); 
} 
+0

나는 이것이 맞는 대답이라고 생각합니다. D thanks again .. 다시 한번 감사드립니다. 이제 2 명의 다른 사람들로부터 2 개의 입력을 얻으려면이 코드를 복제해야합니다 ... –

+0

또는 원형 라운드를 반복하고 원하는만큼 많이 추가하여 답변을 추가하십시오. –

+0

:: Love 인덱스를위한 프로그램이므로 2 개의 이름 만 얻을 수 있습니다 ... 임의의 입력에 대해 새로운 질문을 할 것입니다 ... –

1

당신은 변수 당신이 그것을 선언 후 "A"권리를 선언 재입니다. 또한 int에 char을 캐스팅 할 때 int [] a를 배열로 선언 할 필요가 없습니다. int의 각 루프를 외부로 이동하고 0으로 초기화합니다. 그런 다음 각 루프의 외부에있는 합계를 시스템 인쇄로 이동합니다.

public static void main(String[] args) { 
Scanner scan = new Scanner(System.in); 
System.out.println("Testing Scanner, write something: "); 
String testi = scan.nextLine(); 
testi = testi.toUpperCase(); 
char[] ascii1 = testi.toCharArray(); 
int a = 0; 
for(char ch:ascii1){ 
    // System.out.println((int)ch+" "); 
    a += (int)ch; 

} 
    System.out.println("The sum is " + a); 

    System.out.println("Testing Scanner, write something: "); 
    String testi2 = scan.nextLine(); 
     testi2 = testi2.toUpperCase(); 
    char[] ascii2 = testi2.toCharArray(); 
for(char ch:ascii2){ 
    System.out.println((int)ch + " "); 
} 
    scan.close(); 
} 

}

0

이 코드는 자바 8로 컴파일하더라도, 이해되지 않는다 : 입력 문자열의 각 문자에 대해

char[] ascii1 = testi.toCharArray(); 
for(char ch:ascii1){ 
    // System.out.println((int)ch+" "); 
    int[] a = {(int)ch}; 
    int all = IntStream.of(a).sum(); // >>> here it shows an error and the quick fix as always is just a bigger mess ... 
    System.out.println("The sum is " + a); 
} 

는, 당신은의 배열을 생성하는 단일 문자가 int로 형변환 된 다음 단일 int의 합계를 계산합니다.

대신, 당신은 아마 함께 모든 문자를 합계를 : 당신이

char[] ascii1 = testi.toCharArray(); 
int sum = 0; 
for(char ch:ascii1){ 
    sum += ch; 
} 
System.out.println("The sum is " + sum); 
+0

int 로의 캐스트가 필요하지 않으며 char이 자동으로 업 캐스트됩니다. –

+0

@RobertBain 맞습니다. 감사. – Eran