나는 숙제를하고 있는데 가까이 있지만 문제가있다. 방금 패키지에서 다른 클래스를 가져 오는 클래스가 있으므로 이클립스에서 패키지로 작업하는 법을 배웠다. (나는 올바르게 말한 것으로 생각한다.) 메인은 사용자에게 -100과 100 사이의 정수를 입력하라는 메시지를 표시하고 나는 가지고있다. 그것을 확인하는 문제. 문제는 내가 가져 오는 곳이라는 것을 알고 있습니다. 문제를 해결하기 위해 필요한 방향을 확신 할 수 없습니다.가져 오기? 패키지로 작업하기
이것은 내 주요 코드 섹션입니다. 나는 아래 구불 구불 한 선을 얻을 코드
int i = c.
의 마지막 줄에서
import myUtils.util.Console;
public class ConsoleTestApp
{
public static void main(String args[])
{
// create the Console object
Console c = new Console();
// display a welcome message
c.println("Welcome to the Console Tester application");
c.println();
// int
c.println("Int Test");
int i = c.getIntWithinRange("Enter an integer between -100 and 100: ", -101, 101);
c.println();
을 내가 사용하고 있지 않다 알려줍니다 그 (당신이 건너 뛰려면 내 문제는 지난 몇 라인으로 시작) 그래서 내가 다른 클래스에서 그것을 사용하려고하기 때문에이 상황에서 정확히 무엇을 고칠 지 확신하지 못한다. 객체를 만들어야합니까?
콘솔을 가져 와서 제대로 가져온 다른 패키지에있는 클래스가 있습니다. 여기 내 콘솔 클래스에 고정되어있는 코드입니다.
package myUtils.util;
import java.util.Scanner;
public class Console
{
Scanner sc = new Scanner(System.in);
public void print(String s)
{
System.out.println();
}
public void println(String s)
{
System.out.println();
}
public void println()
{
System.out.println();
}
public int getIntWithinRange(String prompt, int min, int max)
{
int i = 0;
boolean isValid = false;
while (isValid == false)
{
System.out.println(prompt);
if (sc.hasNextInt())
{
i = sc.nextInt();
if (i < min)
{
System.out.println("Error! Please enter an integer greater than -100");
}
else if (i > max)
{
System.out.println("Error! Please enter an integer less than 100");
}
else
isValid = true;
}
else
System.out.println("Error! Invalid number value");
sc.nextLine();
}
// return the int
return i;
}
public double getDoubleWithinRange(String prompt, double min, double max)
{
int d = 0 ;
boolean isValid = false;
while (isValid == false)
{
System.out.println(prompt);
if (sc.hasNextInt())
{
//if user chooses menu option less than 1 the program will print an error message
d = sc.nextInt();
if (d < min)
{
System.out.println("Error! Please select menu option 1, 2, or 3");
}
//if the user chooses a menu option greater than 3 the program will print an error
else if (d > max)
{
System.out.println("Error! Please select menu option 1, 2, or 3");
}
//if the option is between 1 and 3 the menu option is valid
else
isValid = true;
}
else
System.out.println("Error! Invalid number value");
sc.nextLine();
}
// return the int
return d;
}
public String getRequiredString(String prompt)
{
return prompt;
}
public String getChoiceString(String prompt, String s1, String s2)
{
return s2;
}
public int getInt(String prompt)
{
return 0;
}
}
내가 잘못된 숫자 값입니다 내 마지막 인쇄가 계속이 프로그램을 실행할 때. 다른 콘솔의 main 메소드에서 코드를 제대로 가져 오지 못했습니까?
아마 관련이 없지만'println (String s)'와'print (String s)'는 인자와 함께 아무 일도하지 않습니다. –
@ Mike Samuel 방금 코드를 작성하기 시작했고 필요한 항목에 대한 스펙이 주어 졌으므로이 코드에 뛰어 들었습니다. 가장 깨끗한 방법은 아니지만 각 방법을 가장 쉬운 순서로 다루기를 원했습니다. –