2017-03-12 1 views
1

JOptionePane에서 이중 입력을 사용하여 사각형 영역을 계산합니다. 그래서 나는 사용자에게 length을 넣고 width을 입력해야합니다. 그러나, 첫 번째 입력 후, 난 바로 뒤에 폭을 doesft 팝업에 대한 두 번째 JOptionPane 리더를 넣어. 나는 이것이 많은 일을 필요로한다는 것을 깨닫는다. 그는 콘솔에서 입력을 기다리고 있기 때문에JOptionPane이 독자를 따라 간다

String inputLength = JOptionPane.showInputDialog("Enter the length of the edge: "); 
int length = Integer.parseInt(inputLength); 

Scanner을 제거

import java.util.Scanner; 
import javax.swing.JOptionPane; 


public class Project3_1 { 

public static void main(String[] args) { 

     Scanner reader = new Scanner(System.in); 
     int length; 
     int width; 
     int surfacearea; 

     JOptionPane.showInputDialog("Enter the length of the edge: "); 
     length = reader.nextInt(); // doesnt work past this 
     JOptionPane.showInputDialog("Enter the width of the edge: "); 
     width = reader.nextInt(); 

     surfacearea = length * width; 

     JFrame someFrame = new JFrame(); // how to insert surfacearea?? 
     JLabel label = new JLabel(); 
     someFrame.add(label); 
     someFrame.setSize(230, 230); 
     someFrame.setVisible(true); 
    } 
} 
+0

귀하의 질문은 무엇인가? 또는 무엇을 원하니? –

+0

@Yohannes 질문은 두 번째 입력을하지 않는 이유입니다. – minigeek

+0

아래의 minigeek의 답변을 참조하십시오. 또한 "표면적 삽입"이란 무엇을 의미합니까? –

답변

1

프로그램에 데이터를 입력하는 방법이 혼합되어 있습니다. 시작합시다 :

Scanner reader = new Scanner(System.in);

줄 위의 키보드에서 명령 행에서 데이터를 잡을 수 있습니다.

JOptionPane.showInputDialog("Enter the length of edge: "); 

이 옵션 창은 올바르게 표시되고 값을 입력하면 아무 반응이 없습니다. 프로그램이 명령 줄에 뭔가를 넣어 때까지 reader.nextInt()프로그램을 멈 춥니 다 명령 줄

length=reader.nextInt(); 

프로그램이 위의 라인에 도착에 입력 뭔가 기다리고 있기 때문이다.

이 같은이어야합니다 올바른 방법 :

length = Integer.parseInt(JOptionPane.showInputDialog("Enter the length of the edge: ")); 
width = Integer.parseInt(JOptionPane.showInputDialog("Enter the width of the edge:")); 

및 제거 :

length = reader.nextInt(); 
width = reader.nextInt(); 
+0

감사합니다! 내가 이해 – albanian

+0

당신은 오신 것을 환영합니다 :) – minigeek

1

는이 같은 JOptionPane의 입력을 처리해야합니다.

+0

'너는'JFrame'의 크기와는 아무런 관련이없는 변수'width'와'length'을 오해하지만 계산을하기위한 임무의 일부라고 생각합니다. –

+0

사실이 부분을 삭제합니다 –

관련 문제