2017-02-15 1 views
-3

입력 파일에서 정수 집합을 읽도록 프로그램을 얻는 방법. 목표는 배열에 저장 한 다음 입력 값보다 큰 값을 표시하는 것입니다. 또한 정수 배열과 정수 n을 허용하는 greater_than_n()이라는 메서드를 만듭니다. 이 방법의 목적은 당신이 배열의 인덱스를 인쇄하는 것입니다 원하는 무엇 Arrayobject을 인쇄배열 및 메서드 문제

+0

주기와 배열 ... 첫 번째 오류 내용하시기 바랍니다 - ('해야하지만,'인덱스

답변

-1

N

import java.util.Scanner; 
import java.io.*; 
public class Lab5 // File Name{ 
public static void main(String[] args) throws IOException 
{ 
    Scanner userInput = new Scanner(System.in); 
    File Integers = new File("Integers.txt"); 
    Scanner inputReader = new Scanner(Integers); 

    String line = inputReader.nextLine(); 
    System.out.print(line); 
    inputReader.close(); 

    System.out.print("Enter an Integer: "); 

    int userAction = userInput.nextInt(); 
    System.out.println("The numbers in the input file that are greater than " + userAction + " are: "); 

    for (int index = 0; index < Integers.length; index++) 
    { 
     if(Integers[index] > userAction) 
     { 
      System.out.print(Integers + " "); 
     } 
    } 
} 

}보다 숫자가 더 표시하는 것입니다, 이것은 사용하여 수행 할 수 있습니다 :

System.out.print(numbers[index]). 
1

잘못된 방법으로 확인하고 있습니다. 루프는 배열의 길이만큼 반복해야합니다. 그런 다음 배열에 큰 숫자를 인쇄하려면 입력 된 숫자보다 큰 경우 각 요소를 확인하십시오. 그렇다면 색인에 숫자를 인쇄하십시오. 당신은 루프의 모든 단계에 배열을 인쇄하는

Scanner userInput = new Scanner(System.in); 

int[] numbers = {2, -4, 6, 8, 19}; 
System.out.print("Enter an Integer: "); 

int userAction = userInput.nextInt(); 
System.out.println("The numbers in the input file that are greater than " + userAction + " are: "); 

for (int index = 0; index < numbers.length; index++) { 
    if(numbers[index] > userAction) 
     System.out.print(numbers[index] + " "); 
} 
+0

프로그램을 읽는 방법 입력 파일의 정수 집합. 목표는 배열에 저장 한 다음 입력 값보다 큰 값을 표시하는 것입니다. 또한 정수 배열과 정수 n을 허용하는 greater_than_n()이라는 메서드를 만듭니다. 이 방법의 목적은 n보다 큰 숫자를 표시하는 것입니다. – user7549103

1

, 당신은 System.out.print(numbers[index]);에 대한 System.out.print(numbers);을 변경해야 번호를 인쇄합니다.

이상의 숫자를 인쇄하려는 경우 (예 : userAction=3, 출력은 6, 8, 19이어야 함) 알고리즘에 오류가 있습니다. 알고리즘이하는 일은 단지 배열 userAction 번을 출력하는 것입니다. 이 문제를 해결하려면이 조각 사용할 수 있습니다

for (int index = 0; index < numbers.length; index++) 
{ 
    if (numbers[index] > userAction) { 
     System.out.print(numbers[index]); 
    } 
}