2017-11-16 3 views
0

원본 코드에서 오류를 수정하고 올바르게 형식을 지정했습니다. 그러나 코드는 마지막 메소드로 진행하기 전에 String next()를 반복합니다. 나는 그 이유를 이해할 줄 알았지 만, 고칠 때 프로그램이 다시 실패했습니다. 시간 내 주셔서 감사합니다!업데이트 된 util.scanner 반복 방법

import java.util.Scanner; 

public class LearnScanner { 
    public static void main (String[] args) { 
     first(); 
     next(); 
     third(); 
    } 
    public static void first() { 
     Scanner input = new Scanner(System.in); 
     System.out.println("Welcome to Vacation Planner!!"); 
     System.out.print("What is your name?"); 
     String name = input.nextLine(); 
     System.out.print("Nice to meet you " + name + ", where are you travelling to?"); 
     String destination = input.nextLine(); 
     System.out.println("Great! "+destination +" sounds like a great trip"); 
    } 
    public static String next() { 
     Scanner input = new Scanner(System.in); 
     System.out.print("How many days are you going to spend travelling?"); 
     String days = input.nextLine(); 
     System.out.print("How much money in USD are you planning to spend on your trip?"); 
     String budget = input.nextLine(); 
     System.out.print("What is the three letter currency symbol for your travel destination?"); 
     String currency = input.nextLine(); 
     System.out.print("How many " + currency + " are there in 1 USD?"); 
     String currencyConversion = input.nextLine(); 
     return days; 
    } 
    public static void third() { 
     int days = Integer.valueOf(next()); 
     int hours = days * 24; 
     int minutes = hours * 60; 
     System.out.println("If your are travelling for " + days + " days that is the same as " + hours + " hours or " + minutes + " minutes"); 
    } 


    } 

답변

1
내가 옆 방법은(), main 메소드에서 한 번 호출되는 보는 것과

다음

int days = Integer.valueOf(next()); 

에서 세 번째에 다시 호출되는이 어쩌면 당신은 인스턴스 변수를 만들어야합니다라는 일 그 안에 next() 값을 저장하십시오. 그런 다음 third() 메소드에서 변수 값을 사용하십시오. 즉

import java.util.Scanner; 

public class LearnScanner { 
    private static int days = 0; 

    public static void main (String[] args) { 
     first(); 
     days = Integer.parseInt(next()); 
     third(); 
    } 
    public static void first() { 
     Scanner input = new Scanner(System.in); 
     System.out.println("Welcome to Vacation Planner!!"); 
     System.out.print("What is your name?"); 
     String name = input.nextLine(); 
     System.out.print("Nice to meet you " + name + ", where are you travelling to?"); 
     String destination = input.nextLine(); 
     System.out.println("Great! "+destination +" sounds like a great trip"); 
    } 
    public static String next() { 
     Scanner input = new Scanner(System.in); 
     System.out.print("How many days are you going to spend travelling?"); 
     String days = input.nextLine(); 
     System.out.print("How much money in USD are you planning to spend on your trip?"); 
     String budget = input.nextLine(); 
     System.out.print("What is the three letter currency symbol for your travel destination?"); 
     String currency = input.nextLine(); 
     System.out.print("How many " + currency + " are there in 1 USD?"); 
     String currencyConversion = input.nextLine(); 
     return days; 
    } 
    public static void third() { 
     int tempDays = Integer.valueOf(days); 
     int hours = days * 24; 
     int minutes = hours * 60; 
     System.out.println("If your are travelling for " + tempDays + " days that is the same as " + hours + " hours or " + minutes + " minutes"); 
    } 
+0

이 나는 ​​다음과 같은 오류 얻을이 변경 적용한 후 정답 – mvreijn

+0

입니다 : 자바 : 호환되지 않는 유형 : java.lang.String의 내가 .. 당신이 문자열로 변환해야 얻을 –

+0

을 int로 변환 할 수 없습니다 정수. 지금 코드를 리펙토링합니다. –

관련 문제