2017-11-16 6 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()の値を格納します。次に、3番目の()メソッドで変数の値を使用します。 Javaの:互換性のない型:すなわち

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に変換することはできません整数。私は今コードをリファクタリングします –

関連する問題