2014-09-09 3 views
-3

3.2 : 큐브 합계 큐브 합계를 인쇄하는 응용 프로그램을 작성하십시오. 두 개의 정수 값을 요구하고 읽고 각 값의 합을 세 번째로 인쇄합니다.큐브 합계를 인쇄하는 응용 프로그램 작성

프롬프트, 레이블 및 출력의 지정 : 코드는 다음과 같은 프롬프트를 사용해야합니다 : "정수 1 :"및 "정수 2". 프롬프트는 사용자가 다음 줄에 필요한 입력을 강요해서는 안됩니다. 모든 입력을 읽은 후에 출력은 "이 숫자들의 큐브 합계는 다음과 같습니다."라는 한 줄로 구성되어야합니다. 그 다음에 계산 된 값이옵니다. integer1은 : 예를 들어 3 호 integer2 : 5 이 숫자의 큐브의 합은 다음과 같습니다

: 이름 (152)

사양 : 애플리케이션 클래스가


오류 CubeSum 호출해야

예상 출력 :

integer1은 : · ↵ 호 integer2 · ↵ · · · · ·이 · 수의 · 큐브 인의 · 합 · 9

실제 출력 :

integer1은 : 호 integer2 · ·이 · 큐브 · 합 · 인 · 9↵


My Code: 

import java.util.*; 

    public class CubeSum { 

     public static void main (String args []) { 

      Scanner scan = new Scanner(System.in); 

      int integer1, integer2, cube1, cube2; 
      System.out.print("integer1: "); 

      integer1=scan.nextInt(); 
      cube1 = (int)Math.pow(integer1 ,3); 
      System.out.print("integer2: "); 

      integer2=scan.nextInt(); 
      cube2 = (int)Math.pow(integer2 ,3); 
      System.out.println("the sum of these cubes is: " + (cube1 + cube2)); 

     } 
    } 
+0

이 큐브의 합계가 다음과 같이 누락되었습니다. – ajb

+0

실제로 컴파일러 오류에 대한 링크를 게시하는 것이 아니라 실제로 질문해야합니다. 단, 합계를 인쇄하는 행의 문자열 concat에 +가 누락되었습니다. –

+0

게시물 자체에 오류 메시지를 포함 시키십시오. 클릭하지 않을 링크가 아닙니다. – rgettman

답변

0

변경이 라인 :

System.out.println("the sum of these cubes is: " + (cube1 + cube2)); 

출력을 연결하기 위해 +이 누락되었습니다. 또한

, math 클래스, 그래서 당신은 다음과 같이 사용한다 :

cube1 = (int) Math.pow(integer1, 3); 

편집 출력을 인쇄하여 줄에

import java.util.*; 

    public class CubeSum { 

     public static void main (String args []) { 

      Scanner scan = new Scanner(System.in); 

      int integer1, integer2, cube1, cube2; 
      System.out.print("integer1: "); 

      integer1=scan.nextInt(); 
      cube1 = (int)Math.pow(integer1 ,3); 
      System.out.print("integer2: "); 

      integer2=scan.nextInt(); 
      cube2 = (int)Math.pow(integer2 ,3); 
      System.out.println("the sum of these cubes is: " + (cube1 + cube2)); 

     } 
    } 
+0

MyProgrammingLab이 double 대신 int를 사용하라고했기 때문에 이상합니다. 두 번 입을 때 그것이 잘못되었다고 말해줍니다. 이 일은 너무 까다 롭습니다. – grizzlyyy

+0

@grizzlyyy 오류의 이미지를 보여줍니다. – JosEduSol

0

, 당신은 참으로 누락에 ' + ':

줄 바꾸기 :

System.out.println("the sum of these cubes is: " + (cube1 + cube2)); 

또한, 두 개 더 실수있다 :

cub2 = Math.pow(int2, 3); 

이어야

cube1 = math.pow(int1 ,3); 

cube1 = (int)Math.pow(integer1 ,3); 

을해야

cube2 = (int)Math.pow(integer2, 3); 

실제로 'int1'또는 'int2'를 정의하지 않았으므로

+0

원본 게시물의 코드를 업데이트했습니다. JosEdu의 의견에 대한 회신으로 게시 한 오류가 표시됩니다. 왜 저에게 그 오류를 줄지에 대한 어떤 생각? – grizzlyyy

+0

예, 리턴 값 유형을 int로 형변환해야합니다. Math.pow는 double을 반환합니다. 내 편집을 봅니다. 이제 캐스팅이 포함됩니다. –

+0

또는 cube1 및 cube2를 double 유형의 변수로 정의 할 수 있습니다. –

관련 문제